Documentation ¶
Overview ¶
Example (DanglingKey) ¶
setup() Info(context.Background(), "Hello, world!", "myDanglingKey")
Output: {"level":"error","msg":"Ignored key without a value.","caller":"log_test.go:175","ts":"2000-01-01T00:00:00.000000000Z","ignored":"myDanglingKey"} {"level":"info","msg":"Hello, world!","caller":"log_test.go:175","ts":"2000-01-01T00:00:00.000000000Z"}
Example (DefaultFields) ¶
setup() logger = NewLoggerWithDefaultFields(Config{ OutputPaths: []string{"stdout"}, Level: InfoLevel, }, []interface{}{"foo", "bar"}) logger.now = func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) } logger.Info(ctx, "Hello, world!")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:360","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar"}
Example (DefaultFieldsDanglingKey) ¶
setup() logger = NewLoggerWithDefaultFields(Config{ OutputPaths: []string{"stdout"}, Level: InfoLevel, }, []interface{}{"foo", "bar", "foobar"}) logger.now = func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) } logger.Info(ctx, "Hello, world!")
Output: {"level":"error","msg":"defaultFields contains a key without a value.","ignored":"foobar"} {"level":"info","msg":"Hello, world!","caller":"log_test.go:374","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar"}
Example (EnvVarLogLevel) ¶
old := os.Getenv(LOG_LEVEL_ENV_VAR) os.Setenv(LOG_LEVEL_ENV_VAR, "WARN") setup() Info(ctx, "Hello, world!") Warn(ctx, "Hello, world!")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:345","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Level) ¶
setup() logger = NewLogger(Config{ OutputPaths: []string{"stdout"}, Level: InfoLevel, }) logger.now = func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) } Debug(ctx, "Hello, world!") Info(ctx, "Hello, world!")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:335","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Index ¶
- Constants
- func Debug(ctx context.Context, msg string, keysAndValues ...interface{})
- func DebugNoCtx(msg string, keysAndValues ...interface{})
- func Debugf(ctx context.Context, fs string, args ...interface{})
- func DebugfNoCtx(fs string, args ...interface{})
- func Debugv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func Error(ctx context.Context, msg string, keysAndValues ...interface{})
- func ErrorAndReturn(ctx context.Context, msg string, keysAndValues ...interface{}) error
- func ErrorNoCtx(msg string, keysAndValues ...interface{})
- func ErrorNoCtxAndReturn(msg string, keysAndValues ...interface{}) error
- func Errorf(ctx context.Context, fs string, args ...interface{})
- func ErrorfAndReturn(ctx context.Context, fs string, args ...interface{}) error
- func ErrorfNoCtx(fs string, args ...interface{})
- func Errorv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func ErrorvAndReturn(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) error
- func Fatal(ctx context.Context, msg string, keysAndValues ...interface{})
- func FatalNoCtx(msg string, keysAndValues ...interface{})
- func Fatalf(ctx context.Context, fs string, args ...interface{})
- func FatalfNoCtx(fs string, args ...interface{})
- func Fatalv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func Info(ctx context.Context, msg string, keysAndValues ...interface{})
- func InfoNoCtx(msg string, keysAndValues ...interface{})
- func Infof(ctx context.Context, fs string, args ...interface{})
- func InfofNoCtx(fs string, args ...interface{})
- func Infov(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func InjectTestLogger(testLogger *zap.SugaredLogger)
- func SetLoggerConfig(config Config)
- func SetLoggerLevel(level zapcore.Level)
- func Warn(ctx context.Context, msg string, keysAndValues ...interface{})
- func WarnNoCtx(msg string, keysAndValues ...interface{})
- func Warnf(ctx context.Context, fs string, args ...interface{})
- func WarnfNoCtx(fs string, args ...interface{})
- func Warnv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func WithGinRequestID(ctx *gin.Context)
- func WithRequestID(ctx context.Context, requestID uuid.UUID) context.Context
- type Config
- type Logger
- func (l *Logger) Debug(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) DebugNoCtx(msg string, keysAndValues ...interface{})
- func (l *Logger) Debugf(ctx context.Context, fs string, args ...interface{})
- func (l *Logger) Debugv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func (l *Logger) Error(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) ErrorAndReturn(ctx context.Context, msg string, keysAndValues ...interface{}) error
- func (l *Logger) ErrorNoCtx(msg string, keysAndValues ...interface{})
- func (l *Logger) ErrorNoCtxAndReturn(msg string, keysAndValues ...interface{}) error
- func (l *Logger) Errorf(ctx context.Context, fs string, args ...interface{})
- func (l *Logger) ErrorfAndReturn(ctx context.Context, fs string, args ...interface{}) error
- func (l *Logger) Errorv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func (l *Logger) ErrorvAndReturn(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) error
- func (l *Logger) Fatal(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) FatalNoCtx(msg string, keysAndValues ...interface{})
- func (l *Logger) Fatalf(ctx context.Context, fs string, args ...interface{})
- func (l *Logger) Fatalv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func (l *Logger) Info(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) InfoNoCtx(msg string, keysAndValues ...interface{})
- func (l *Logger) Infof(ctx context.Context, fs string, args ...interface{})
- func (l *Logger) Infov(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
- func (l *Logger) Warn(ctx context.Context, msg string, keysAndValues ...interface{})
- func (l *Logger) WarnNoCtx(msg string, keysAndValues ...interface{})
- func (l *Logger) Warnf(ctx context.Context, fs string, args ...interface{})
- func (l *Logger) Warnv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})
Examples ¶
- Package (DanglingKey)
- Package (DefaultFields)
- Package (DefaultFieldsDanglingKey)
- Package (EnvVarLogLevel)
- Package (Level)
- Debug
- Debug (Logger)
- DebugNoCtx
- DebugNoCtx (Logger)
- Debugf
- Debugf (Logger)
- DebugfNoCtx
- Debugv
- Debugv (Logger)
- Error
- Error (Logger)
- ErrorNoCtx
- ErrorNoCtx (Logger)
- Errorf
- Errorf (Logger)
- ErrorfNoCtx
- Errorv
- Errorv (Logger)
- Info
- Info (Logger)
- InfoNoCtx
- InfoNoCtx (Logger)
- Infof
- Infof (Logger)
- InfofNoCtx
- Infov
- Infov (Logger)
- SetLoggerConfig
- Warn
- Warn (Logger)
- WarnNoCtx
- WarnNoCtx (Logger)
- Warnf
- Warnf (Logger)
- WarnfNoCtx
- Warnv
- Warnv (Logger)
Constants ¶
const ( // DebugLevel logs are typically voluminous. DebugLevel = zapcore.DebugLevel // InfoLevel is the default logging priority. InfoLevel = zapcore.InfoLevel // WarnLevel logs are more important than Info, but don't need individual human review. WarnLevel = zapcore.WarnLevel // ErrorLevel logs are high-priority. // Applications running smoothly shouldn't generate any error-level logs. ErrorLevel = zapcore.ErrorLevel // FatalLevel logs a message, then calls os.Exit(1). FatalLevel = zapcore.FatalLevel // RFC3339TrailingNano is RFC3339 format with trailing nanoseconds precision. RFC3339TrailingNano = "2006-01-02T15:04:05.000000000Z07:00" // LOG_LEVEL_ENV_VAR is the environment variable name used to set logging level. LOG_LEVEL_ENV_VAR = "LOG_LEVEL" )
const ( // Best practices for avoiding key collisions in context say this shouldn't be a string type. // Gin forces us to use a string for their context, so choose a name that is unlikely to collide with anything else. RequestIDContextKey = "grail_logger_request_id" )
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
Debug logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Debug(ctx, "Hello, world!") Debug( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:31","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"debug","msg":"Hello, world!","caller":"log_test.go:32","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.Debug(ctx, "Hello, world!") logger.Debug( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:183","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"debug","msg":"Hello, world!","caller":"log_test.go:184","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
func DebugNoCtx ¶
func DebugNoCtx(msg string, keysAndValues ...interface{})
DebugNoCtx logs a message and variadic key-value pairs.
Example ¶
setup() DebugNoCtx("Hello, world!")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:147","ts":"2000-01-01T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.DebugNoCtx("Hello, world!")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:299","ts":"2000-01-01T00:00:00.000000000Z"}
func Debugf ¶
Debugf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Debugf(ctx, "Hello, %s!", "world")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:91","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Debugf(ctx, "Hello, %s!", "world")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:243","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func DebugfNoCtx ¶
func DebugfNoCtx(fs string, args ...interface{})
DebugfNoCtx uses fmt.Sprintf to log a templated message.
Example ¶
setup() DebugfNoCtx("Hello, %s!", "world")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:382","ts":"2000-01-01T00:00:00.000000000Z"}
func Debugv ¶
Debugv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Debugv(ctx, 0, "Hello, world!")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:119","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Debugv(ctx, 0, "Hello, world!")
Output: {"level":"debug","msg":"Hello, world!","caller":"log_test.go:271","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func Error ¶
Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Error(ctx, "Hello, world!") Error( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:76","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"error","msg":"Hello, world!","caller":"log_test.go:77","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.Error(ctx, "Hello, world!") logger.Error( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:228","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"error","msg":"Hello, world!","caller":"log_test.go:229","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
func ErrorAndReturn ¶
Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted. Returns an error with the given message for convenience
func ErrorNoCtx ¶
func ErrorNoCtx(msg string, keysAndValues ...interface{})
ErrorNoCtx logs a message and variadic key-value pairs.
Example ¶
setup() ErrorNoCtx("Hello, world!")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:168","ts":"2000-01-01T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.ErrorNoCtx("Hello, world!")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:320","ts":"2000-01-01T00:00:00.000000000Z"}
func ErrorNoCtxAndReturn ¶
ErrorNoCtx logs a message and variadic key-value pairs. Returns an error with the given message for convenience
func Errorf ¶
Errorf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Errorf(ctx, "Hello, %s!", "world")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:112","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Errorf(ctx, "Hello, %s!", "world")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:264","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func ErrorfAndReturn ¶
Errorf uses fmt.Errorf to construct an error and log its message If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted. Returns the error for convenicence
func ErrorfNoCtx ¶
func ErrorfNoCtx(fs string, args ...interface{})
ErrorfNoCtx uses fmt.Sprintf to log a templated message.
Example ¶
setup() ErrorfNoCtx("Hello, %s!", "world")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:403","ts":"2000-01-01T00:00:00.000000000Z"}
func Errorv ¶
Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Errorv(ctx, 0, "Hello, world!")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:140","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Errorv(ctx, 0, "Hello, world!")
Output: {"level":"error","msg":"Hello, world!","caller":"log_test.go:292","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func ErrorvAndReturn ¶
Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted. Returns an error with the given message for convenience
func Fatal ¶
Fatal logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func FatalNoCtx ¶
func FatalNoCtx(msg string, keysAndValues ...interface{})
FatalNoCtx logs a message and variadic key-value pairs.
func Fatalf ¶
Fatalf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func FatalfNoCtx ¶
func FatalfNoCtx(fs string, args ...interface{})
FatalfNoCtx uses fmt.Sprintf to log a templated message.
func Fatalv ¶
Fatalv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func Info ¶
Info logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Info(ctx, "Hello, world!") Info( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:46","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"info","msg":"Hello, world!","caller":"log_test.go:47","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.Info(ctx, "Hello, world!") logger.Info( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:198","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"info","msg":"Hello, world!","caller":"log_test.go:199","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
func InfoNoCtx ¶
func InfoNoCtx(msg string, keysAndValues ...interface{})
InfoNoCtx logs a message and variadic key-value pairs.
Example ¶
setup() InfoNoCtx("Hello, world!")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:154","ts":"2000-01-01T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.InfoNoCtx("Hello, world!")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:306","ts":"2000-01-01T00:00:00.000000000Z"}
func Infof ¶
Infof uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Infof(ctx, "Hello, %s!", "world")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:98","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Infof(ctx, "Hello, %s!", "world")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:250","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func InfofNoCtx ¶
func InfofNoCtx(fs string, args ...interface{})
InfofNoCtx uses fmt.Sprintf to log a templated message.
Example ¶
setup() InfofNoCtx("Hello, %s!", "world")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:389","ts":"2000-01-01T00:00:00.000000000Z"}
func Infov ¶
Infov logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Infov(ctx, 0, "Hello, world!")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:126","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Infov(ctx, 0, "Hello, world!")
Output: {"level":"info","msg":"Hello, world!","caller":"log_test.go:278","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func InjectTestLogger ¶
func InjectTestLogger(testLogger *zap.SugaredLogger)
func SetLoggerConfig ¶
func SetLoggerConfig(config Config)
SetLoggerConfig sets the logging config.
Example ¶
setup() SetLoggerConfig(Config{ OutputPaths: TestConfig.OutputPaths, Level: InfoLevel, }) logger.now = func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) } Debug(ctx, "Hello, world!") Info(ctx, "Goodbye, world!")
Output: {"level":"info","msg":"Goodbye, world!","caller":"log_test.go:418","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func SetLoggerLevel ¶
SetLoggerLevel sets the logging level.
func Warn ¶
Warn logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Warn(ctx, "Hello, world!") Warn( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:61","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"warn","msg":"Hello, world!","caller":"log_test.go:62","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.Warn(ctx, "Hello, world!") logger.Warn( ctx, "Hello, world!", "foo", "bar", "abc", 123, "time", time.Date(2000, 1, 2, 0, 0, 0, 0, time.UTC), )
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:213","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"} {"level":"warn","msg":"Hello, world!","caller":"log_test.go:214","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","foo":"bar","abc":123,"time":"2000-01-02T00:00:00.000000000Z"}
func WarnNoCtx ¶
func WarnNoCtx(msg string, keysAndValues ...interface{})
WarnNoCtx logs a message and variadic key-value pairs.
Example ¶
setup() WarnNoCtx("Hello, world!")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:161","ts":"2000-01-01T00:00:00.000000000Z"}
Example (Logger) ¶
setup() logger.WarnNoCtx("Hello, world!")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:313","ts":"2000-01-01T00:00:00.000000000Z"}
func Warnf ¶
Warnf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Warnf(ctx, "Hello, %s!", "world")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:105","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Warnf(ctx, "Hello, %s!", "world")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:257","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func WarnfNoCtx ¶
func WarnfNoCtx(fs string, args ...interface{})
WarnfNoCtx uses fmt.Sprintf to log a templated message.
Example ¶
setup() WarnfNoCtx("Hello, %s!", "world")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:396","ts":"2000-01-01T00:00:00.000000000Z"}
func Warnv ¶
Warnv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
Example ¶
setup() Warnv(ctx, 0, "Hello, world!")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:133","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
Example (Logger) ¶
setup() logger.Warnv(ctx, 0, "Hello, world!")
Output: {"level":"warn","msg":"Hello, world!","caller":"log_test.go:285","ts":"2000-01-01T00:00:00.000000000Z","requestID":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"}
func WithGinRequestID ¶
WithGinRequestID creates a uuid that is set as a string on the gin Context and as a uuid on the regular-flavor Request context that it wraps. The context should be passed to the methods in this package to prefix logs with the identifier.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func GetNewLoggerWithDefaultFields ¶
func GetNewLoggerWithDefaultFields(addedInfo ...interface{}) *Logger
Instantiate a new logger and assign any key-value pair to addedInfo field in logger to log additional information specific to service
func NewLoggerFromCore ¶
func NewLoggerFromCore(lager *zap.SugaredLogger) *Logger
NewLoggerFromCore allows the caller to pass in a zap.SugaredLogger into the logger. This allows one to make unit test assertions about logs.
func NewLoggerWithDefaultFields ¶
NewLogger creates a new logger instance. defaultFields is a list of key-value pairs to be included in every log message.
func (*Logger) Debug ¶
Debug logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) DebugNoCtx ¶
DebugNoCtx logs a message and variadic key-value pairs.
func (*Logger) Debugf ¶
Debugf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Debugv ¶
Debugv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller stack field is skipped by skip levels. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Error ¶
Error logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) ErrorAndReturn ¶
func (l *Logger) ErrorAndReturn(ctx context.Context, msg string, keysAndValues ...interface{}) error
ErrorAndReturn logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted. Returns a new error constructed from the message.
func (*Logger) ErrorNoCtx ¶
ErrorNoCtx logs a message and variadic key-value pairs.
func (*Logger) ErrorNoCtxAndReturn ¶
ErrorNoCtxAndReturn logs a message and variadic key-value pairs. Returns a new error constructed from the message.
func (*Logger) Errorf ¶
Errorf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) ErrorfAndReturn ¶
ErrorfAndReturn uses fmt.Errorf to construct an error from the provided arguments. It then logs the error message, along with data from the context. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted. Returns the error resulting from invoking fmt.Errorf with the provided arguments.
func (*Logger) Errorv ¶
Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller stack field is skipped by skip levels. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) ErrorvAndReturn ¶
func (l *Logger) ErrorvAndReturn(ctx context.Context, skip int, msg string, keysAndValues ...interface{}) error
Errorv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller is skipped by skip. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted. Returns a new error constructed from the message.
func (*Logger) Fatal ¶
Fatal logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) FatalNoCtx ¶
FatalNoCtx logs a message and variadic key-value pairs.
func (*Logger) Fatalf ¶
Fatalf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Fatalv ¶
Fatalv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller stack field is skipped by skip levels. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Info ¶
Info logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Infof ¶
Infof uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Infov ¶
Infov logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller stack field is skipped by skip levels. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Warn ¶
Warn logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Warnf ¶
Warnf uses fmt.Sprintf to log a templated message and the key-value pairs defined in contextFields from ctx. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.
func (*Logger) Warnv ¶
Warnv logs a message, the key-value pairs defined in contextFields from ctx, and variadic key-value pairs. Caller stack field is skipped by skip levels. If ctx is nil, all fields from contextFields will be omitted. If ctx does not contain a key in contextFields, that field will be omitted.