golog

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2023 License: MIT Imports: 6 Imported by: 8

README

golog

Go Report Card GoDoc Lines of Code

golog implements a logging infrastructure for Go. It focuses on performance, while providing a simple API.

Installation

golog can be installed by using go get:

go get github.com/skifli/golog

After this command has been run, golog is ready to use. golog can be updated by using go get -u:

go get -u github.com/skifli/golog

Example

The example demonstrates the majority of the features found in golog.

Example Output

Documentation

See the godoc reference.

Documentation

Index

Constants

View Source
const (
	FATAL levelIndexes = iota
	ERROR
	WARNING
	INFO
	DEBUG
)

Enum to represent a level's index.

Variables

View Source
var LevelColours = map[string]string{
	"FATAL":   "\u001b[35m",
	"ERROR":   "\u001b[31m",
	"WARNING": "\u001b[33m",
	"INFO":    "\u001b[34m",
	"DEBUG":   "",

	"RESET": "\u001b[0m",
}
View Source
var Levels = []string{
	"FATAL",
	"ERROR",
	"WARNING",
	"INFO",
	"DEBUG",
}

Level names.

View Source
var LongestLevel = func() int {
	longestLevelLength := 0

	for _, levelName := range Levels {
		levelLength := len(levelName)

		if levelLength > longestLevelLength {
			longestLevelLength = levelLength
		}
	}

	return longestLevelLength
}()

Functions

func FormatterHuman

func FormatterHuman(
	now time.Time,
	level string,
	msg string,
	fields Fields,
) string

An example formatter for humans. Returns a human-readable representation of the log.

func FormatterJSON

func FormatterJSON(
	now time.Time,
	level string,
	msg string,
	fields Fields,
) string

An example formatter for JSON. Returns a JSON-compliant representation of the log.

Types

type Fields

type Fields = map[string]any

Represents the fields arguments used when logging.

type Log

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

Represents a log output. The zero value for Log is not ready for use, and must be populated.

func NewLog

func NewLog(writers []*os.File, formatter formatterType) *Log

Creates a new log output, and returns it. The formatter must follow the signature found in [formatterType].

func (*Log) AddWriter

func (log *Log) AddWriter(writer *os.File)

Adds a writer to the log.

func (*Log) AddWriters

func (log *Log) AddWriters(writers []*os.File)

Adds multiple writers to the log. Also see Log.AddWriter.

func (*Log) RemoveWriter

func (log *Log) RemoveWriter(writer *os.File)

Removes a writer from the log. It will remove as many instances of it as it can find.

func (*Log) RemoveWriters

func (log *Log) RemoveWriters(writers []*os.File)

Removes a writer from the log. It will remove as many instances of it as it can find. Also see Log.RemoveWriter.

type Logger

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

Represents a logger. The zero value for Logger is ready for use.

func NewLogger

func NewLogger(logs []*Log) *Logger

Creates a new logger, and returns it.

func (*Logger) AddLog

func (logger *Logger) AddLog(log *Log)

Adds a log to the logger.

func (*Logger) AddLogs

func (logger *Logger) AddLogs(logs []*Log)

Adds multiple logs to the logger. Also see Logger.AddLog

func (*Logger) Debug

func (logger *Logger) Debug(msg string)

Logs a message, using DEBUG as the log level.

func (*Logger) DebugFields added in v1.1.0

func (logger *Logger) DebugFields(msg string, fields Fields)

Logs a message with the specified fields, using DEBUG as the log level.

func (*Logger) DebugFieldsf added in v1.1.0

func (logger *Logger) DebugFieldsf(msg string, fields Fields, format ...any)

Formats the message, and then logs it with the specified fields, using DEBUG as the log level.

func (*Logger) Debugf

func (logger *Logger) Debugf(msg string, format ...any)

Formats the message, using DEBUG as the log level.

func (*Logger) Error

func (logger *Logger) Error(msg string)

Logs a message, using ERROR as the log level.

func (*Logger) ErrorFields added in v1.1.0

func (logger *Logger) ErrorFields(msg string, fields Fields)

Logs a message with the specified fields, using ERROR as the log level.

func (*Logger) ErrorFieldsf added in v1.1.0

func (logger *Logger) ErrorFieldsf(msg string, fields Fields, format ...any)

Formats the message, and then logs it with the specified fields, using ERROR as the log level.

func (*Logger) Errorf

func (logger *Logger) Errorf(msg string, format ...any)

Formats the message, using ERROR as the log level.

func (*Logger) Fatal

func (logger *Logger) Fatal(msg string)

Logs a message, using FATAL as the log level. Then calls 'os.Exit(1)'.

func (*Logger) FatalFields added in v1.1.0

func (logger *Logger) FatalFields(msg string, fields Fields)

Logs a message with the specified fields, using FATAL as the log level. Then calls 'os.Exit(1)'.

func (*Logger) FatalFieldsf added in v1.1.0

func (logger *Logger) FatalFieldsf(msg string, fields Fields, format ...any)

Formats the message, and then logs it with the specified fields, using FATAL as the log level. Then calls 'os.Exit(1)'.

func (*Logger) Fatalf

func (logger *Logger) Fatalf(msg string, format ...any)

Formats the message, using FATAL as the log level. Then calls 'os.Exit(1)'.

func (*Logger) Info

func (logger *Logger) Info(msg string)

Logs a message, using INFO as the log level.

func (*Logger) InfoFields added in v1.1.0

func (logger *Logger) InfoFields(msg string, fields Fields)

Logs a message with the specified fields, using INFO as the log level.

func (*Logger) InfoFieldsf added in v1.1.0

func (logger *Logger) InfoFieldsf(msg string, fields Fields, format ...any)

Formats the message, and then logs it with the specified fields, using INFO as the log level.

func (*Logger) Infof

func (logger *Logger) Infof(msg string, format ...any)

Formats the message, using INFO as the log level.

func (*Logger) Panic

func (logger *Logger) Panic(err error)

Logs the error, using ERROR as the log level. Then calls 'panic(err)'.

func (*Logger) PanicFields added in v1.1.0

func (logger *Logger) PanicFields(err error, fields Fields)

Logs the error with the specified fields, using ERROR as the log level. Then calls 'panic(err)'.

func (*Logger) PanicFieldsf added in v1.1.0

func (logger *Logger) PanicFieldsf(msg string, fields Fields, format ...any)

Formats the message, and then logs it with the specified fields, using ERROR as the log level. Then calls 'panic(err)'.

func (*Logger) Panicf

func (logger *Logger) Panicf(msg string, format ...any)

Formats the message, using ERROR as the log level. Then calls 'panic(err)'.

func (*Logger) RemoveLog

func (logger *Logger) RemoveLog(log *Log)

Removes a log from the logger. It will remove as many instances of it as it can find.

func (*Logger) RemoveLogs

func (logger *Logger) RemoveLogs(logs []*Log)

Removes multiple logs from the logger. It will remove as many instances of it as it can find. Also see Logger.RemoveLog.

func (*Logger) Warning

func (logger *Logger) Warning(msg string)

Logs a message, using WARNING as the log level.

func (*Logger) WarningFields added in v1.1.0

func (logger *Logger) WarningFields(msg string, fields Fields)

Logs a message with the specified fields, using WARNING as the log level.

func (*Logger) WarningFieldsf added in v1.1.0

func (logger *Logger) WarningFieldsf(msg string, fields Fields, format ...any)

Formats the message, and then logs it with the specified fields, using WARNING as the log level.

func (*Logger) Warningf

func (logger *Logger) Warningf(msg string, format ...any)

Formats the message, using WARNING as the log level.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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