prettyslog

package module
v0.0.0-...-44f60ae Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: Unlicense Imports: 8 Imported by: 0

README

Prettyslog

prettyslog is a custom Go slog handler that formats log records according to your preferences. It provides a clean, colored text output with configurable options, ensuring your logs are both informative and easy to read.

Features

  • Customizable Log Format: Define how your logs look, including timestamp formats and attribute styling.
  • Color Support: Enhance readability with color-coded log levels and keys.
  • Group Handling: Required group names at the start of each log line, with support for nested groups.
  • Functional Options: Easily configure the handler using functional options.
  • Thread-Safe: Safe for concurrent use by multiple goroutines.

Installation

go get github.com/dikkadev/prettyslog

Usage

package main

import (
    "log/slog"
    "github.com/dikkadev/prettyslog"
)

func main() {
    // Create a new prettyslog handler with the group "MyApp" and default options
    handler := prettyslog.NewPrettyslogHandler("MyApp")

    // Set the default slog logger to use our custom handler
    slog.SetDefault(slog.New(handler))

    // Now, use slog as usual
    slog.Info("Application started", "version", "1.0.0")

    // Using a logger with additional attributes
    logger := slog.With("request_id", "12345")
    logger.Info("User logged in", "username", "johndoe")

    // Using a different group
    dbLogger := logger.WithGroup("db")
    dbLogger.Info("Query executed", "query", "SELECT * FROM users")

    // Including source information
    handlerWithSource := prettyslog.NewPrettyslogHandler("MyApp",
        prettyslog.WithSource(true),
    )
    slog.SetDefault(slog.New(handlerWithSource))
    slog.Info("This log includes source information")
}

Configuration Options

prettyslog uses functional options for configuration:

  • WithTimestamp(enabled bool): Enable or disable timestamps. Default is true.
  • WithTimestampFormat(format string): Set a custom timestamp format. Default is "[15:04:05.000]".
  • WithSource(enabled bool): Include source file and line number. Default is false.
  • WithColors(enabled bool): Enable or disable colors. Default is true.
  • WithLevel(level slog.Leveler): Set the minimum log level. Default is slog.LevelInfo.
  • WithWriter(w io.Writer): Set a custom io.Writer. Default is os.Stderr.
Example with Custom Options
handler := prettyslog.NewPrettyslogHandler("MyApp",
    prettyslog.WithSource(true),
    prettyslog.WithLevel(slog.LevelDebug),
    prettyslog.WithColors(false),
    prettyslog.WithTimestampFormat("[2006-01-02 15:04:05]"),
)
slog.SetDefault(slog.New(handler))

Documentation

Index

Constants

View Source
const (
	FgRed     = "\033[31m"
	FgGreen   = "\033[32m"
	FgYellow  = "\033[33m"
	FgBlue    = "\033[34m"
	FgMagenta = "\033[35m"
	FgCyan    = "\033[36m"
	FgWhite   = "\033[37m"
	FgGray    = "\033[90m"

	ColorReset = "\033[0m"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*options)

func WithColors

func WithColors(enabled bool) Option

func WithLevel

func WithLevel(level slog.Leveler) Option

func WithSource

func WithSource(enabled bool) Option

func WithTimestamp

func WithTimestamp(enabled bool) Option

func WithTimestampFormat

func WithTimestampFormat(format string) Option

func WithWriter

func WithWriter(w io.Writer) Option

type PrettyslogHandler

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

func NewPrettyslogHandler

func NewPrettyslogHandler(group string, opts ...Option) *PrettyslogHandler

func (*PrettyslogHandler) Enabled

func (h *PrettyslogHandler) Enabled(_ context.Context, level slog.Level) bool

func (*PrettyslogHandler) Handle

func (*PrettyslogHandler) WithAttrs

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

func (*PrettyslogHandler) WithGroup

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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