Documentation
¶
Overview ¶
Package logger provides tooling for structured logging. With logger, you can use context to add logging details to your call stack.
Example (WithDetails) ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Info(ctx, "foo", logger.Fields{ "userID": 42, "accountID": 24, }) }
Index ¶
- func AsyncLogging() func()
- func ContextWith(ctx context.Context, lds ...LoggingDetail) context.Context
- func Debug(ctx context.Context, msg string, ds ...LoggingDetail)
- func Error(ctx context.Context, msg string, ds ...LoggingDetail)
- func Fatal(ctx context.Context, msg string, ds ...LoggingDetail)
- func Info(ctx context.Context, msg string, ds ...LoggingDetail)
- func LogWithTB(tb testingTB, optionalHijack ...HijackFunc)
- func RegisterFieldType[T any](mapping func(T) LoggingDetail) func()
- func Warn(ctx context.Context, msg string, ds ...LoggingDetail)
- type Details
- type Fields
- type HijackFunc
- type Level
- type Logger
- func (l *Logger) AsyncLogging() func()
- func (l Logger) Clone() Logger
- func (l *Logger) Debug(ctx context.Context, msg string, ds ...LoggingDetail)
- func (l *Logger) Error(ctx context.Context, msg string, ds ...LoggingDetail)
- func (l *Logger) Fatal(ctx context.Context, msg string, ds ...LoggingDetail)
- func (l *Logger) Info(ctx context.Context, msg string, ds ...LoggingDetail)
- func (l *Logger) Warn(ctx context.Context, msg string, ds ...LoggingDetail)
- type LoggingDetail
- type StubOutput
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsyncLogging ¶
func AsyncLogging() func()
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() defer logger.AsyncLogging()() logger.Info(ctx, "this log message is written out asynchronously") }
func ContextWith ¶
func ContextWith(ctx context.Context, lds ...LoggingDetail) context.Context
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() ctx = logger.ContextWith(ctx, logger.Fields{ "foo": "bar", "baz": "qux", }) logger.Info(ctx, "message") // will have details from the context }
func Debug ¶
func Debug(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Debug(ctx, "foo") }
func Error ¶
func Error(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Error(ctx, "foo") }
func Fatal ¶
func Fatal(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Fatal(ctx, "foo") }
func Info ¶
func Info(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Info(ctx, "foo") }
func LogWithTB ¶
func LogWithTB(tb testingTB, optionalHijack ...HijackFunc)
LogWithTB pipes all application log generated during the test execution through the testing.TB's Log method. LogWithTB meant to help debugging your application during your TDD flow. If the optionalHijack argument is not supplied, a default test HijackFunc will be used
Example ¶
package main import ( "go.llib.dev/frameless/pkg/logger" "testing" ) func main() { var tb testing.TB logger.LogWithTB(tb) // somewhere in your application logger.Debug(nil, "the logging message", logger.Field("bar", 24)) }
func RegisterFieldType ¶
func RegisterFieldType[T any](mapping func(T) LoggingDetail) func()
Example (AsLoggingDetails) ¶
package main import ( "go.llib.dev/frameless/pkg/logger" ) func main() { type MyEntity struct { ID string NonSensitiveData string SensitiveData string } // at package level var _ = logger.RegisterFieldType(func(ent MyEntity) logger.LoggingDetail { return logger.Fields{ "id": ent.ID, "data": ent.NonSensitiveData, } }) }
Types ¶
type Fields ¶
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { logger.Error(context.Background(), "msg", logger.Fields{ "key1": "value", "key2": "value", }) }
type HijackFunc ¶ added in v0.170.2
type Logger ¶
type Logger struct { Out io.Writer // Level is the logging level. // The default Level is LevelInfo. Level Level Separator string MessageKey string LevelKey string TimestampKey string // MarshalFunc is used to serialise the logging message event. // When nil it defaults to JSON format. MarshalFunc func(any) ([]byte, error) // KeyFormatter will be used to format the logging field keys KeyFormatter func(string) string // Hijack will hijack the logging and instead of letting it logged out to the Out, // the logging will be done with the Hijack function. // This is useful if you want to use your own choice of logging, // but also packages that use this logging package. Hijack HijackFunc // contains filtered or unexported fields }
var Default Logger
func (*Logger) AsyncLogging ¶
func (l *Logger) AsyncLogging() func()
AsyncLogging will change the logging strategy from sync to async. This makes the log calls such as Logger.Info not wait on io operations. The AsyncLogging function call is where the async processing will happen, You should either run it in a separate goroutine, or use it with the tasker package. After the AsyncLogging returned, the logger returns to log synchronously.
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() l := logger.Logger{} defer l.AsyncLogging()() l.Info(ctx, "this log message is written out asynchronously") }
type LoggingDetail ¶
type LoggingDetail interface {
// contains filtered or unexported methods
}
func ErrField ¶
func ErrField(err error) LoggingDetail
Example ¶
package main import ( "context" "errors" "go.llib.dev/frameless/pkg/logger" ) func main() { ctx := context.Background() err := errors.New("boom") logger.Error(ctx, "task failed successfully", logger.ErrField(err)) }
type StubOutput ¶
func Stub ¶
func Stub(tb testingTB) StubOutput
Stub the logger.Default and return the buffer where the logging output will be recorded. Stub will restore the logger.Default after the test.
Example ¶
package main import ( "go.llib.dev/frameless/pkg/logger" "strings" "testing" ) func main() { var tb testing.TB buf := logger.Stub(tb) // stub will clean up after itself when the test is finished logger.Info(nil, "foo") strings.Contains(buf.String(), "foo") // true }