logger

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 8 Imported by: 2

README

go-logger

Go module providing simple logging support.

Take a look at main.go for an example.

Documentation

Overview

logger provides basic logging support for your application. Supported log destinations are the console and a log file

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseFile

func CloseFile()

CloseFile closes the underlaying file to which the logger messages are written.

func Debug

func Debug(message string, parameters ...any)

func Error

func Error(message string, parameters ...any)

func Fatal

func Fatal(message string, parameters ...any)

func Info

func Info(message string, parameters ...any)

func SetGlobalLogger

func SetGlobalLogger(l *Logger)

SetGlobalLogger updates the global default logger with a custom one. You can create one via the Logger struct.

func Trace

func Trace(message string, parameters ...any)

func Warning

func Warning(message string, parameters ...any)

Types

type FileLogger added in v1.3.0

type FileLogger struct {

	// Minimum log level for logging into a file
	Level Level

	// Absolute or relative path to log files to
	Path string

	// With this option the path of the log file will be appended with the current date
	// so that an own log file for each day is used. The format of the date is 'YYYYMMDD'
	AppendDate bool
	// contains filtered or unexported fields
}

FileLogger contains configuration options specific to logging into a file. It is enabled if the FilePath != ""

func (*FileLogger) CloseFile added in v1.3.0

func (l *FileLogger) CloseFile()

CloseFile closes the file that is currently used for logging messages to a file

type Level

type Level uint8

Level of the log message

const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarning
	LevelError
	LevelFatal
)

func GetLevelByName

func GetLevelByName(levelName string) Level

GetLevelByName tries to convert the given level name to the represented level code. Allowed values are: 'trace', 'debug', 'info', 'warn', 'warning', 'error', 'panic' and 'fatal' If an incorrect level name was given a warning is logged and info will be returned

func (Level) String added in v1.2.0

func (lvl Level) String() string

String returns a string expression of the level

type Logger

type Logger struct {

	// Minimum log level for printing to the console (stdout and stderr)
	Level Level

	// Colorizes the log messages for the console.
	// Even if you set this to true the user is able to overwrite this behaviour by
	// setting the environment variables "TERMINAL_DISABLE_COLORS" and
	// "TERMINAL_ENABLE_COLORS" (to force coloring for "unsupported" terminals)
	ColoredOutput bool

	// Whether to print the file and line number of the invoking (calling line)
	PrintSource bool

	// Only print the log message without any additional info. This property will ignore other options linke
	// PrintSource or FuncCallIncrement
	OnlyPrintMessage bool

	// While logging, the file and line number of the
	// invoking (calling) line can be printed out.
	// This defines an offset that is applied to the call stack.
	// If you are using an own wrapper function, you
	// have to set this value to one
	FuncCallIncrement int

	// Prefix is applied as a prefix for all log messages.
	// It's positioned after all other information:
	//  [INFO ] 2024-04-10 19:00:00 (file:1)PREFIX - Message
	Prefix string

	// Configuration options for logging into a file
	File *FileLogger
	// contains filtered or unexported fields
}

func CloneLogger added in v1.3.3

func CloneLogger(logger *Logger) *Logger

CloneLogger creates a copy of the provided logger with it's file reference. All configuration options are cloned from "logger" to the new one

func GetGlobalLogger

func GetGlobalLogger() *Logger

func GetLoggerFromEnv added in v1.2.0

func GetLoggerFromEnv(defaultLogger *Logger) *Logger

GetLoggerFromEnv returns a logging instance configured from the available environment variables.

The environment variables have to be named like the struct fields in upper case with the prefix "LOGGER_". Sub structs are divided also by an underscore. Example: "LOGGER_SUBCONFIG_DISABLED"

If no env variable was found the default value of the given logger struct will be used.

Note that only generic options can be set like: - Print and Log Level - Log path - ColoredOutput - Tracing disabled

func NewLogger

func NewLogger(logger *Logger) *Logger

NewLogger creates a new instance of the logger with the given configuration.

func NewLoggerWithFile

func NewLoggerWithFile(logger *Logger, file *Logger) *Logger

NewLoggerWithFile creates a new instance with the given logger configuration. Instead of opening a new file to write the log messages to, the old file reference of the other logger will be used internal. This enables you to write to the same file with different log configurations.

func (*Logger) Debug added in v1.0.2

func (l *Logger) Debug(message string, parameters ...any)

func (*Logger) Error added in v1.0.2

func (l *Logger) Error(message string, parameters ...any)

func (*Logger) Fatal added in v1.0.2

func (l *Logger) Fatal(message string, parameters ...any)

func (*Logger) Info added in v1.0.2

func (l *Logger) Info(message string, parameters ...any)

func (*Logger) Log

func (l *Logger) Log(level Level, message string, parameters ...any)

Log logs a message with the given level. As additional parameters you can specify replace values for the message. See "fmt.printf()" for more infos.

func (*Logger) Trace added in v1.0.2

func (l *Logger) Trace(message string, parameters ...any)

func (*Logger) Warning added in v1.0.2

func (l *Logger) Warning(message string, parameters ...any)

Directories

Path Synopsis
main contains a simple example of using this logger package
main contains a simple example of using this logger package

Jump to

Keyboard shortcuts

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