rotoslog

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2023 License: MIT Imports: 9 Imported by: 0

README

rotoslog

This package implements a simple log file rotator handler for slog. It works out of the box using the standard JSONHandler (default) and TextHandler for output formatting, but supports custom handlers.

Example using default configuration:

package main

import (
    "log/slog"

    "github/alchemy/rotoslog"
)

func init() {
	h, err := rotoslog.NewHandler(rotoslog.WithFilePrefix("msg-"))
	if err != nil {
		panic(err)
	}
	logger := slog.New(h)
	slog.SetDefault(logger)
}

Example using custom slog-formatter handler:

package main

import (
	"fmt"
	"io"
	"log/slog"
	"os"
	"testing"

    "github/alchemy/rotoslog"

	formatter "github.com/samber/slog-formatter"
)

func init() {
	formatter1 := formatter.FormatByKey("pwd", func(v slog.Value) slog.Value {
		return slog.StringValue("***********")
	})
	formatter2 := formatter.ErrorFormatter("error")

	builder := func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
		formattingMiddleware := formatter.NewFormatterHandler(formatter1, formatter2)
		textHandler := NewTextHandler(w, opts)
		return formattingMiddleware(textHandler)
	}
	h, err := rotoslog.NewHandler(rotoslog.WithHandlerBuilder(builder))
	if err != nil {
		panic(err)
	}
	logger := slog.New(h)
	slog.SetDefault(logger)
}

Documentation

Overview

Package rotoslog provides a slog.Handler implementation that writes to a rotating set of files. Log file names have the following structure: <prefix>(<suffix>|<timestamp>)<extension>. When creating a new handler the user can set various options:

Index

Constants

View Source
const (
	DEFAULT_FILE_DIR            = "log"
	DEFAULT_FILE_NAME_PREFIX    = ""
	DEFAULT_CURRENT_FILE_SUFFIX = "current"
	DEFAULT_FILE_EXTENSION      = ".log"
	DEFAULT_CURRENT_FILE_NAME   = DEFAULT_FILE_NAME_PREFIX + DEFAULT_CURRENT_FILE_SUFFIX + DEFAULT_FILE_EXTENSION
	DEFAULT_FILE_DATE_FORMAT    = "20060102150405"
	DEFAULT_MAX_FILE_SIZE       = 32 * 1024 * 1024
	DEFAULT_MAX_ROTATED_FILES   = 8
)

Variables

This section is empty.

Functions

func CurrentFileSuffix added in v0.2.0

func CurrentFileSuffix(suffix string) optFun

CurrentFileSuffix sets the current logging file suffix.

func DateTimeLayout added in v0.2.0

func DateTimeLayout(layout string) optFun

DateTimeLayout sets the timestamp layout used in rotated file names.

func FileExt added in v0.2.2

func FileExt(ext string) optFun

FileExt sets the log file extension.

func FilePrefix added in v0.2.0

func FilePrefix(prefix string) optFun

FilePrefix sets the logging file prefix.

func HandlerOptions added in v0.2.0

func HandlerOptions(opts slog.HandlerOptions) optFun

HandlerOptions sets the slog.HandlerOptions for the handler.

func LogDir added in v0.2.0

func LogDir(dir string) optFun

LogDir sets the path to the logging directory

func LogHandlerBuilder added in v0.2.0

func LogHandlerBuilder(builder HandlerBuilder) optFun

LogHandlerBuilder sets the HandlerBuilder used for formatting.

func MaxFileSize added in v0.2.0

func MaxFileSize(size uint64) optFun

MaxFileSize sets the size threshold that triggers file rotation.

func MaxRotatedFiles added in v0.2.0

func MaxRotatedFiles(n uint64) optFun

MaxRotatedFiles sets the maximum number of rotated files. When this number is exceeded the oldest rotated fle is deleted.

func NewHandler

func NewHandler(options ...optFun) (slog.Handler, error)

NewHandler creates a new handler with the given options.

func NewJSONHandler

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

NewJSONHandler is a HandlerBuilder that creates a slog.JSONHandler.

func NewTextHandler

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

NewTestHandler is a HandlerBuilder that creates a slog.TextHandler.

Types

type HandlerBuilder

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

HandlerBuilder is a type representing functions used to create handlers to control formatting of logging data.

Jump to

Keyboard shortcuts

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