log

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2022 License: MIT Imports: 5 Imported by: 33

README

Log

Log is a library which provides utilities for logging messages

Getting started

To retrieve the library, simply run:

go get code.waarp.fr/lib/log

Then import it in your project

import "code.waarp.fr/lib/log"

Basic Usage

In order to create a logger, its backend must first be initialized. Logging backends can be initialized using the NewBackend function. This function requires the following arguments:

  1. level- The minimum logging level. All messages bellow this level will be ignored.
  2. logTo- The logs output.
  3. facility- The logging facility (only used for syslog backends).
  4. tag- The application's tag in the logging facility (only used for syslog backends).

Once the backend initialized, loggers can be created using the Backend.NewLogger method. This will return a new logger with the given name, and which writes to the target backend.

License

Log is licensed under the terms of the MIT License which are detailed in the LICENSE file.

Documentation

Overview

Package log provides utilities for logging messages to various outputs.

Index

Constants

View Source
const (
	Stdout  = "stdout"
	Stderr  = "stderr"
	Discard = "discard"
	Syslog  = "syslog"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

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

func NewBackend

func NewBackend(level Level, logTo, facility, tag string) (*Backend, error)

NewBackend initializes the logging backend according to the given configuration. If the backend cannot be accessed, an error is returned.

Level represents the minimum logging priority accepted by the backend. Any message with a lower priority will be discarded.

LogTo is the logging output. It can be either: - Stdout - Stderr - Discard - Syslog - a file name

Facility and Tag are only used with a Syslog output. They represent respectively the logging facility and the application's name (in the logging facility).

func (*Backend) ChangeLevel

func (b *Backend) ChangeLevel(level Level)

func (*Backend) NewLogger

func (b *Backend) NewLogger(name string) *Logger

NewLogger initiates and returns a new logger with the target Backend as output.

func (*Backend) SetFormatter added in v1.2.0

func (b *Backend) SetFormatter(formatter Formatter)

type BackendPool added in v1.1.0

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

BackendPool represent a pool of backends. When instantiating a Logger from a pool of backend, any message written to this Logger will be written to all to backends from the pool. This can be useful if one wishes to send log messages to different outputs depending on the message's level.

func (*BackendPool) AddBackend added in v1.1.0

func (b *BackendPool) AddBackend(back *Backend)

AddBackend adds a backend to the pool.

func (*BackendPool) NewLogger added in v1.1.0

func (b *BackendPool) NewLogger(name string) *Logger

NewLogger initiates and returns a new logger with all the pool's backends as output.

type Formatter added in v1.2.0

type Formatter func(*Record) string

Formatter is the types of the functions that can be used to format a log entry. They take a pointer to a record and return a formatted string.

type Level

type Level uint8

Level represents a logging level. Available levels are: - LevelDebug - LevelInfo - LevelNotice - LevelWarning - LevelError - LevelCritical - LevelAlert - LevelFatal.

const (
	LevelTrace Level
	LevelDebug
	LevelInfo
	LevelNotice
	LevelWarning
	LevelError
	LevelCritical
	LevelAlert
	LevelFatal
)

func (Level) String

func (i Level) String() string

type Logger

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

Logger is an internal abstraction of the underlying logging library.

func (*Logger) Alert

func (l *Logger) Alert(msg string, args ...any)

Alert outputs a log message at the "alert" (LevelAlert) priority.

func (*Logger) AsStdLogger added in v1.1.0

func (l *Logger) AsStdLogger(level Level) *log.Logger

AsStdLogger returns an instance of the standard log.Logger. Any messages written to this standard logger will be written to the source logger with the given level.

func (*Logger) Critical

func (l *Logger) Critical(msg string, args ...any)

Critical outputs a log message at the "critical" (LevelCritical) priority.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug outputs a log message at the "debug" (LevelDebug) priority.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error outputs a log message at the "error" (LevelError) priority.

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...any)

Fatal outputs a log message at the "fatal" (LevelFatal) priority, and immediately exits the program afterwards.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info outputs a log message at the "info" (LevelInfo) priority.

func (*Logger) Notice

func (l *Logger) Notice(msg string, args ...any)

Notice outputs a log message at the "notice" (LevelNotice) priority.

func (*Logger) Trace

func (l *Logger) Trace(msg string, args ...any)

Trace outputs a log message at the "trace" (LevelTrace) priority.

func (*Logger) Warning

func (l *Logger) Warning(msg string, args ...any)

Warning outputs a log message at the "warning" (LevelWarning) priority.

type Record added in v1.2.0

type Record struct {
	Logger    string
	Timestamp time.Time
	Level     Level
	Message   string
}

Record contains the data to be logged. It is passed to a formatter to generate the logged message.

Jump to

Keyboard shortcuts

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