Categorygithub.com/dihedron/go-log
modulepackage
0.0.0-20200302195143-d77e77a2cc39
Repository: https://github.com/dihedron/go-log.git
Documentation: pkg.go.dev

# README

go-log - Yet another logger wrapper for golang

A simple logger for Go; it provides log messages colorising, automatic addition or source file, line number and/or calling function to log messages. All top level messages are synchronised, so it is safe to reconfigure the logger from different goroutines.

Usage

To import the logger, simply add it as follows:

import (
	"github.com/dihedron/go-log"
)

All methods are available under the log namespace. To configure the logger, use a combination of the following methods:

	log.SetLevel(log.DBG)
	log.SetStream(os.Stdout, true)
	log.SetTimeFormat("15:04:05.000")
	log.SetPrintCallerInfo(true)
	log.SetPrintSourceInfo(log.SourceInfoShort)

where log.SetLevel() sets the current logging level to one of log.DBG (debugging messages or higher), log.INF (informational messages or more severe), log.WRN (warning messages or more severe), log.ERR (error messages only) or log.NUL (no messages at all).

log.SetStream() sets the io.Writer to which messages will be output; it can be os.Stdout or os.Stderr, or it can be a file on disk or a socket. The second boolean parameter specifies whether messages should be colorised according to their severity; this really only applies to console output.

log.SetTimeFormat() sets the format for timestamps; the suggested format provides timestamping to the milliseconds.

log.SetPrintCallerInfo() instructs the logger to write the name of the calling method before the message; the name is retrieved at runtime by walking the stack, so it is quite cumbersome and can result in a significant slowdown.

log.SetPrintSourceInfo() instructs the logger to print the name of the file (log.SourceInfoShort) or the full path (log.SourceInfoLong) and the line number of the call site. Also this information is retrieved at runtime by walking the stack and can be quite cumbersome: use sparingly!

To actually log messages, you can use two families of functions which follow the fmt.Printf and fmt.Println usage patterns, e.g.:

log.Errorf("this is an error message: %v", err)

log.Infoln("this is an informational message")

License

The code is released under an MIT License. All contributions are welcome provided they don't decrease the coverage of unit tests and are in line with the style of the rest of the library.

# Functions

Debugf writes a debug message to the current output stream, appending a new line.
Debugln writes a debug message to the current output stream, appending a new line.
Errorf writes an error message to the current output stream, appending a new line.
Errorln writes an error message to the current output stream, appending a new line.
Fatalf writes an error message to the current output stream, appending a new line.
Fatalln writes an error message to the current output stream, appending a new line.
GetLevel retur s the current log level.
GetPrintCallerInfo returns whether the automatic addition of the calling function (with package) to the log messages is enabled.
GetPrintSourceInfo returns whether the automatic addition of the source and line number info to the log messages is enabled, and whether the file name will be printed in short or long form.
GetStream returns the current log stream.
GetTimeFormat returns the current format of log messages time.
Infof writes an informational message to the current output stream, appending a new line.
Infoln writes an informational message to the current output stream, appending a new line.
IsDebug returns whether the debug (DebugLevel) log elevel is enabled.
IsDisabled returns whether the log is disabled.
IsError returns whether the error (ErrorLevel) log elevel is enabled.
IsFatal returns whether the fatal (FatalLevel) log elevel is enabled.
IsInfo returns whether the informational (InfoLevel) log elevel is enabled.
IsPanic returns whether the panic (PanicLevel) log elevel is enabled.
IsTrace returns whether the trace (TraceLevel) log elevel is enabled.
IsWarning returns whether the warning (WarnLevel) log elevel is enabled.
LevelFromString returns a log Level value by parsing the user-provided string in a lenient way; if the parsing fails, returns and error.
Panicf writes an error message to the current output stream, appending a new line; then it panics.
Panicln writes an error message to the current output stream, appending a new line; then it panics.
Printf is a raw version of the debug functions; it tries to interpret the message by checking if it starts with anything like "[D]" or "[W]"; if so, it delegates to the corresponding logging function, otherwise it just prints to the log stream as is, with no additional formatting.
Println is a raw version of the debug functions; it tries to interpret the message by checking if it starts with anthing like "[D]" or "[W]"; if so, it delegates to the corresponding logging function, otherwise it just prints to the log stream as is, with no additional formatting.
SetLevel sets the log level for the application.
SetPrintCallerInfo enables or disables the automatic addition of the calling function (with package) to the log messages.
SetPrintSourceInfo enables or disables the automatic addition of the source and line number info to the log messages; use one among SourceFileNone, SourceFileShort and SourceFileLong here.
SetStream sets the stream to write messages to; if the colorise flag is set, the logger will wrap the stream so it always produces properly coloured output messages; this might be less appropriate when writing to a file.
SetTimeFormat sets the format for log messages time.
ToJSON converts an object into pretty-printed JSON format.
Tracef writes a trace message to the current output stream, appending a new line.
Traceln writes a trace message to the current output stream, appending a new line.
Warnf writes a warning message to the current output stream, appending a new line.
Warnln writes a warning message to the current output stream, appending a new line.

# Constants

DebugLevel is the LogLevel for debug messages.
ErrorLevel is the LogLevel for error messages.
FatalLevel is the LogLevel for fatal error messages.
FlagFunctionInfo specifies whether the log message should automatically include the name of the containing function (note: this feature can be computationally expensive since it uses reflection at runtime).
FlagSourceInfo specifies whether the log message should automatically include the source location (note: this feature can be computationally expensive since it uses reflection at runtime).
InfoLevel is the LogLevel for informational messages.
NoneLevel is the LogLevel corresponding to no log output.
PanicLevel is the LogLevel for fatal error messages that cause a panic.
SourceInfoLong is the constants that specifies that the source file information should be printed in log form (complete file path).
SourceInfoNone is the constant that specifies that no source file information (file and line) should be printed out.
SourceInfoShort is the constants that specifies that the source file information should be printed in short form (file name only).
TraceLevel is the LogLevel for trace messages.
WarnLevel is the LogLevel for warning messages.

# Type aliases

Flag is used to influence some aspects of the logger's behaviour such as automatically including runtime information (source file, caller function).
LogLevel represents the log level.