logging

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: BSD-3-Clause Imports: 6 Imported by: 4

README


This package provides generic logger implementation.

Documentation

Index

Examples

Constants

View Source
const (
	// Defined log levels.
	TRACE = Level(-2) // Trace level
	DEBUG = Level(-1) // Debug level
	INFO  = Level(0)  // Info level
	WARN  = Level(1)  // Warning level
	ERROR = Level(2)  // Error level
	FATAL = Level(3)  // Fatal level
	PANIC = Level(4)  // Panic level
)
View Source
const (
	// standard time format for log messages
	STD_TIME_FORMAT = "2006-01-02 15:04:05.000000"
)

Variables

This section is empty.

Functions

func BasicFormatter

func BasicFormatter(ts time.Time, lvl Level, src, msg string) string

BasicFormatter generates a basic formatted text log message. Format: {time} {level} {message}

Example:

2006-01-02 15:04:05.000000 INFO log message

func JsonFormatter

func JsonFormatter(ts time.Time, lvl Level, src, msg string) string

JsonFormatter generates a JSON formatted text log message.

Example:

{"time": "2006-01-02 15:04:05.000000", "level": "INFO", "source": "logger_name", "message": "log message"}

func RawFormatter

func RawFormatter(ts time.Time, lvl Level, src, msg string) string

RawFormatter generates a minimal formatted text log message. Format: {time} {message}

Example:

2006-01-02 15:04:05.000000 log message

func StdFormatter

func StdFormatter(ts time.Time, lvl Level, src, msg string) string

StdFormatter generates a standard text formatted log message. Format: {time} {level} [{source}] {message}

Example:

2006-01-02 15:04:05.000000 INFO [logger_name] log message

Types

type FileHandler

type FileHandler struct {
	FilePath string // Path to the log file
	// contains filtered or unexported fields
}

FileHandler writes log messages to a specified file.

func NewFileHandler

func NewFileHandler(path string) *FileHandler

NewFileHandler creates a new FileHandler for the specified path.

func (*FileHandler) HandleMessage

func (h *FileHandler) HandleMessage(msg string) error

HandleMessage writes the log message to the specified file.

type Formatter

type Formatter func(ts time.Time, lvl Level, src, msg string) string

Formatter defines function for formatting log record into messages.

type Handler

type Handler interface {
	// HandleMessage process a log message.
	HandleMessage(msg string) error
}

Handler interface for processing log messages.

type Level added in v0.4.2

type Level int8

func (Level) String added in v0.4.2

func (l Level) String() string

returns the string representation of the log level.

type Logger

type Logger struct {
	Level  Level  // Logger level
	Prefix string // an optional prefix for all logger records
	Suffix string // an optional suffix for all logger records
	// contains filtered or unexported fields
}

A Logger records structured information about each call to its methods. For each call, it creates a new log record and passes it to the logger handlers and to its parent logger.

func NewFileLogger

func NewFileLogger(name, path string) *Logger

NewFileLogger creates a new logger that logs to a specified file.

Example
package main

import (
	"os"
	"path/filepath"

	"github.com/exonlabs/go-utils/pkg/logging"
)

func main() {
	log_path := filepath.Join(os.TempDir(), "foo.log")

	logger := logging.NewFileLogger("main", log_path)
	logger.Level = logging.DEBUG

	logger.Warn("logging message type: warn")
	logger.Info("logging message type: info")
	logger.Debug("logging message type: debug")
}
Output:

func NewStdoutLogger

func NewStdoutLogger(name string) *Logger

NewStdoutLogger creates a new logger that outputs to standard output.

Example
package main

import (
	"github.com/exonlabs/go-utils/pkg/logging"
)

func main() {
	logger := logging.NewStdoutLogger("main")
	logger.Level = logging.DEBUG

	logger.Warn("logging message type: warn")
	logger.Info("logging message type: info")
	logger.Debug("logging message type: debug")
}
Output:

func (*Logger) AddHandler

func (l *Logger) AddHandler(h Handler)

AddHandler adds a new handler to the logger.

func (*Logger) ChildLogger

func (l *Logger) ChildLogger(name string) *Logger

ChildLogger creates new named child logger. child logger inherits the parent logger Formatter.

Example:

2006-01-02 15:04:05.000000 INFO [child_name] log message

func (*Logger) ClearHandlers

func (l *Logger) ClearHandlers()

ClearHandlers removes all handlers from the logger.

func (*Logger) Debug

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

Debug logs a record with Debug level.

func (*Logger) Error

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

Error logs a record with Error level.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...any) error

Fatal logs a record with Fatal level.

func (*Logger) Info

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

Info logs a record with Info level.

func (*Logger) Log

func (l *Logger) Log(lvl Level, msg string) error

Log handles a log message, sending it to all handlers and parents.

func (*Logger) Name

func (l *Logger) Name() string

Name returns the logger name.

func (*Logger) Panic

func (l *Logger) Panic(msg string, args ...any) error

Panic logs a record with Panic level.

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(f Formatter)

SetFormatter sets a new formatter for the logger.

func (*Logger) SetHandler

func (l *Logger) SetHandler(h Handler)

SetHandler clears all handler and set new one to the logger.

func (*Logger) SubLogger

func (l *Logger) SubLogger(name string) *Logger

SubLogger creates a new child logger with name added between brackets before prefix. child sub logger inherits the parent logger Formatter.

Example:

2006-01-02 15:04:05.000000 INFO [parent_name] (child_name) log message

func (*Logger) Trace

func (l *Logger) Trace(msg string, args ...any) error

Trace logs a record with Trace level.

func (*Logger) Warn

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

Warn logs a record with Warn level.

type StdoutHandler

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

StdoutHandler writes log messages to standard output.

func NewStdoutHandler

func NewStdoutHandler() *StdoutHandler

NewStdoutHandler creates a new instance of StdoutHandler.

func (*StdoutHandler) HandleMessage

func (h *StdoutHandler) HandleMessage(msg string) error

HandleMessage writes a log message to standard output.

Jump to

Keyboard shortcuts

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