log

package module
v0.0.0-...-d77e77a Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2020 License: MIT Imports: 11 Imported by: 2

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.

Documentation

Index

Constants

View Source
const (
	// 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).
	FlagSourceInfo = 1 << iota
	// 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).
	FlagFunctionInfo
)
View Source
const (
	// SourceInfoNone is the constant that specifies that no source file information
	// (file and line) should be printed out.
	SourceInfoNone int8 = iota
	// SourceInfoShort is the constants that specifies that the source file
	// information should be printed in short form (file name only).
	SourceInfoShort
	// SourceInfoLong is the constants that specifies that the source file
	// information should be printed in log form (complete file path).
	SourceInfoLong
)

Variables

This section is empty.

Functions

func Debugf

func Debugf(format string, args ...interface{}) (int, error)

Debugf writes a debug message to the current output stream, appending a new line.

func Debugln

func Debugln(args ...interface{}) (int, error)

Debugln writes a debug message to the current output stream, appending a new line.

func Errorf

func Errorf(format string, args ...interface{}) (int, error)

Errorf writes an error message to the current output stream, appending a new line.

func Errorln

func Errorln(args ...interface{}) (int, error)

Errorln writes an error message to the current output stream, appending a new line.

func Fatalf

func Fatalf(format string, args ...interface{}) (int, error)

Fatalf writes an error message to the current output stream, appending a new line.

func Fatalln

func Fatalln(args ...interface{}) (int, error)

Fatalln writes an error message to the current output stream, appending a new line.

func GetPrintCallerInfo

func GetPrintCallerInfo() bool

GetPrintCallerInfo returns whether the automatic addition of the calling function (with package) to the log messages is enabled.

func GetPrintSourceInfo

func GetPrintSourceInfo() int8

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.

func GetStream

func GetStream() io.Writer

GetStream returns the current log stream.

func GetTimeFormat

func GetTimeFormat() string

GetTimeFormat returns the current format of log messages time.

func Infof

func Infof(format string, args ...interface{}) (int, error)

Infof writes an informational message to the current output stream, appending a new line.

func Infoln

func Infoln(args ...interface{}) (int, error)

Infoln writes an informational message to the current output stream, appending a new line.

func IsDebug

func IsDebug() bool

IsDebug returns whether the debug (DebugLevel) log elevel is enabled.

func IsDisabled

func IsDisabled() bool

IsDisabled returns whether the log is disabled.

func IsError

func IsError() bool

IsError returns whether the error (ErrorLevel) log elevel is enabled.

func IsFatal

func IsFatal() bool

IsFatal returns whether the fatal (FatalLevel) log elevel is enabled.

func IsInfo

func IsInfo() bool

IsInfo returns whether the informational (InfoLevel) log elevel is enabled.

func IsPanic

func IsPanic() bool

IsPanic returns whether the panic (PanicLevel) log elevel is enabled.

func IsTrace

func IsTrace() bool

IsTrace returns whether the trace (TraceLevel) log elevel is enabled.

func IsWarning

func IsWarning() bool

IsWarning returns whether the warning (WarnLevel) log elevel is enabled.

func Panicf

func Panicf(format string, args ...interface{}) (int, error)

Panicf writes an error message to the current output stream, appending a new line; then it panics.

func Panicln

func Panicln(args ...interface{}) (int, error)

Panicln writes an error message to the current output stream, appending a new line; then it panics.

func Printf

func Printf(format string, args ...interface{}) (int, error)

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.

func Println

func Println(args ...interface{}) (int, error)

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.

func SetLevel

func SetLevel(level LogLevel)

SetLevel sets the log level for the application.

func SetPrintCallerInfo

func SetPrintCallerInfo(enabled bool)

SetPrintCallerInfo enables or disables the automatic addition of the calling function (with package) to the log messages. NOTE: enabling this feature can have severe impacts on performances since it uses reflection at runtime.

func SetPrintSourceInfo

func SetPrintSourceInfo(value int8)

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. NOTE: enabling this feature can have severe impacts on performances since it uses reflection at runtime.

func SetStream

func SetStream(stream io.Writer, colorise bool)

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.

func SetTimeFormat

func SetTimeFormat(format string)

SetTimeFormat sets the format for log messages time.

func ToJSON

func ToJSON(object interface{}) string

ToJSON converts an object into pretty-printed JSON format.

func Tracef

func Tracef(format string, args ...interface{}) (int, error)

Tracef writes a trace message to the current output stream, appending a new line.

func Traceln

func Traceln(args ...interface{}) (int, error)

Traceln writes a trace message to the current output stream, appending a new line.

func Warnf

func Warnf(format string, args ...interface{}) (int, error)

Warnf writes a warning message to the current output stream, appending a new line.

func Warnln

func Warnln(args ...interface{}) (int, error)

Warnln writes a warning message to the current output stream, appending a new line.

Types

type Flag

type Flag int8

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

type LogLevel

type LogLevel int

LogLevel represents the log level.

const (
	//TraceLevel is the LogLevel for trace messages
	TraceLevel LogLevel = iota
	// DebugLevel is the LogLevel for debug messages.
	DebugLevel
	// InfoLevel is the LogLevel for informational messages.
	InfoLevel
	// WarnLevel is the LogLevel for warning messages.
	WarnLevel
	// ErrorLevel is the LogLevel for error messages.
	ErrorLevel
	// FatalLevel is the LogLevel for fatal error messages.
	FatalLevel
	// PanicLevel is the LogLevel for fatal error messages that cause a panic.
	PanicLevel
	// NoneLevel is the LogLevel corresponding to no log output.
	NoneLevel
)

func GetLevel

func GetLevel() LogLevel

GetLevel retur s the current log level.

func LevelFromString

func LevelFromString(s string) (LogLevel, error)

LevelFromString returns a log Level value by parsing the user-provided string in a lenient way; if the parsing fails, returns and error.

func (LogLevel) String

func (l LogLevel) String() string

String returns a string representation of the log level for use in traces.

Jump to

Keyboard shortcuts

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