mirror of
https://codeberg.org/superseriousbusiness/gotosocial.git
synced 2024-12-21 00:21:59 +03:00
a156188b3e
* update dependencies, bump Go version to 1.19 * bump test image Go version * update golangci-lint * update gotosocial-drone-build * sign * linting, go fmt * update swagger docs * update swagger docs * whitespace * update contributing.md * fuckin whoopsie doopsie * linterino, linteroni * fix followrequest test not starting processor * fix other api/client tests not starting processor * fix remaining tests where processor not started * bump go-runners version * don't check last-webfingered-at, processor may have updated this * update swagger command * update bun to latest version * fix embed to work the same as before with new bun Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: tsmethurst <tobi.smethurst@protonmail.com>
107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package pgx
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// The values for log levels are chosen such that the zero value means that no
|
|
// log level was specified.
|
|
const (
|
|
LogLevelTrace = 6
|
|
LogLevelDebug = 5
|
|
LogLevelInfo = 4
|
|
LogLevelWarn = 3
|
|
LogLevelError = 2
|
|
LogLevelNone = 1
|
|
)
|
|
|
|
// LogLevel represents the pgx logging level. See LogLevel* constants for
|
|
// possible values.
|
|
type LogLevel int
|
|
|
|
func (ll LogLevel) String() string {
|
|
switch ll {
|
|
case LogLevelTrace:
|
|
return "trace"
|
|
case LogLevelDebug:
|
|
return "debug"
|
|
case LogLevelInfo:
|
|
return "info"
|
|
case LogLevelWarn:
|
|
return "warn"
|
|
case LogLevelError:
|
|
return "error"
|
|
case LogLevelNone:
|
|
return "none"
|
|
default:
|
|
return fmt.Sprintf("invalid level %d", ll)
|
|
}
|
|
}
|
|
|
|
// Logger is the interface used to get logging from pgx internals.
|
|
type Logger interface {
|
|
// Log a message at the given level with data key/value pairs. data may be nil.
|
|
Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
|
|
}
|
|
|
|
// LoggerFunc is a wrapper around a function to satisfy the pgx.Logger interface
|
|
type LoggerFunc func(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
|
|
|
|
// Log delegates the logging request to the wrapped function
|
|
func (f LoggerFunc) Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{}) {
|
|
f(ctx, level, msg, data)
|
|
}
|
|
|
|
// LogLevelFromString converts log level string to constant
|
|
//
|
|
// Valid levels:
|
|
//
|
|
// trace
|
|
// debug
|
|
// info
|
|
// warn
|
|
// error
|
|
// none
|
|
func LogLevelFromString(s string) (LogLevel, error) {
|
|
switch s {
|
|
case "trace":
|
|
return LogLevelTrace, nil
|
|
case "debug":
|
|
return LogLevelDebug, nil
|
|
case "info":
|
|
return LogLevelInfo, nil
|
|
case "warn":
|
|
return LogLevelWarn, nil
|
|
case "error":
|
|
return LogLevelError, nil
|
|
case "none":
|
|
return LogLevelNone, nil
|
|
default:
|
|
return 0, errors.New("invalid log level")
|
|
}
|
|
}
|
|
|
|
func logQueryArgs(args []interface{}) []interface{} {
|
|
logArgs := make([]interface{}, 0, len(args))
|
|
|
|
for _, a := range args {
|
|
switch v := a.(type) {
|
|
case []byte:
|
|
if len(v) < 64 {
|
|
a = hex.EncodeToString(v)
|
|
} else {
|
|
a = fmt.Sprintf("%x (truncated %d bytes)", v[:64], len(v)-64)
|
|
}
|
|
case string:
|
|
if len(v) > 64 {
|
|
a = fmt.Sprintf("%s (truncated %d bytes)", v[:64], len(v)-64)
|
|
}
|
|
}
|
|
logArgs = append(logArgs, a)
|
|
}
|
|
|
|
return logArgs
|
|
}
|