logger

package
v0.0.0-...-28b05a4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2024 License: GPL-2.0 Imports: 5 Imported by: 0

README

Package Logger

This is the package for logging (based on zap).

Usage

// Initialise a logger first
logger.InitLogger()
// Use sugared logger
logger.Infof("Message: %s", msg)
// Don't forget to sync your logs when you log to file
defer logger.SyncAll()

Configuration

log:
  # development mode is more human-friendly: console encoding and not log to file
  development: true
  # THE FOLLOWING CONFIGS ONLY WORKS IN THE PRODUCTION MODE (development: false)
  # console or json
  encoding: console
  info:
    # whether log to stdout or not
    toStdout: true
    # whether log to file or not
    toFile: true
    # path and name of the INFO-level log file
    fileName: logs/core-info.log
    # maximum size of a single INFO-level log file (megabytes)
    maxSize: 10
    # how many days old INFO-level log files to retain
    maxAge: 7
    # how much old INFO-level log files to retain
    maxBackups: 10
  error:
    # whether log to stderr or not
    toStderr: true
    # whether log to file or not
    toFile: true
    # path and name of the ERROR-level log file
    fileName: logs/core-error.log
    # maximum size of a single ERROR-level log file (megabytes)
    maxSize: 10
    # how many days old ERROR-level log files to retain
    maxAge: 7
    # how much old ERROR-log files to retain
    maxBackups: 10

Supported log levels

From Log levels in Zap:

  • DEBUG (-1):
    • Usage: logger.Debug() or zap.L().Debug().
    • For recording messages useful for debugging.
  • INFO (0):
    • Usage: logger.Info() or zap.L().Info().
    • For messages describing normal application operations.
  • WARN (1):
    • Usage: logger.Warn() or zap.L().Warn().
    • For recording messages indicating something unusual happened that may need attention before it escalates to a more severe issue.
  • ERROR (2):
    • Usage: logger.Error() or zap.L().Error()
    • For recording unexpected error conditions in the program.
  • DPANIC (3):
    • Usage: logger.DPanic() or zap.L().DPanic().
    • For recording severe error conditions in development. It behaves like PANIC in development and ERROR in production.
  • PANIC (4):
    • Usage: logger.Panic() or zap.L().Panic().
    • Calls panic() after logging an error condition.
  • FATAL (5):
    • Usage: logger.Fatal() or zap.L().Fatal().
    • Calls os.Exit(1) after logging an error condition.

Sugared logger

Sugared logger is more human-friendly and have a syntax similar to fmt.Print() and fmt.Printf().

// Use sugared logger
logger.Info("This is sugared logger!!!")
logger.Infof("Message: %s", msg)
logger.Errorf("Failed to do bar: %s", err.Error())

Supported formats:

  • Print()-like (e.g., Info())
  • Printf()-like (e.g., Infof())
  • Println()-like (e.g., Infoln())
  • Non-sugared logger style (use key-value pairs, e.g., Infow())

Non-sugared logger

Non-sugared logger has a better performance and is more suitable for performance sensitive scenario.

// Use non-sugared logger
zap.L().Info("This is message", zap.String("key", "value"))
zap.L().Error("Failed to do foo", zap.String("error", err.Error()))

Log format

The following two formats are supported:

  • json (default): Default format that is better for parsing and processing. Example: {"level":"info","timestamp":"2024-03-15T12:29:34.657+1100","msg":"This is test logger: INFO level"}
  • console: A more human-friendly format. Example: 2024-03-15T12:30:13.390+1100 INFO This is test logger: INFO level

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Debug    func(...interface{})
	Debugf   func(string, ...interface{})
	Debugw   func(string, ...interface{})
	Debugln  func(...interface{})
	Info     func(...interface{})
	Infof    func(string, ...interface{})
	Infow    func(string, ...interface{})
	Infoln   func(...interface{})
	Warn     func(...interface{})
	Warnf    func(string, ...interface{})
	Warnw    func(string, ...interface{})
	Warnln   func(...interface{})
	Error    func(...interface{})
	Errorf   func(string, ...interface{})
	Errorw   func(string, ...interface{})
	Errorln  func(...interface{})
	DPanic   func(...interface{})
	DPanicf  func(string, ...interface{})
	DPanicw  func(string, ...interface{})
	DPanicln func(...interface{})
	Panic    func(...interface{})
	Panicf   func(string, ...interface{})
	Panicw   func(string, ...interface{})
	Panicln  func(...interface{})
	Fatal    func(...interface{})
	Fatalf   func(string, ...interface{})
	Fatalw   func(string, ...interface{})
	Fatalln  func(...interface{})
)

Functions

func InitLogger

func InitLogger()

InitLogger initialise a logger and assign it to the global variables

func SyncAll

func SyncAll() error

SyncAll sync all the files added to the logger

Types

This section is empty.

Jump to

Keyboard shortcuts

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