logger

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package logger provides a simple logging framework that supports different log levels (DEBUG, INFO, WARN, ERROR) and color-coded output.

The logger is configurable to print messages at a specified level or higher, and it allows for custom output writers. Each log level has a corresponding color to make messages easier to distinguish in the output.

Example usage:

// Create a new logger that logs to stdout and has an INFO level.
l := logger.New(logger.INFO)

// Log messages at various levels.
l.Debug("This is a debug message")  // Won't be shown since log level is INFO.
l.Info("This is an info message")   // Will be shown.
l.Warn("This is a warning message") // Will be shown.
l.Error("This is an error message") // Will be shown.

You can also create a custom logger with a different output:

// Create a logger that writes to a file and logs at the DEBUG level.
file, _ := os.Create("app.log")
l := logger.NewCustom(logger.DEBUG, file)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Level

type Level string

Level represents the severity of a log message. It can be one of DEBUG, INFO, WARN, or ERROR.

const (
	SILENT Level = "silent" // SILENT represents no logging, effectively muting all log messages.
	DEBUG  Level = "debug"  // DEBUG represents debug-level messages, useful for development and troubleshooting.
	INFO   Level = "info"   // INFO represents informational messages, typically used for normal operation.
	WARN   Level = "warn"   // WARN represents warning messages, which indicate potential issues but not failures.
	ERROR  Level = "error"  // ERROR represents error messages, indicating failure in operation.
	ALWAYS Level = "always" // ALWAYS represents messages that should always be logged, regardless of the current log level.
)

func (Level) AsInt

func (l Level) AsInt() int

func (Level) IsAllowed

func (l Level) IsAllowed() bool

IsAllowed checks if the log Level is a valid value (DEBUG, INFO, WARN, ERROR).

type Logger

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

Logger holds the configuration for logging. It includes the current logging level, the output writer, and color mappings for each log level.

func New

func New(level Level) *Logger

New creates a new Logger instance with the specified log level and writes to stdout. If an invalid log level is provided, it defaults to INFO.

func NewCustom

func NewCustom(level Level, output io.Writer) *Logger

NewCustom creates a new Logger instance with the specified log level and output writer. If an invalid log level is provided, it defaults to INFO.

func (*Logger) Always

func (l *Logger) Always(format string, args ...any)

Always logs a message at the INFO level, regardless of the current log level.

func (*Logger) Debug

func (l *Logger) Debug(format string, args ...any)

Debug logs a debug-level message if the current log level allows it.

func (*Logger) Error

func (l *Logger) Error(format string, args ...any)

Error logs an error message if the current log level allows it.

func (*Logger) Info

func (l *Logger) Info(format string, args ...any)

Info logs an informational message if the current log level allows it.

func (*Logger) Warn

func (l *Logger) Warn(format string, args ...any)

Warn logs a warning message if the current log level allows it.

Jump to

Keyboard shortcuts

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