Documentation ¶
Overview ¶
Package logm is a Go log management providing facilities to deal with logs and trace context.
Index ¶
- Constants
- func DebugLogger(name string, w io.Writer) *slog.Logger
- func DefaultLogger(name string, w io.Writer) *slog.Logger
- func DiscardLogger() *slog.Logger
- func LogHandler(l *slog.Logger, next http.Handler) http.Handler
- func NewLogger(name string, w io.Writer, level slog.Level) *slog.Logger
- func RecoverHandler(msg string, l *slog.Logger, next http.Handler) http.Handler
- func TimeElapsed(ctx context.Context, l *slog.Logger, level slog.Level, msg string, ...) func()
- func TraceHandler(next http.Handler) http.Handler
- type File
- type Middleware
- type Trace
Constants ¶
const ( // AppNameKey is the name of the application in structured log. AppNameKey = "app" // AppVersionKey is the version of the application in structured log. AppVersionKey = "version" // HTTPRequestKey is the HTTP request name in structured log. HTTPRequestKey = "req" // HTTPPathKey is the HTTP request path in structured log. HTTPPathKey = "path" // HTTPMethodKey is the HTTP request method in structured log. HTTPMethodKey = "method" // HTTPRemoteAddrKey is the HTTP remote address in structured log. HTTPRemoteAddrKey = "remote_addr" // HTTPQueryKey is the HTTP request query in structured log. HTTPQueryKey = "query" // HTTPResponseKey is the HTTP response name in structured log. HTTPResponseKey = "resp" // HTTPStatusKey is the HTTP response status in structured log. HTTPStatusKey = "status" // HTTPSizeKey is the HTTP response size in structured log. HTTPSizeKey = "size" // PanicKey is a panic in structured log. PanicKey = "panic" // TraceKey is the name of the trace in structured log. TraceKey = "trace" // TraceIDKey is the name of the trace ID in structured log. TraceIDKey = "id" // TraceSpanIDKey is the name of the trace span ID in structured log. TraceSpanIDKey = "span_id" // TraceTimeElapsedKey is the name of the trace time in structured log. TraceTimeElapsedKey = "time_elapsed_ms" )
List of standard key to use to qualify structured log.
const TraceIDHTTPHeader = "X-Trace-Id"
TraceIDHTTPHeader is the name of the HTTP header used to share a trace context ID.
Variables ¶
This section is empty.
Functions ¶
func DebugLogger ¶
DebugLogger returns a new instance of Logger dedicated to debug or test environment. Debug messages are included and each message will include the application name and version.
func DefaultLogger ¶
DefaultLogger returns a new instance of Logger, ready to be use in production mode. Debug messages are ignored and each message will include the application name and version.
func DiscardLogger ¶
DiscardLogger is a logger doing anything. Useful for test purpose and default behavior.
func LogHandler ¶
LogHandler is an HTTP middleware designed to log every request and response.
func NewLogger ¶
NewLogger returns a new instance of Logger where the level is the minimum log level to consider. Each message will include the application name and version.
func RecoverHandler ¶
RecoverHandler is an HTTP middleware designed to recover on panic, log the error and debug the stack trace.
func TimeElapsed ¶
func TimeElapsed(ctx context.Context, l *slog.Logger, level slog.Level, msg string, attrs ...slog.Attr) func()
TimeElapsed allows to monitor the execution time of a function. Example:
defer t.TimeElapsed(ctx, logger, slog.LevelDebug, "func")()
It offers a useful interface to be called in defer statement.
Types ¶
type File ¶
type File = lumberjack.Logger
File is a log file.
func NewFile ¶
NewFile returns a file with this name. This file will be automatically rotated if it its size exceeds the 100 Mo. The newly created file will have this format `name-timestamp.ext` and will be compressed. timestamp is the time at which the log was rotated formatted with the time.Time format of `2006-01-02T15-04-05.000` and the extension is the original extension. For example, with `/data/log/server.log` as name, a backup created at 6:30pm on Nov 11 2016 would use the filename `/data/log/server-2016-11-04T18-30-00.000.log` Each record will have a local time.
type Middleware ¶ added in v0.2.1
Middleware provides some standard HTTP handlers to deal with logs.
func (Middleware) LogHandler ¶ added in v0.2.1
func (m Middleware) LogHandler(next http.Handler) http.Handler
LogHandler is an HTTP middleware designed to log every request and response.
func (Middleware) RecoverHandler ¶ added in v0.2.1
func (m Middleware) RecoverHandler(next http.Handler) http.Handler
RecoverHandler is an HTTP middleware designed to recover on panic, log the error and debug the stack trace. If no error message is provided, we used the default internal error message.
type Trace ¶
Trace represents a trace context.
func NewTrace ¶
func NewTrace() *Trace
NewTrace creates a new Trace with a new generated UUID v4 as identifier.
func NewTraceFromContext ¶
NewTraceFromContext returns a new Trace based on the context.Context. If the trace ID value is not found or blank, a new one is created. Otherwise, we create a trace span with this trace identifier as parent identifier.
func NewTraceFromHTTPRequest ¶
NewTraceFromHTTPRequest returns a new Trace based on the http.Request. If the trace ID value is not found or blank, a new one is created. Otherwise, we create a trace span with this trace identifier as parent identifier.
func NewTraceSpan ¶
NewTraceSpan creates a trace span based on the Trace.
func (*Trace) End ¶
func (t *Trace) End()
End ends the context trace and calculates the time elapsed since its starting.
func (*Trace) NewContext ¶
NewContext creates a new trace context.Context to carry the trace identifier.