Documentation ¶
Index ¶
- Constants
- Variables
- func NewHandler(devMode bool, w io.Writer) slog.Handler
- func StructValue(v any) slog.Value
- type DevModeHandler
- type DevModeHandlerOptions
- type Logger
- func (l *Logger) DebugWithSource(ctx context.Context, source uintptr, msg string, args ...any)
- func (l *Logger) Error(err error, args ...any)
- func (l *Logger) ErrorCtx(ctx context.Context, err error, args ...any)
- func (l *Logger) ErrorWithSource(ctx context.Context, source uintptr, err error, args ...any)
- func (l *Logger) InfoWithSource(ctx context.Context, source uintptr, msg string, args ...any)
- func (l *Logger) WarnWithSource(ctx context.Context, source uintptr, msg string, args ...any)
- func (l *Logger) With(args ...any) *Logger
Constants ¶
const ( Reset = "\033[0m" Red = "\033[31m" Yellow = "\033[33m" Blue = "\033[34m" Gray = "\033[90m" WhiteBold = "\033[37;1m" GrayBold = "\033[90;1m" BGYellow = "\033[43m" BGRed = "\033[41m" BGCyan = "\033[46m" BGGray = "\033[100m" )
Colors and formats
Variables ¶
var (
// Indent the string used to indent attribute groups in the dev mode handler.
Indent = " "
)
Functions ¶
func NewHandler ¶
NewHandler creates a new `slog.Handler` with default options. If `devMode` is true, a `*DevModeHandler` is returned, else a `*slog.JSONHandler`.
func StructValue ¶
StructValue recursively convert a structure, structure pointer or map to a `slog.GroupValue`. If the given value implements `slog.LogValuer`, this value is returned instead. Returns AnyValue if the type is not supported.
Types ¶
type DevModeHandler ¶
type DevModeHandler struct {
// contains filtered or unexported fields
}
DevModeHandler is a `slog.Handler` that writes Records to an io.Writer. The records are formatted to be easily readable by humans. This handler is meant for development use only as it doesn't provide optimal performance and its output is not machine-readable.
func NewDevModeHandler ¶
func NewDevModeHandler(w io.Writer, opts *DevModeHandlerOptions) *DevModeHandler
NewDevModeHandler creates a new `DevModeHandler` that writes to w, using the given options. If `opts` is `nil`, the default options are used.
func (*DevModeHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
func (*DevModeHandler) Handle ¶
Handle formats its argument `Record` in an output easily readable by humans. The output contains multiple lines:
- The first one contains the log level, the time and the source
- The second one contains the message
- The next lines contain the attributes and groups, if any
Each call to `Handle` results in a single serialized call to `io.Writer.Write()`.
type DevModeHandlerOptions ¶
type DevModeHandlerOptions struct { // Level reports the minimum record level that will be logged. // The handler discards records with lower levels. // If Level is nil, the handler assumes `LevelInfo`. // The handler calls `Level.Level()` for each record processed; // to adjust the minimum level dynamically, use a `slog.LevelVar`. Level slog.Leveler }
DevModeHandlerOptions options for the dev mode handler.
type Logger ¶
Logger an extension of standard `*slog.Logger` overriding the `Error()` and `ErrorCtx()` functions so they take an error as parameter and handle `*errors.Error` gracefully.
func (*Logger) DebugWithSource ¶
DebugWithSource logs at `LevelDebug`. The given source will be used instead of the automatically collecting it from the caller.
func (*Logger) ErrorWithSource ¶
ErrorWithSource logs at `LevelError`. The given source will be used instead of the automatically collecting it from the caller.
func (*Logger) InfoWithSource ¶
InfoWithSource logs at `LevelInfo`. The given source will be used instead of the automatically collecting it from the caller.
func (*Logger) WarnWithSource ¶
WarnWithSource logs at `LevelWarn`. The given source will be used instead of the automatically collecting it from the caller.
func (*Logger) With ¶
With returns a new Logger that includes the given arguments, converted to Attrs as in [Logger.Log]. The Attrs will be added to each output from the Logger. The new Logger shares the old Logger's context. The new Logger's handler is the result of calling WithAttrs on the receiver's handler.