logger

package module
v0.0.0-...-37c31ec Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// LevelToString is a map that converts a Level to its string representation.
	LevelToString = map[Level]string{DebugLevel: "debug", InfoLevel: "info", WarningLevel: "warning", ErrorLevel: "error", FatalLevel: "fatal"}

	// StringToLevel is a map that converts a string to its Level equivalent.
	StringToLevel = map[string]Level{"debug": DebugLevel, "info": InfoLevel, "warning": WarningLevel, "error": ErrorLevel, "fatal": FatalLevel}
)
View Source
var (
	ErrInvalidFormat = errors.New("format is invalid")
	ErrInvalidLevel  = errors.New("level is invalid")
)
View Source
var (
	SetFormat = std.SetFormat
	SetLevel  = std.SetLevel
	SetOutput = std.SetOutput
	Output    = std.Output
	Debug     = std.Debug
	Debugf    = std.Debugf
	Info      = std.Info
	Infof     = std.Infof
	Warning   = std.Warning
	Warningf  = std.Warningf
	Warn      = std.Warn
	Warnf     = std.Warnf
	Error     = std.Error
	Errorf    = std.Errorf
	Fatal     = std.Fatal
	Fatalf    = std.Fatalf
)

Aliases of logger functions.

Functions

This section is empty.

Types

type Format

type Format int

Format is used to specify the format in which logs should be written.

const (
	// PlainFormat represents plain text logs (default).
	PlainFormat Format = 0 + iota

	// JSONFormat represents logs in JSON format.
	JSONFormat
)

type Item

type Item struct {
	// Date is the timestamp of the log.
	Date time.Time `json:"date"`

	// File represents the source code file where the log was created.
	File string `json:"file"`

	// Level is a string representation of the logging level (e.g., "info", "warning").
	Level string `json:"level"`

	// Message contains the log entry.
	// It could be a string or a fmt.Stringer interface with json annotations.
	Message any `json:"message"`

	// Line contains the corresponding line number where the log was created.
	Line int `json:"line"`
	// contains filtered or unexported fields
}

Item represents a log item with date, file name, line number, level, and Message.

func NewItem

func NewItem(level Level, file string, line int, message any) *Item

NewItem creates a new Item with the given parameters and current time.

func (*Item) Marshal

func (r *Item) Marshal() ([]byte, error)

Marshal converts an Item to a JSON byte array and returns it. If there's an error during conversion, it wraps the error.

func (*Item) String

func (r *Item) String() string

String returns a string representation of the log item in the format "[LEVEL] [DATE] [FILE]:[LINE]: MESSAGE". The level is colored based on its value.

type Level

type Level int
const (
	// DebugLevel is the lowest level and usually only enabled during development.
	// It provides detailed information useful for debugging purposes,
	// such as tracking variable values or understanding the flow of execution through a program.
	// In production environments, this level should be disabled to reduce log size and improve performance.
	DebugLevel Level = 0 + iota

	// InfoLevel messages are typically used when you want to provide contextual details
	// about normal operations in your application.
	// For example, "Starting server on port 8080" or "Connected to database".
	// These logs can help with monitoring the health of an application and understanding its behavior over time.
	InfoLevel

	// WarningLevel messages are used when something unexpected happened but did not cause the program to fail.
	// For example, "Failed to connect to database", "Invalid configuration value".
	// It's important to monitor these logs as they often indicate a problem that may need attention soon.
	WarningLevel

	// ErrorLevel messages are for reporting failures in a way that allows the application to continue running.
	// For instance, "Could not find file", "Failed to parse JSON".
	// These types of errors should be fixed immediately but do not necessarily mean the program is failing.
	ErrorLevel

	// FatalLevel logs indicate that an unrecoverable error has occurred and the application cannot proceed
	// with its current operation. The application will typically exit after logging a fatal message,
	// possibly with a non-zero status code to indicate failure.
	// For example, "Failed to start server", "Database connection lost".
	// These types of errors are usually indicative of serious problems that require immediate attention.
	FatalLevel

	// WarnLevel is an alias of WarningLevel.
	WarnLevel = WarningLevel
)

type Logger

type Logger struct {
	Warn  func(...any)
	Warnf func(string, ...any)
	// contains filtered or unexported fields
}

Logger allows messages with different levels/priorities to be sent to stderr/stdout

func Default

func Default() *Logger

Default returns the default logger.

func New

func New() *Logger

New returns a new logger at the debug level and on stderr.

func (*Logger) Debug

func (r *Logger) Debug(v ...any)

Debug writes debug level messages

func (*Logger) Debugf

func (r *Logger) Debugf(format string, v ...any)

Debugf writes debug level messages using formatted string

func (*Logger) Error

func (r *Logger) Error(v ...any)

Error writes error level messages

func (*Logger) Errorf

func (r *Logger) Errorf(format string, v ...any)

Errorf writes error level messages using formatted string

func (*Logger) Fatal

func (r *Logger) Fatal(v ...any)

Fatal writes fatal level messages and terminates the application

func (*Logger) Fatalf

func (r *Logger) Fatalf(format string, v ...any)

Fatalf writes fatal level messages using formatted string and terminates the application

func (*Logger) Info

func (r *Logger) Info(v ...any)

Info writes info level messages

func (*Logger) Infof

func (r *Logger) Infof(format string, v ...any)

Infof writes info level messages using formatted string

func (*Logger) Output

func (r *Logger) Output(level Level, calldepth int, format *string, v ...any)

Output is used to log messages with a specific level and format.

func (*Logger) SetFormat

func (r *Logger) SetFormat(format Format) error

SetFormat sets the output format for the logger.

func (*Logger) SetLevel

func (r *Logger) SetLevel(level Level) error

SetLevel sets the current loglevel to the desired value.

func (*Logger) SetOutput

func (r *Logger) SetOutput(filename string) error

SetOutput opens or create the given file and set it as the new logging destination.

func (*Logger) Warning

func (r *Logger) Warning(v ...any)

Warning writes warning level messages

func (*Logger) Warningf

func (r *Logger) Warningf(format string, v ...any)

Warningf writes warning level messages using formatted string

Jump to

Keyboard shortcuts

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