log

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package log provides sane default loggers using slog.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONHandler

func JSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler

JSONHandler is a LogHandlerFn shim for slog.NewJSONHandler.

func NewDefaultEnvLogger

func NewDefaultEnvLogger(opts ...Opt) *slog.Logger

NewDefaultEnvLogger creates new slog.Logger using sane default configuration and sets it as a default logger. Environment variables can be used to configure loggers format and level. Options can be provided to overwrite defaults.

Name: Value: LOG_LEVEL DEBUG|INFO|WARN|ERROR LOG_FORMAT JSON|TEXT

Note: LOG_FORMAT can't be changed at runtime.

Example
package main

import (
	"log/slog"

	"github.com/elisasre/go-common/log"
)

func main() {
	log.NewDefaultEnvLogger()
	slog.Info("Hello world")
	slog.Error("Some error")
}
Output:

func ParseLogLevel

func ParseLogLevel(level string) slog.Level

ParseFormat turns string into slog.Level using case-insensitive parser. If the input doesn't match to any slog.Level then slog.LevelInfo is used.

func ParseLogLevelFromEnv

func ParseLogLevelFromEnv() slog.Level

ParseLogLevelFromEnv turns LOG_LEVEL env variable into slog.Level using logic from ParseLogLevel.

func ParseSource added in v1.3.5

func ParseSource(source string) bool

func ParseSourceFromEnv added in v1.3.5

func ParseSourceFromEnv() bool

func TextHandler

func TextHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler

TextHandler is a LogHandlerFn shim for slog.NewTextHandler.

Types

type HandlerFn

type HandlerFn func(w io.Writer, opts *slog.HandlerOptions) slog.Handler

HandlerFn is a shim type for slog's NewHandler functions.

func ParseFormat

func ParseFormat(format string) HandlerFn

ParseFormat parses string into supported log handler function. If the input doesn't match to any supported format then JSON is used.

func ParseFormatFromEnv added in v1.3.5

func ParseFormatFromEnv() HandlerFn

ParseFormatFromEnv turns LOG_FORMAT env variable into slog.Handler function using ParseLogFormat.

type Opt added in v1.3.1

type Opt func(*builder)

func WithHandlerFn added in v1.3.1

func WithHandlerFn(h HandlerFn) Opt

WithHandlerFn can be used to provide slog.Handler lazily.

func WithLeveler added in v1.3.1

func WithLeveler(l slog.Leveler) Opt

WithLeveler sets slog.HandlerOptions.Level.

func WithOutput added in v1.3.1

func WithOutput(w io.Writer) Opt

WithOutput sets logger's output.

func WithReplacer added in v1.3.1

func WithReplacer(fn func([]string, slog.Attr) slog.Attr) Opt

WithReplacer sets slog.HandlerOptions.ReplaceAttr.

func WithShortSource added in v1.3.7

func WithShortSource(short bool) Opt

WithShortSource sets slog.ReplaceAttr source file as short format.

func WithSource added in v1.3.1

func WithSource(enabled bool) Opt

WithSource sets slog.HandlerOptions.AddSource.

Jump to

Keyboard shortcuts

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