ulog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: MIT Imports: 13 Imported by: 0

README

ULOG - логгер

ULOG - это GO модуль для создания персонализированного логгера для slog.

  • Поддерживает текстовый и JSON формат логов.
  • Формат логов копирует формат логов из библиотеки monolog для PHP.
  • Поддерживает ротацию логов.

Documentation

Overview

Package ulog provides a configurable logging system based on Go's slog package. It follows the log structure from PHP Symfony monolog package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config Config) slog.Handler

New creates a new slog.Handler using the provided configuration

func NewFileLogger

func NewFileLogger(path string, format string, channel string, maxSize int64, maxBackups int, maxAge int) (slog.Handler, error)

NewFileLogger creates a new logger that writes to a file

func NewJSONLogger

func NewJSONLogger(w io.Writer, channel string) slog.Handler

NewJSONLogger creates a new logger with JSON formatting

func NewTextLogger

func NewTextLogger(w io.Writer, channel string) slog.Handler

NewTextLogger creates a new logger with text formatting

func ParseByteSize

func ParseByteSize(size string) (int64, error)

func WithAttrsContext

func WithAttrsContext(ctx context.Context, attrs ...slog.Attr) context.Context

WithAttrsContext adds slog.Attr values to the context

Types

type Config

type Config struct {
	Formatter  Formatter
	Writer     io.Writer
	Channel    string
	CtxKeyName string
	AddSource  bool
}

Config holds the configuration for the ulog handler

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a default configuration with text formatter and os.Stderr output

type FileWriter

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

FileWriter is a writer that writes to a file with optional rotation

func NewFileWriter

func NewFileWriter(path string, maxSize int64, maxBackups int, maxAge int) (*FileWriter, error)

NewFileWriter creates a new FileWriter

func (*FileWriter) Close

func (w *FileWriter) Close() error

Close implements the io.Closer interface

func (*FileWriter) Write

func (w *FileWriter) Write(p []byte) (n int, err error)

Write implements the io.Writer interface

type Formatter

type Formatter interface {
	Format(record LogRecord) ([]byte, error)
}

Formatter defines the interface for log formatters

type Handler

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

Handler implements slog.Handler interface

func NewHandler

func NewHandler(config Config) *Handler

NewHandler creates a new ulog Handler

func (*Handler) Enabled

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

Enabled implements slog.Handler interface

func (*Handler) Handle

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

Handle implements slog.Handler interface

func (*Handler) WithAttrs

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

WithAttrs implements slog.Handler interface

func (*Handler) WithGroup

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

WithGroup implements slog.Handler interface

type JSONFormatter

type JSONFormatter struct {
	TimeFormat string
}

JSONFormatter formats logs as JSON following Symfony monolog format

func NewJSONFormatter

func NewJSONFormatter() *JSONFormatter

NewJSONFormatter creates a new JSONFormatter

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(record LogRecord) ([]byte, error)

Format implements the Formatter interface

type LogRecord

type LogRecord struct {
	Message   string                 `json:"message"`
	Channel   string                 `json:"channel"`
	Level     int                    `json:"level"`
	LevelName string                 `json:"level_name"`
	DateTime  time.Time              `json:"datetime"`
	Context   map[string]interface{} `json:"context"`
	Extra     map[string]interface{} `json:"extra"`
}

LogRecord represents a log entry following the Symfony monolog structure

type MultiHandler

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

MultiHandler sends log entries to multiple handlers

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

func (*MultiHandler) Enabled

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

Enabled reports whether the handler handles records at the given level.

func (*MultiHandler) Handle

func (h *MultiHandler) Handle(ctx context.Context, record slog.Record) error

Handle dispatches the record to all handlers.

func (*MultiHandler) WithAttrs

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

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments.

func (*MultiHandler) WithGroup

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

WithGroup returns a new Handler with the given group name.

type MultiWriter

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

MultiWriter wraps multiple io.Writers into a single Writer

func NewMultiWriter

func NewMultiWriter(writers ...io.Writer) *MultiWriter

NewMultiWriter creates a new MultiWriter with the provided writers

func (*MultiWriter) AddWriter

func (mw *MultiWriter) AddWriter(w io.Writer)

AddWriter adds a new writer to the MultiWriter

func (*MultiWriter) Write

func (mw *MultiWriter) Write(p []byte) (n int, err error)

Write implements the io.Writer interface It writes the same data to all writers and returns the number of bytes written and the first error encountered (if any)

type TextFormatter

type TextFormatter struct {
	TimeFormat string
}

TextFormatter formats logs as human-readable text

func NewTextFormatter

func NewTextFormatter() *TextFormatter

NewTextFormatter creates a new TextFormatter

func (*TextFormatter) Format

func (f *TextFormatter) Format(record LogRecord) ([]byte, error)

Format implements the Formatter interface

type Writer

type Writer interface {
	io.Writer
	Close() error
}

Writer is an interface that wraps the basic Write method.

type WriterCloser

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

WriterCloser is a simple implementation of Writer that wraps an io.Writer

func NewWriterCloser

func NewWriterCloser(w io.Writer) *WriterCloser

NewWriterCloser creates a new WriterCloser

func (*WriterCloser) Close

func (w *WriterCloser) Close() error

Close implements the io.Closer interface

func (*WriterCloser) Write

func (w *WriterCloser) Write(p []byte) (n int, err error)

Write implements the io.Writer interface

Jump to

Keyboard shortcuts

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