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" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Info(ctx, "foo", logger.Fields{ "userID": 42, "accountID": 24, }) }
Output:
Index ¶
- 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 RegisterFieldType[T any](mapping func(T) LoggingDetail) any
- func Stub(tb testingTB) *bytes.Buffer
- func Warn(ctx context.Context, msg string, ds ...LoggingDetail)
- type Details
- type Fields
- type 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) Field(key string, value any) 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWith ¶ added in v0.135.0
func ContextWith(ctx context.Context, lds ...LoggingDetail) context.Context
Example ¶
package main import ( "context" "github.com/adamluzsi/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 }
Output:
func Debug ¶
func Debug(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Debug(ctx, "foo") }
Output:
func Error ¶
func Error(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Error(ctx, "foo") }
Output:
func Fatal ¶
func Fatal(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Fatal(ctx, "foo") }
Output:
func Info ¶
func Info(ctx context.Context, msg string, ds ...LoggingDetail)
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { ctx := context.Background() logger.Info(ctx, "foo") }
Output:
func RegisterFieldType ¶ added in v0.135.0
func RegisterFieldType[T any](mapping func(T) LoggingDetail) any
Example ¶
package main import ( "github.com/adamluzsi/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, } }) }
Output:
func Stub ¶
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 ( "github.com/adamluzsi/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 }
Output:
Types ¶
type Fields ¶ added in v0.135.1
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { logger.Error(context.Background(), "msg", logger.Fields{ "key1": "value", "key2": "value", }) }
Output:
type Logger ¶
type Logger struct { Out io.Writer 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 // contains filtered or unexported fields }
var Default Logger
type LoggingDetail ¶ added in v0.135.0
type LoggingDetail interface {
// contains filtered or unexported methods
}
func ErrField ¶ added in v0.135.0
func ErrField(err error) LoggingDetail
Example ¶
package main import ( "context" "errors" "github.com/adamluzsi/frameless/pkg/logger" ) func main() { ctx := context.Background() err := errors.New("boom") logger.Error(ctx, "task failed successfully", logger.ErrField(err)) }
Output:
Click to show internal directories.
Click to hide internal directories.