log

package
v1.2.28 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package log provides sane default loggers using slog.

Index

Examples

Constants

View Source
const DefaultRefreshInterval = time.Second * 5

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() *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. Changing log level at runtime is also supported.

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 RefreshLogLevel

func RefreshLogLevel(l *slog.LevelVar, t *time.Ticker)

RefreshLogLevel updates l's value from env with given interval until ticker is stopped.

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 ParseFormatEnv

func ParseFormatEnv() HandlerFn

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

Jump to

Keyboard shortcuts

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