logger

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 14 Imported by: 0

README

Logger pakcage

Help manage logger. This package does not implement a logger but user logrus as logger behind. This package will simplify call of logger and allow more features like *log.Logger wrapper.

Exmaple of implement

In your file, first add the import of golib/logger :

import . "github.com/nabbar/golib/logger"
// Check if the function call will return an error, and if so, will log a fatal (log and os.exit) message
FatalLevel.LogError(GetVersion().CheckGo("1.12", ">="))

This call, will disable color, trace,

FileTrace(false)
DisableColor()
EnableViperLog(true)

This call, return a go *log.Logger interface. This example can be found in the golib/httpserver package :

log := GetLogger(ErrorLevel, log.LstdFlags|log.Lmicroseconds, "[http/http2 server '%s']", host)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGID

func AddGID(enable bool)

AddGID Reconfigure the current logger to add or not the thread GID before each message.

func DisableColor

func DisableColor()

DisableColor Reconfigure the current logger to not use color in messages format. This apply only for next message and only for TextFormat.

func EnableColor

func EnableColor()

EnableColor Reconfigure the current logger to use color in messages format. This apply only for next message and only for TextFormat.

func EnableViperLog

func EnableViperLog(enable bool)

EnableViperLog enable or not the Gin Logger configuration.

func FileTrace

func FileTrace(enable bool)

FileTrace Reconfigure the current logger to add or not the origin file/line of each message. This option is apply for all message except info message.

func GetFormatListString

func GetFormatListString() []string

GetFormatListString return the full list (slice of string) of all available formats.

func GetIOWriter

func GetIOWriter(level Level, msgPrefixPattern string, msgPrefixArgs ...interface{}) io.Writer

GetIOWriter return a io.Writer instance to Write on logger with a specified log level.

level specify the log level to use to redirect all entry to current logger
msgPrefixPattern is a string pattern to prefix all entry
msgPrefixArgs is a list of args to apply on the msgPrefixPattern pattern to prefix all entry

func GetLevelListString

func GetLevelListString() []string

GetLevelListString return a list ([]string) of all string loglevel available.

func GetLogger

func GetLogger(lvl Level, logFlags int, msgPrefixPattern string, msgPrefixArgs ...interface{}) *log.Logger

GetLogger return a golang log.logger instance linked with this main logger. This function is useful to keep the format, mode, color, output... same as current config.

msgPrefixPattern a pattern prefix to identify or comment all message passed throw this log.logger instance
msgPrefixArgs a list of interface to apply on pattern with a fmt function

func SetFormat

func SetFormat(fmt Format)

SetFormat Change the format of all log entry with the Format type given in parameter. The change is apply for next entry only. If the given Format type is not matching a correct Format type, no change will be apply.

fmt a Format type for the format to use

func SetLevel

func SetLevel(level Level)

SetLevel Change the Level of all log entry with the Level type given in parameter. The change is apply for next log entry only. If the given Level type is not matching a correct Level type, no change will be apply.

level a Level type to use to specify the new level of logger message

func SetOutput

func SetOutput(out io.WriteCloser)

func SetStdLogger

func SetStdLogger(lvl Level, logFlags int, msgPrefixPattern string, msgPrefixArgs ...interface{})

GetLogger force the default golang log.logger instance linked with this main logger. This function is useful to keep the format, mode, color, output... same as current config.

msgPrefixPattern a pattern prefix to identify or comment all message passed throw this log.logger instance
msgPrefixArgs a list of interface to apply on pattern with a fmt function

func SetTracePathFilter added in v1.3.0

func SetTracePathFilter(path string)

SetTracePathFilter customize the filter apply to filepath on trace.

func Timestamp

func Timestamp(enable bool)

Timestamp Reconfigure the current logger to add or not the timestamp before each message.

Types

type Format

type Format uint8

Format a uint8 type customized with function to manage the result logger format.

const (

	// TextFormat a text format for logger entry.
	TextFormat Format
	// JsonFormat a json format for logger entry.
	JsonFormat
)

func GetCurrentFormat

func GetCurrentFormat() Format

GetCurrentFormat Return the current Format Type used for all log entry.

func GetFormatString

func GetFormatString(format string) Format

GetFormatString return a valid Format Type matching the given string parameter.

format the string representation of a Format type

func (Format) String

func (f Format) String() string

String Return the string name of the Format Type.

type IOWriter

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

IOWriter is struct redirected all entry to the current logger.

func (IOWriter) Write

func (iow IOWriter) Write(p []byte) (n int, err error)

Write implement the Write function of the io.Writer interface and redirect all entry to current logger. The return n will always return the len on the p parameter and err will always be nil.

p the entry to be redirect to current logger

type Level

type Level uint8

Level a uint8 type customized with function to log message with the current log level.

const (
	// PanicLevel Panic level for entry log, will result on a Panic() call (trace + fatal).
	PanicLevel Level = iota
	// FatalLevel Fatal level for entry log, will result on os.Exit with error.
	FatalLevel
	// ErrorLevel Error level for entry log who's meaning the caller stop his process and return to the pre caller.
	ErrorLevel
	// WarnLevel Warning level for entry log who's meaning the caller don't stop his process and try to continue it.
	WarnLevel
	// InfoLevel Info level for entry log who's meaning it is just an information who's have no impact on caller's process but can be useful to inform human of a state, event, success, ...
	InfoLevel
	// DebugLevel Debug level for entry log who's meaning the caller has no problem and the information is only useful to identify a potential problem who's can arrive later.
	DebugLevel
	// NilLevel Nil level will never log anything and is used to completely disable current log entry. It cannot be used in the SetLogLevel function.
	NilLevel
)

func GetCurrentLevel

func GetCurrentLevel() Level

GetCurrentLevel return the current loglevel setting in the logger. All log entry matching this level or below will be logged.

func GetLevelString

func GetLevelString(level string) Level

GetLevelString return a valid Level Type matching the given string parameter. If the given parameter don't represent a valid level, the InfoLevel will be return.

level the string representation of a Level type

func (Level) Log

func (level Level) Log(message string)

Log Simple function to log directly the given message with the attached log Level.

message a string message to be logged with the attached log Level

func (Level) LogData

func (level Level) LogData(message string, data interface{})

LogData Simple function to log directly the given message with given data with the attached log Level.

message a string message to be logged with the attached log Level
data an interface of data to be logged with the message. (In Text format, the data will be json marshaled)

func (Level) LogError

func (level Level) LogError(err error) bool

LogError Simple function to log directly the given error with the attached log Level.

How iot works :

  • when the err is a valid error, this function will : +--- log the Error with the attached log Level +--- return true

  • when the err is nil, this function will : +--- return false

    err an error object message to be logged with the attached log Level

func (Level) LogErrorCtx

func (level Level) LogErrorCtx(levelElse Level, context string, err error) bool

LogErrorCtx Function to test, log and inform about the given error object.

How iot works :

  • when the err is a valid error, this function will : +--- log the Error with the attached log Level +--- return true

  • when the err is nil, this function will : +--- use the levelElse if valid to inform with context there is no error found +--- return false

    levelElse level used if the err is nil before returning a False result context a string for the context of the current test of the error err a error object to be log with the attached log level before return true, if the err is nil, the levelElse is used to log there are no error and return false

func (Level) LogErrorCtxf

func (level Level) LogErrorCtxf(levelElse Level, contextPattern string, err error, args ...interface{}) bool

LogErrorCtxf Function to test, log and inform about the given error object, but with a context based on a pattern and matching args.

How iot works :

  • when the err is a valid error, this function will : +--- log the Error with the attached log Level +--- return true

  • when the err is nil, this function will : +--- use the levelElse if valid to inform with context there is no error found +--- return false

    levelElse level used if the err is nil before returning a False result contextPattern a pattern string for the context of the current test of the error. This string will be used in a fmt function as pattern string err a error object to be log with the attached log level before return true, if the err is nil, the levelElse is used to log there are no error and return false args a list of interface for the context of the current test of the error. This list of interface will be used in a fmt function as the matching args for the pattern string

func (Level) LogGinErrorCtx

func (level Level) LogGinErrorCtx(levelElse Level, context string, err error, c *gin.Context) bool

LogGinErrorCtx Function to test, log and inform about the given error object. This function will also add an Gin Tonic Error if the c parameters is a valid GinTonic Context reference.

How iot works :

  • when the err is a valid error, this function will : +--- log the Error with the attached log Level +--- if the Context Gin Tonic is valid, add the Error into this context +--- return true
  • when the err is nil, this function will : +--- use the levelElse if valid to inform with context there is no error found +--- return false

levelElse level used if the err is nil before returning a False result context a string for the context of the current test of the error err a error object to be log with the attached log level before return true, if the err is nil, the levelElse is used to log there are no error and return false c a valid Go GinTonic Context reference to add current error to the Gin Tonic Error Context.

func (Level) LogGinErrorCtxf

func (level Level) LogGinErrorCtxf(levelElse Level, contextPattern string, err error, c *gin.Context, args ...interface{}) bool

LogGinErrorCtxf Function to test, log and inform about the given error object, but with a context based on a couple of pattern and matching args. This function will also add an Gin Tonic Error if the c parameters is a valid GinTonic Context reference.

How iot works :

  • when the err is a valid error, this function will : +--- log the Error with the attached log Level +--- if the Context Gin Tonic is valid, add the Error into this context +--- return true

  • when the err is nil, this function will : +--- use the levelElse if valid to inform with context there is no error found +--- return false

    levelElse level used if the err is nil before returning a False result contextPattern a pattern string for the context of the current test of the error. This string will be used in a fmt function as pattern string err a error object to be log with the attached log level before return true, if the err is nil, the levelElse is used to log there are no error and return false c a valid Go GinTonic Context reference to add current error to the Gin Tonic Error Context args a list of interface for the context of the current test of the error. This list of interface will be used in a fmt function as the matching args for the pattern string

func (Level) Logf

func (level Level) Logf(format string, args ...interface{})

Logf Simple function to log (to the attached log Level) with a fmt function a given pattern and arguments in parameters.

format a string pattern for fmt function
args a list of interface to match the references in the pattern

func (Level) String

func (level Level) String() string

String Convert the current Level type to a string. E.g. PanicLevel becomes "Critical Error".

func (Level) Uint8

func (level Level) Uint8() uint8

Uint8 Convert the current Level type to a uint8 value. E.g. FatalLevel becomes 1.

func (Level) WithFields

func (level Level) WithFields(message string, fields map[string]interface{})

WithFields Simple function to log directly the given message with given fields with the attached log Level.

message a string message to be logged with the attached log Level
fields a map of string key and interfaces value for a complete list of field ("field name" => value interface)

Jump to

Keyboard shortcuts

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