Documentation
¶
Index ¶
- Constants
- func BasicFormatter(ts time.Time, lvl Level, src, msg string) string
- func JsonFormatter(ts time.Time, lvl Level, src, msg string) string
- func RawFormatter(ts time.Time, lvl Level, src, msg string) string
- func StdFormatter(ts time.Time, lvl Level, src, msg string) string
- type FileHandler
- type Formatter
- type Handler
- type Level
- type Logger
- func (l *Logger) AddHandler(h Handler)
- func (l *Logger) ChildLogger(name string) *Logger
- func (l *Logger) ClearHandlers()
- func (l *Logger) Debug(msg string, args ...any) error
- func (l *Logger) Error(msg string, args ...any) error
- func (l *Logger) Fatal(msg string, args ...any) error
- func (l *Logger) Info(msg string, args ...any) error
- func (l *Logger) Log(lvl Level, msg string) error
- func (l *Logger) Name() string
- func (l *Logger) Panic(msg string, args ...any) error
- func (l *Logger) SetFormatter(f Formatter)
- func (l *Logger) SetHandler(h Handler)
- func (l *Logger) SubLogger(name string) *Logger
- func (l *Logger) Trace(msg string, args ...any) error
- func (l *Logger) Warn(msg string, args ...any) error
- type StdoutHandler
Examples ¶
Constants ¶
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 )
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 ¶
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 ¶
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 ¶
RawFormatter generates a minimal formatted text log message. Format: {time} {message}
Example:
2006-01-02 15:04:05.000000 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 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 ¶
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 ¶
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 ¶
AddHandler adds a new handler to the logger.
func (*Logger) ChildLogger ¶
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) SetFormatter ¶
SetFormatter sets a new formatter for the logger.
func (*Logger) SetHandler ¶
SetHandler clears all handler and set new one to the logger.
func (*Logger) SubLogger ¶
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
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.