log

package
v2.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatText = iota // Log format is TEXT.
	FormatJson        // Log format is JSON.
)

Constants for log formats

View Source
const (
	OutputConsole = iota // Log output is Console.
	OutputFile           // Log output is File.
	OutputBoth           // Log output is Console and File.
)

Constants for log output

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...interface{})

Debug logs a debug message with optional arguments.

func Default

func Default() *slog.Logger

Default returns the default logger instance.

func Error

func Error(msg string, args ...interface{})

Error logs an error message with optional arguments.

func Fatal

func Fatal(msg string, args ...interface{})

Fatal logs a fatal error message with optional arguments and exits the program.

func Info

func Info(msg string, args ...interface{})

Info logs an informational message with optional arguments.

func Init

func Init(opt *Options)

Init initializes the logger with the provided options.

func Log

func Log(level slog.Level, msg string, args ...interface{})

Log logs a message with the specified log level and optional arguments.

func Warn

func Warn(msg string, args ...interface{})

Warn logs a warning message with optional arguments.

func Writer

func Writer() io.Writer

Writer returns the writer associated with the logger.

Types

type Format

type Format int

Format represents the log format type.

type Handler

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

Handler is a wrapper for slog.Handler.

func NewHandler

func NewHandler(opt *Options, writer io.Writer) *Handler

NewHandler creates a new handler based on the provided options and writer.

func (*Handler) Enabled

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

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*Handler) Handle

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

Handle handles the Record. It will only be called when Enabled returns true.

func (*Handler) WithAttrs

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

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

func (*Handler) WithGroup

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

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger is a wrapper for slog.Logger.

func New

func New(opt *Options) *Logger

New creates a new Logger based on the provided options.

type Options

type Options struct {

	// Indicates whether logging is enabled.
	Enable bool `json:"enable" yaml:"enable"`

	// Log format, currently supports Text: 0 and JSON: 1, with Text as the default.
	Format Format `json:"format" yaml:"format"`

	// Log output location Console: 0 or File: 1 or Both: 2, with Console as the default.
	Output Output `json:"output" yaml:"output"`

	// Log level, with supported values LevelDebug: 4, LevelInfo: 0, LevelWarn: 4, and LevelError: 8.
	Level int `json:"level" yaml:"level"`

	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory. If empty, logs will not be written to a file.
	Filename string `json:"filename" yaml:"filename"`

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `json:"maxsize" yaml:"maxsize"`

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `json:"maxage" yaml:"maxage"`

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `json:"maxbackups" yaml:"maxbackups"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress" yaml:"compress"`
}

Options defines configuration options for the logger.

func DefaultOptions

func DefaultOptions() *Options

Options defines configuration options for the logger.

type Output

type Output int

Output represents the log output type.

Jump to

Keyboard shortcuts

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