yagll

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 5 Imported by: 0

README

YAGLL - Yet Another Go Logging Libary

My personal take on a logging libary, primarily made for ResponsePlan, inspired by Distillog.

Features:

Adjustable log output

The log output can be adjusted using the SetOutput function. Any os.File can be used as an output.

Toggle every log level on and off

Every log level can be toggled on and off using the Toggle function.

Adjustable colors

The colors used for the log levels can be adjusted using the SetColor function.

Adjustable template using text/template

The template used to format the log messages is fully adjustable using text/template. The following variables are available:

  • {{ .Time }} - The time the log message was created
  • {{ .Level }} - The log level of the message
  • {{ .Message }} - The message itself
  • {{ .Color }} - The color of the log level
  • {{ .Reset }} - The reset code to reset the color

Default logger

A default logger is provided, which can be used to log messages without creating a new logger. The default logger can be accessed at package level.

Documentation

Index

Constants

View Source
const (
	// ANSI color codes
	Reset  = "\033[0m"
	Black  = "\033[30m"
	Red    = "\033[31m"
	Green  = "\033[32m"
	Yellow = "\033[33m"
	Blue   = "\033[34m"
	Purple = "\033[35m"
	Cyan   = "\033[36m"
	White  = "\033[37m"
)

Variables

This section is empty.

Functions

func Debugf

func Debugf(format string, a ...interface{})

Print the formatted message to the debug level output. The message is formatted using fmt.Sprintf and always ends in a new line.

func Debugln

func Debugln(message string)

Print the message to the debug level output. The message always ends in a new line.

func Errorf

func Errorf(format string, a ...interface{})

Print the formatted message to the error level output. The message is formatted using fmt.Sprintf and always ends in a new line.

func Errorln

func Errorln(message string)

Print the message to the error level output. The message always ends in a new line.

func Infof

func Infof(format string, a ...interface{})

Print the formatted message to the info level output. The message is formatted using fmt.Sprintf and always ends in a new line.

func Infoln

func Infoln(message string)

Print the message to the info level output. The message always ends in a new line.

func SetColor

func SetColor(level Level, color string)

Sets the color used by the logger for the given level.

func SetDefaultColors

func SetDefaultColors()

Sets the default colors used by the logger.

func SetDefaultOutput

func SetDefaultOutput()

Sets the default output files used by the logger (Stdout for DEBUG and INFO, Stderr for ERROR).

func SetDefaultTemplate

func SetDefaultTemplate()

Sets the default template used by the logger. text/template is used to format the output.

func SetDefaultTimeFormat

func SetDefaultTimeFormat()

Sets the time format to the default format used by the logger.

func SetOutput

func SetOutput(level Level, file *os.File)

Set output file for the given level.

func SetTemplate

func SetTemplate(template string)

Sets the template used by the logger. text/template is used to format the output. Default template: {{ .Time }} [{{ .Color }}{{ .Level }}{{ .Reset }}] {{ .Message }}

The following variables are available:

.Time: The current time formatted using the time format. Format can be set using SetTimeFormat.

.Color: The ANSI color for the level.

.Level: The level as string.

.Reset: The ANSI reset code.

.Message: The message.

func SetTimeFormat

func SetTimeFormat(format string)

Sets the time format used by the logger (time.Now().Format(format)).

func Toggle

func Toggle(level Level, toggle bool)

Toggle the output for the given level.

Types

type Level

type Level int
const (
	INFO Level = iota
	DEBUG
	ERROR
)

func (Level) String

func (l Level) String() string

Returns the string representation of the level.

type Logger

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

func NewLogger

func NewLogger() *Logger

func (*Logger) Debugf

func (l *Logger) Debugf(format string, a ...interface{})

Print the formatted message to the debug level output. The message is formatted using fmt.Sprintf and always ends in a new line.

func (*Logger) Debugln

func (l *Logger) Debugln(message string)

Print the message to the debug level output. The message always ends in a new line.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, a ...interface{})

Print the formatted message to the error level output. The message is formatted using fmt.Sprintf and always ends in a new line.

func (*Logger) Errorln

func (l *Logger) Errorln(message string)

Print the message to the error level output. The message always ends in a new line.

func (*Logger) Infof

func (l *Logger) Infof(format string, a ...interface{})

Print the formatted message to the info level output. The message is formatted using fmt.Sprintf and always ends in a new line.

func (*Logger) Infoln

func (l *Logger) Infoln(message string)

Print the message to the info level output. The message always ends in a new line.

func (*Logger) SetColor

func (l *Logger) SetColor(level Level, color string)

func (*Logger) SetDefaultColors

func (l *Logger) SetDefaultColors()

func (*Logger) SetDefaultOutput

func (l *Logger) SetDefaultOutput()

func (*Logger) SetDefaultTemplate

func (l *Logger) SetDefaultTemplate()

Sets the template to the default template used by the logger. Uses text/template to format the message.

Default template: {{ .Time }} [{{ .Color }}{{ .Level }}{{ .Reset }}] {{.Message}}

func (*Logger) SetDefaultTimeFormat

func (l *Logger) SetDefaultTimeFormat()

Sets the time format to the default format used by the logger.

func (*Logger) SetOutput

func (l *Logger) SetOutput(level Level, file *os.File)

func (*Logger) SetTemplate

func (l *Logger) SetTemplate(template string)

Sets the template used by the logger. Uses text/template to format the message.

func (*Logger) SetTimeFormat

func (l *Logger) SetTimeFormat(format string)

Sets the time format used by the logger (time.Now().Format(format)).

func (*Logger) Toggle

func (l *Logger) Toggle(level Level, toggle bool)

Toggle weather or not to print a message to the output.

Jump to

Keyboard shortcuts

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