splitslog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: MIT Imports: 3 Imported by: 0

README

AtomicGo | splitslog

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/splitslog

splitslog

import "atomicgo.dev/splitslog"

Package splitslog provides a handler that splits log records to different handlers based on their level.

The most common use case is to split logs to stdout and stderr based on their level.

func main() {
	splitter := splitslog.Splitter{
		// Debug and info messages are printed to stdout.
		slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),
		slog.LevelInfo:  slog.NewJSONHandler(os.Stdout, nil),
		// Warn and error messages are printed to stderr.
		slog.LevelWarn:  slog.NewJSONHandler(os.Stderr, nil),
		slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),
	}

	handler := splitslog.NewSplitHandler(splitter)
	logger := slog.New(handler)

	logger.Info("info message prints to stdout")
	logger.Error("error message prints to stderr")

	// Output:
	// stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"}
	// stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"}
}

Index

type SplitHandler

SplitHandler is a handler that splits log records to different handlers based on their level.

type SplitHandler struct {
    Splitter Splitter
    // contains filtered or unexported fields
}

func NewSplitHandler
func NewSplitHandler(splitter Splitter) *SplitHandler

NewSplitHandler returns a new SplitHandler.

Example

package main

import (
	"atomicgo.dev/splitslog"
	"log/slog"
	"os"
)

func main() {
	splitter := splitslog.Splitter{
		// Debug and info messages are printed to stdout.
		slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),
		slog.LevelInfo:  slog.NewJSONHandler(os.Stdout, nil),
		// Warn and error messages are printed to stderr.
		slog.LevelWarn:  slog.NewJSONHandler(os.Stderr, nil),
		slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),
	}

	handler := splitslog.NewSplitHandler(splitter)
	logger := slog.New(handler)

	logger.Info("info message prints to stdout")
	logger.Error("error message prints to stderr")

	// stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"}
	// stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"}
}

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

Enabled implements Handler.Enabled.

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

Handle implements Handler.Handle.

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

WithAttrs implements Handler.WithAttrs.

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

WithGroup implements Handler.WithGroup.

type Splitter

Splitter is a map of log levels to handlers. The default log levels (slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError) must be present, otherwise the SplitHandler panics.

type Splitter map[slog.Level]slog.Handler

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

Documentation

Overview

Package splitslog provides a handler that splits log records to different handlers based on their level.

The most common use case is to split logs to stdout and stderr based on their level.

func main() {
	splitter := splitslog.Splitter{
		// Debug and info messages are printed to stdout.
		slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),
		slog.LevelInfo:  slog.NewJSONHandler(os.Stdout, nil),
		// Warn and error messages are printed to stderr.
		slog.LevelWarn:  slog.NewJSONHandler(os.Stderr, nil),
		slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),
	}

	handler := splitslog.NewSplitHandler(splitter)
	logger := slog.New(handler)

	logger.Info("info message prints to stdout")
	logger.Error("error message prints to stderr")

	// Output:
	// stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"}
	// stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SplitHandler

type SplitHandler struct {
	Splitter Splitter
	// contains filtered or unexported fields
}

SplitHandler is a handler that splits log records to different handlers based on their level.

func NewSplitHandler

func NewSplitHandler(splitter Splitter) *SplitHandler

NewSplitHandler returns a new SplitHandler.

Example
package main

import (
	"atomicgo.dev/splitslog"
	"log/slog"
	"os"
)

func main() {
	splitter := splitslog.Splitter{
		// Debug and info messages are printed to stdout.
		slog.LevelDebug: slog.NewJSONHandler(os.Stdout, nil),
		slog.LevelInfo:  slog.NewJSONHandler(os.Stdout, nil),
		// Warn and error messages are printed to stderr.
		slog.LevelWarn:  slog.NewJSONHandler(os.Stderr, nil),
		slog.LevelError: slog.NewJSONHandler(os.Stderr, nil),
	}

	handler := splitslog.NewSplitHandler(splitter)
	logger := slog.New(handler)

	logger.Info("info message prints to stdout")
	logger.Error("error message prints to stderr")

	// stdout: {"time":"2023-09-07T16:56:22.563817+02:00","level":"INFO","msg":"info message prints to stdout"}
	// stderr: {"time":"2023-09-07T16:56:22.564103+02:00","level":"ERROR","msg":"error message prints to stderr"}
}
Output:

func (*SplitHandler) Enabled

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

Enabled implements Handler.Enabled.

func (*SplitHandler) Handle

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

Handle implements Handler.Handle.

func (*SplitHandler) WithAttrs

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

WithAttrs implements Handler.WithAttrs.

func (*SplitHandler) WithGroup

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

WithGroup implements Handler.WithGroup.

type Splitter

type Splitter map[slog.Level]slog.Handler

Splitter is a map of log levels to handlers. The default log levels (slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError) must be present, otherwise the SplitHandler panics.

Jump to

Keyboard shortcuts

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