Categorygithub.com/cmstar/go-logx
modulepackage
1.4.0
Repository: https://github.com/cmstar/go-logx.git
Documentation: pkg.go.dev

# README

logx - Simple logging abstraction

GoDoc Go codecov License GoVersion Go Report Card

There are so many logging frameworks, it's not necessary to write a new one.

This package only provides some logging abstraction. The purpose is to be used as a middleware between business codes and other logging frameworks.

Logger

The Logger interface has two methods:

  • Log(): writes a log message.
  • LogFn(): similar to Log, but accepts a factory function to produce the log message.

There are two built-in loggers:

  • NopLogger: an empty logger that logs nothing.
  • StdLogger: writes logs to the standard output.

For more details, see the GoDoc.

LoggerOp

The LoggerOp struct provides a group of shortcut methods to simplify the usage of Logger, such as Debug(), Infof(), Warnkv().

For more details, see the GoDoc.

LogManager

LogManager is used for managing Loggers. It's safe for concurrent use.

Methods:

  • Set(name, Logger): register a logger with the given name into the LogManager instance.
  • Find(name): get a logger with the given name.
  • Delete(name): deleted a registered logger from the instance.
  • Op(): similar to Find(), but returns a LoggerOp instance.

LogManager uses case-insensitive header matching when finding Loggers. A name will be split by the dot(.) into several segments, when finding a name like 'A.B.C.D', LogManager finds the Logger in this order, returns the first found Logger:

  • a.b.c.d
  • a.b.c
  • a.b
  • a
  • "" (empty string)

If no logger can be found, LogManger.Find() returns nil.

The logger with the empty name is the root logger. The empty string can be treated as the first segment of all other names, e.g. The name 'a.b' is equivalent to '.a.b'.

It's similar to the LoaManager class in log4j from Java/Common.Logging from .net

For more details, see the Example.

# Packages

Package logxtest provides some types for testing the logx package.

# Functions

FilterLevel wraps the given Logger, returns a new Logger which can filter log messages by the Level mask.
LevelToString returns the string representation of Level.
NewManager creates a new instance of LogManager.
NewSingleLoggerLogFinder creates a new instance of LogFinder, whose Find() will always return the same Logger.
NewStdLogger creates a new StdLogger with the given underlyingLogger, which is used to receive log messages.
Op returns a LoggerOp which wraps the given Logger.
ParseLevel parses the given string to the corresponding Level.

# Constants

LevelBeyondError combines LevelFatal, LevelError, LevelWarn, LevelInfo, LevelDebug.
LevelBeyondError combines LevelFatal, LevelError.
LevelBeyondError combines LevelFatal, LevelError, LevelWarn, LevelInfo.
LevelBeyondError combines LevelFatal, LevelError, LevelWarn.
LevelDebug is the debug level.
LevelError is the error level.
LevelFatal is the fatal level.
LevelInfo is the info level.
LevelWarn is the warn level.

# Variables

DefaultManager is the globally shared LogManager.
NopLogger is a no-op logger, the Log function do nothing and returns no error.

# Structs

LoggerOp wraps a Logger interface, provides a group of shortcut operations to call the methods of the Logger.
LogManager is a simple implementation of LogFinder, which can be used to manage Loggers.
StdLogger sends all log messages to the UnderlyingLogger which is a Logger of the standard library.

# Interfaces

LogFinder defines methods for finding named loggers.
Logger defines the logging action.

# Type aliases

Level defines the log level.