log4go

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 11 Imported by: 0

README

Log4Go


Table of Contents


Terminology


Level - a log level is the severity or priority of a log event (debug, info, etc). Whether an appender will see the event or not is determined by the category’s level. If this is less than or equal to the event’s level, it will be sent to the category’s appender(s).

Category - a label for grouping log events. This can be based on module (e.g. ‘auth’, ‘payment’, ‘http’), or anything you like. Log events with the same category will go to the same appenders. The category for log events is defined when you get a Logger from Log4Go (Log4Go.GetLogger('some-category')).

Appender - appenders are responsible for output of log events. They may write events to files, send emails, store them in a database, or anything. Most appenders use layouts to serialise the events to strings for output.

Logger - this is your code’s main interface with Log4Go. A logger instance may have an optional category, defined when you create the instance. Loggers provide the info, debug, error, ... functions that create LogEvents and pass them on to appenders.

Layout - a function for converting a LogEvent into a string representation. Log4Go comes with a few different implementations: basic, coloured, and a more configurable pattern based layout.

LogEvent - a log event has a timestamp, a level, and optional category, data, and context properties. When you call logger.Info('cheese value:', edam) the logger will create a log event with the timestamp of now, a level of INFO, a category that was chosen when the logger was created, and a data array with two values (the string ‘cheese value:’, and the object ‘edam’), along with any context data that was added to the logger.

API

Configuration


configuration - Log4Go.Configure(map[string]any | string)

This method allows you to configure the logger. You can pass a JSON (such as map[string]any), or, if you prefer, you can pass a string as the path to a json file

Configuration Object

Properties:

  • appenders (map[string]any) - a map of named appenders (string) to appender definitions (object); appender definitions must have a property type (string) - other properties depend on the appender type.
  • categories (map[string]any) - a map of named categories (string) to category definitions (object). You must define the default category which is used for all log events that do not match a specific category. Category definitions have two properties:
    • appenders (array of strings) - the list of appender names to be used for this category. A category must have at least one appender.
    • level (string, case insensitive) - the minimum log level that this category will send to the appenders. For example, if set to ‘error’ then the appenders will only receive log events of level ‘error’, ‘fatal’, ‘mark’ - log events of ‘info’, ‘warn’, ‘debug’, or ‘trace’ will be ignored.
configured - Log4Go.IsConfigured() bool

IsConfigured method call returns a boolean on whether Log4Go.Configure() was successfully called previously. Implicit Log4Go.Configure() call by Log4Go.GetLogger() is will also affect this value.

Loggers - Log4Go.GetLogger(category-name string) Logger

To support the minimalist usage, this function will implicitly call Log4Go.Configure() with the default configurations if it hasn’t been configured before.

@Type Logger
package pkg

type ILog4GoLogger interface {
	AddContext(key string, value any)
	ChangeOneContext(key string, value any)
	ChangeManyContext(c map[string]any)
	RemoveContext(key string)
	ClearContext()
	Trace(args ...any)
	Debug(args ...any)
	Info(args ...any)
	Warn(args ...any)
	Error(args ...any)
	Fatal(args ...any)
	Terminate()
}

Appenders


Type of Appenders
  • dateFile
  • file
  • fileSync
  • stderr
  • stdout

Categories


Layouts


Type of Layouts
  • basic
  • coloured
  • messagePassThrough
  • pattern

Documentation

Overview

Package log4go

This is a golang Logger inspired by log4js

Index

Constants

This section is empty.

Variables

View Source
var ProcessPid = os.Getpid()

Functions

func AddAppender

func AddAppender(key string, f *func(config *pkg.AppenderConfig) pkg.Appender)

AddAppender Use this function to making new custom appenders. You can't use a default appender name like dateFile, file and so on. If you call this function twice with the same key, the last function provided will be retained

func AddLayout

func AddLayout(key string, f *func(*pkg.LayoutConfig) pkg.Layout)

AddLayout Use this function to creating a new custom layouts. If you use a predefined key (e.g. basic, pattern and others), Log4Go will not use the custom layout. If you call this function twice, with the same key but different functions, Log4Go will use the last provided definition

func Configure

func Configure(c any) error

Configure This function allow you to configure your logger.

func IsConfigured

func IsConfigured() bool

IsConfigured Is your Log4Go configured?

func Shutdown

func Shutdown()

Shutdown This function must be pushed at the end of your main function. With this function you will be sure that all your logs were write on their files

Types

type Logger

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

func GetLogger

func GetLogger(names ...string) *Logger

GetLogger this function returns a Log4GoLogger. its get an array of strings, but it used only the first if you don't pass anything to this method, it returns a Log4GoLogger with name "defaults"

func NewLogger

func NewLogger(c any) *Logger

func (*Logger) AddContext

func (l *Logger) AddContext(key string, value any)

func (*Logger) ChangeManyContext

func (l *Logger) ChangeManyContext(c map[string]any)

func (*Logger) ChangeOneContext

func (l *Logger) ChangeOneContext(key string, value any)

func (*Logger) ClearContext

func (l *Logger) ClearContext()

func (*Logger) Debug

func (l *Logger) Debug(args ...any)

func (*Logger) DebugWithContext added in v1.0.4

func (l *Logger) DebugWithContext(c map[string]any, args ...any)

func (*Logger) Error

func (l *Logger) Error(args ...any)

func (*Logger) ErrorWithContext added in v1.0.4

func (l *Logger) ErrorWithContext(c map[string]any, args ...any)

func (*Logger) Fatal

func (l *Logger) Fatal(args ...any)

func (*Logger) FatalWithContext added in v1.0.4

func (l *Logger) FatalWithContext(c map[string]any, args ...any)

func (*Logger) Info

func (l *Logger) Info(args ...any)

func (*Logger) InfoWithContext added in v1.0.4

func (l *Logger) InfoWithContext(c map[string]any, args ...any)

func (*Logger) RemoveContext

func (l *Logger) RemoveContext(key string)

func (*Logger) Terminate

func (l *Logger) Terminate()

func (*Logger) Trace

func (l *Logger) Trace(args ...any)

func (*Logger) TraceWithContext added in v1.0.4

func (l *Logger) TraceWithContext(c map[string]any, args ...any)

func (*Logger) Warn

func (l *Logger) Warn(args ...any)

func (*Logger) WarnWithContext added in v1.0.4

func (l *Logger) WarnWithContext(c map[string]any, args ...any)

Jump to

Keyboard shortcuts

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