plog

package module
v0.0.0-...-1ba0889 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 12 Imported by: 0

README

plog

plog is a wrapper around log/slog to provide a simple interface for logging in go.

motivation

log/slog was introduced to the go standard library in go 1.21. It provides a simple interface for structured logging. In most cases there is no need any more to use third party libraries for logging. plog provides a simple interface to configure the logger and to use it in your application.

installation

go get git.prolicht.digital/golib/plog

usage

Json logger
package main

import (
    "log/slog"
    "git.prolicht.digital/golib/plog"
)

func main() {

    // configure the logger
    loggerConfig := plog.LoggingConfig{
        LevelStr: "DEBUG",
        Handler: "JSON",
    }

    // initialize the logger
    slog.Default(plog.NewLogger(loggerConfig))

    // log something
    slog.Debug("Hello World")

    // or something more complex
    slog.Info("Hello Plogger", "easy", true, "number", 123, "float", 1.23, "nested_json", map[string]interface{}{"key": "value"}, "structs_as_well", loggerConfig)

    // more details for logging can be found here: https://pkg.go.dev/log/slog
}

Graylog logger
package main

import (
    "log/slog"
    "git.prolicht.digital/golib/plog"
)

func main() {

    // configure the logger
    loggerConfig := plog.LoggingConfig{
        LevelStr: "INFO",
        Handler: "GRAYLOG",
        GralogHost: "localhost",
        GraylogPort: 12201,
    }

    // initialize the logger
    slog.Default(plog.NewLogger(loggerConfig))

    // log something
    slog.Info("Hello Graylog")

    // or something more complex
    slog.Info("Hello GrayLogger", "easy", true, "number", 123, "float", 1.23, "nested_json", map[string]interface{}{"key": "value"}, "structs_as_well", loggerConfig)

    // more details for logging can be found here: https://pkg.go.dev/log/slog
}

credits

Documentation

Overview

Package plog is a simple logging package for Go. It is based on the slog package from the Go standard library. It adds a few convienence features, such as the ability to

  • log to graylog
  • log more detailed info in development mode
  • log to multiple writers (eg. stdout/text, stdout/json, stdout/devel, graylog)

Package prettylog provides a pretty logger for slog. inspired by https://github.com/dusted-go/logging/blob/main/prettylog/prettylog.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(cfg *LoggingConfig) *slog.Logger

NewLogger creates a new logger based on the LoggingConfig.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func NewPrettyLogHandler

func NewPrettyLogHandler(opts *slog.HandlerOptions) *Handler

NewHandler returns a new Handler.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

type LoggingConfig

type LoggingConfig struct {
	//  Level is the log level. It is set by GuessLogLevel() based on the LevelStr field.
	Level slog.Leveler

	// LevelStr is the string representation of the log level. Possible values: DEBUG, INFO, WARN, ERROR
	LevelStr string `envconfig:"LOG_LEVEL" default:"INFO" description:"minimum log level to log"`

	// Handler is the log handler. Possible values: DEV, TEXT, JSON, GRAYLOG, GRAYLOGONLY
	// DEV: pretty log handler for development sets log level to DEBUG
	//      and adds source code location to log messages
	// TEXT: text log handler prints structured log messages to stdout in human readable format
	// JSON: json log handler prints structured log messages to stdout in json format
	// GRAYLOG: graylog log handler sends structured log messages to a graylog server
	//          log messages are sent via UDP and printed to stdout
	// GRAYLOGONLY: graylog log handler sends structured log messages to a graylog server
	//              log messages are sent via UDP only. No log messages are printed to stdout
	Handler string `` /* 130-byte string literal not displayed */

	// GraylogHost is the hostname or IP address of the graylog server. (only used if LOG_HANDLER=GRAYLOG)
	GraylogHost string `envconfig:"LOG_GRAYLOG_HOST" default:"localhost" description:"hostname or ip address of the graylog server"`

	// GraylogPort is the port (UDP) of the graylog server. (only used if LOG_HANDLER=GRAYLOG)
	GraylogPort string `envconfig:"LOG_GRAYLOG_PORT" default:"12201" description:"port (UDP) of the graylog server"`
}

LoggingConfig is the configuration for the logger.

Jump to

Keyboard shortcuts

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