logger

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: BSD-2-Clause Imports: 6 Imported by: 3

README

logger is a threadsafe logging library for go that does conditional logging controlled via UNIX-style loglevels.
Documentation is available through go doc.
The library is free software licensed under a 2-clause BSD license.

©2020 Jörg Walter

Documentation

Overview

The package logger provides a Logger type that can receive log records. Logger will only display a record if it is as severe as or more severe than the loglevel set for the Logger. Users of Logger therefore can control how much logging output they will see while running their program. A Logger can be used by multiple goroutines.

For usable loglevels see const.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Loglevels added in v0.4.0

func Loglevels() map[Level]string

Loglevels returns map with the string representations of all available loglevels.

func SetupDefaultLogger added in v0.4.1

func SetupDefaultLogger(w io.Writer, level Level, delimiter string)

Creates a default logger that can be used by calling Default().

Types

type Level added in v0.4.0

type Level int

Represents a loglevel.

const (
	LevelInvalid  Level = iota //Not a loglevel, represents an invalid loglevel internally.
	LevelPanic                 //Reports a crash or a program termination that was necessary to prevent something more severe.
	LevelAlert                 //Reports a condition that needs immediate action by the user.
	LevelCritical              //Reports a hard error.
	LevelError                 //Reports a normal error condition.
	LevelWarning               //Reports something that isn't an error but might cause problems later.
	LevelNotice                //Reports conditions that are no errors but may require special handling.
	LevelInfo                  //Reports normal user information about expected conditions.
	LevelDebug                 //Reports information that is only interesting for debugging or development.
)

func ParseLevel added in v0.4.0

func ParseLevel(input string) (Level, error)

Tries to associate the input string with a specific loglevel. Returns that loglevel on success, on failure LevelInvalid and an error is returned.

func (Level) String added in v0.4.0

func (r Level) String() string

String returns the string representation of a Level. If the Level is not defined, String returns "Undefined".

type LevelFlag

type LevelFlag Level

LevelFlag implements flag.Getter

func (*LevelFlag) Get added in v0.4.0

func (f *LevelFlag) Get() any

func (*LevelFlag) Set

func (f *LevelFlag) Set(arg string) error

func (*LevelFlag) String

func (f *LevelFlag) String() string

type Logger

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

Logger is the data type used for sending log records to.

func Default added in v0.4.1

func Default() *Logger

Returns the default logger, will panic if SetupDefaultLogger() has not been called yet.

func New

func New(w io.Writer, level Level, delimiter string) *Logger

New constructs a new Logger. It will print a log record to its given writer if it fulfills the Logger's designated loglevel or a more severe one. If you set the level to LevelCritical, the Logger will print all messages of LevelPanic or LevelAlert or LevelCritical.

func (*Logger) Alert

func (l *Logger) Alert(v ...any) (n int, err error)

Alert sends a message of loglevel LevelAlert to the Logger.

func (*Logger) Alertf added in v0.3.0

func (l *Logger) Alertf(format string, a ...any) (n int, err error)

Alertf sends a formatted message of loglevel LevelAlert to the Logger.

func (*Logger) Critical

func (l *Logger) Critical(v ...any) (n int, err error)

Critical sends a message of loglevel LevelCritical to the Logger.

func (*Logger) Criticalf added in v0.3.0

func (l *Logger) Criticalf(format string, a ...any) (n int, err error)

Criticalf sends a formatted message of loglevel LevelCritical to the Logger.

func (*Logger) Debug

func (l *Logger) Debug(v ...any) (n int, err error)

Debug sends a message of loglevel LevelDebug to the Logger.

func (*Logger) Debugf added in v0.3.0

func (l *Logger) Debugf(format string, a ...any) (n int, err error)

Debugf sends a formatted message of loglevel LevelDebug to the Logger.

func (*Logger) Die added in v0.2.1

func (l *Logger) Die(v ...any)

Die sends a message of loglevel LevelPanic to the Logger, then exits with code 1.

func (*Logger) Dief added in v0.3.0

func (l *Logger) Dief(format string, a ...any)

Dief sends a formatted message of loglevel LevelPanic to the Logger, then exits with code 1.

func (*Logger) Error

func (l *Logger) Error(v ...any) (n int, err error)

Error sends a message of loglevel LevelError to the Logger.

func (*Logger) Errorf added in v0.3.0

func (l *Logger) Errorf(format string, a ...any) (n int, err error)

Errorf sends a formatted message of loglevel LevelError to the Logger.

func (*Logger) Info

func (l *Logger) Info(v ...any) (n int, err error)

Info sends a message of loglevel LevelInfo to the Logger.

func (*Logger) Infof added in v0.3.0

func (l *Logger) Infof(format string, a ...any) (n int, err error)

Infof sends a formatted message of loglevel LevelInfo to the Logger.

func (*Logger) Level

func (l *Logger) Level() Level

Level returns the Logger's current loglevel as an integer.

func (*Logger) Notice

func (l *Logger) Notice(v ...any) (n int, err error)

Notice sends a message of loglevel LevelNotice to the Logger.

func (*Logger) Noticef added in v0.3.0

func (l *Logger) Noticef(format string, a ...any) (n int, err error)

Noticef sends a formatted message of loglevel LevelNotice to the Logger.

func (*Logger) Panic

func (l *Logger) Panic(v ...any) (n int, err error)

Panic sends a message of loglevel LevelPanic to the Logger. Please note that it does NOT call panic()!

func (*Logger) Panicf added in v0.3.0

func (l *Logger) Panicf(format string, a ...any) (n int, err error)

Panicf sends a formatted message of loglevel LevelPanic to the Logger. Please note that it does NOT call panic()!

func (*Logger) Printf added in v0.3.0

func (l *Logger) Printf(level Level, format string, a ...any) (n int, err error)

Printf writes a formatted log message if the logger was configured to print the given level.

func (*Logger) Println

func (l *Logger) Println(level Level, v ...any) (n int, err error)

Println writes the log message if its log level is equally severe or more severe than that set for the Logger.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets a new loglevel for the Logger. Setting an invalid loglevel will cause a panic.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput changes the writer the Logger will write its messages to.

func (*Logger) SetTimeFormat

func (l *Logger) SetTimeFormat(format string)

SetTimeFormat takes a format string as defined in the "(t Time) Format" function of go's "time" module. If such a string is set, log records will display a timestamp formatted like specified by the format string. To remove timestamps from future log records, set the format string to "".

func (*Logger) TimeFormat

func (l *Logger) TimeFormat() string

TimeFormat returns the current format string for the timestamp. If it returns "", log records will have no timestamp.

func (*Logger) Warning

func (l *Logger) Warning(v ...any) (n int, err error)

Warning sends a message of loglevel LevelWarning to the Logger.

func (*Logger) Warningf added in v0.3.0

func (l *Logger) Warningf(format string, a ...any) (n int, err error)

Warningf sends a formatted message of loglevel LevelWarning to the Logger.

Jump to

Keyboard shortcuts

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