log

package
v0.0.0-...-44c758a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

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

Examples

Constants

View Source
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"
)
View Source
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

func Debug(ctx context.Context, msg string, keysAndValues ...interface{})

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

func Debugf(ctx context.Context, fs string, args ...interface{})

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

func Debugv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func Error(ctx context.Context, msg string, keysAndValues ...interface{})

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

func ErrorAndReturn(ctx context.Context, msg string, keysAndValues ...interface{}) 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. 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

func ErrorNoCtxAndReturn(msg string, keysAndValues ...interface{}) error

ErrorNoCtx logs a message and variadic key-value pairs. Returns an error with the given message for convenience

func Errorf

func Errorf(ctx context.Context, fs string, args ...interface{})

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

func ErrorfAndReturn(ctx context.Context, fs string, args ...interface{}) error

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

func Errorv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func 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 an error with the given message for convenience

func Fatal

func Fatal(ctx context.Context, msg string, keysAndValues ...interface{})

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

func Fatalf(ctx context.Context, fs string, args ...interface{})

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

func Fatalv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func Info(ctx context.Context, msg string, keysAndValues ...interface{})

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

func Infof(ctx context.Context, fs string, args ...interface{})

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

func Infov(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func SetLoggerLevel(level zapcore.Level)

SetLoggerLevel sets the logging level.

func Warn

func Warn(ctx context.Context, msg string, keysAndValues ...interface{})

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

func Warnf(ctx context.Context, fs string, args ...interface{})

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

func Warnv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func WithGinRequestID(ctx *gin.Context)

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.

func WithRequestID

func WithRequestID(ctx context.Context, requestID uuid.UUID) context.Context

WithRequestID sets the uuid value for the RequestIDContextKey key in the context.

Types

type Config

type Config struct {
	OutputPaths []string
	// note: setting the environment variable LOG_LEVEL will override Config.Level
	Level zapcore.Level
}

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 NewLogger

func NewLogger(config Config) *Logger

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

func NewLoggerWithDefaultFields(config Config, defaultFields []interface{}) *Logger

NewLogger creates a new logger instance. defaultFields is a list of key-value pairs to be included in every log message.

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, msg string, keysAndValues ...interface{})

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

func (l *Logger) DebugNoCtx(msg string, keysAndValues ...interface{})

DebugNoCtx logs a message and variadic key-value pairs.

func (*Logger) Debugf

func (l *Logger) Debugf(ctx context.Context, fs string, args ...interface{})

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

func (l *Logger) Debugv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func (l *Logger) Error(ctx context.Context, msg string, keysAndValues ...interface{})

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

func (l *Logger) ErrorNoCtx(msg string, keysAndValues ...interface{})

ErrorNoCtx logs a message and variadic key-value pairs.

func (*Logger) ErrorNoCtxAndReturn

func (l *Logger) ErrorNoCtxAndReturn(msg string, keysAndValues ...interface{}) error

ErrorNoCtxAndReturn logs a message and variadic key-value pairs. Returns a new error constructed from the message.

func (*Logger) Errorf

func (l *Logger) Errorf(ctx context.Context, fs string, args ...interface{})

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

func (l *Logger) ErrorfAndReturn(ctx context.Context, fs string, args ...interface{}) error

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

func (l *Logger) Errorv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func (l *Logger) Fatal(ctx context.Context, msg string, keysAndValues ...interface{})

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

func (l *Logger) FatalNoCtx(msg string, keysAndValues ...interface{})

FatalNoCtx logs a message and variadic key-value pairs.

func (*Logger) Fatalf

func (l *Logger) Fatalf(ctx context.Context, fs string, args ...interface{})

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

func (l *Logger) Fatalv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func (l *Logger) Info(ctx context.Context, msg string, keysAndValues ...interface{})

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) InfoNoCtx

func (l *Logger) InfoNoCtx(msg string, keysAndValues ...interface{})

InfoNoCtx logs a message and variadic key-value pairs.

func (*Logger) Infof

func (l *Logger) Infof(ctx context.Context, fs string, args ...interface{})

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

func (l *Logger) Infov(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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

func (l *Logger) Warn(ctx context.Context, msg string, keysAndValues ...interface{})

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) WarnNoCtx

func (l *Logger) WarnNoCtx(msg string, keysAndValues ...interface{})

WarnNoCtx logs a message and variadic key-value pairs.

func (*Logger) Warnf

func (l *Logger) Warnf(ctx context.Context, fs string, args ...interface{})

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

func (l *Logger) Warnv(ctx context.Context, skip int, msg string, keysAndValues ...interface{})

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL