logger

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package log provides an alternative to the standard log library.

Each LogLevels can have its own log provider. That means INFO can be logged in a file, ERROR is mailed and everything else will logged in the console. Different loggers with different log levels can be created. This means you can have a log for an importer and a different log for the application it self.

The log is easy to extend by implementing the log.Interface.

Example

This example demonstrate the basics of the log.Interface. For more details check the documentation.

package main

import (
	"github.com/patrickascher/gofw/logger"
	"github.com/patrickascher/gofw/logger/file"
)

func main() {

	// Register a new log with the name "access".
	fileLogger, err := file.New(file.Options{Filepath: "access.log"})
	if err != nil {
		//...
	}
	err = logger.Register(
		// log name
		"access",
		// The log should only log messages from the level WARNING and higher.
		// If the LogLevel is empty, it will start logging from TRACE.
		logger.Config{Writer: fileLogger, LogLevel: logger.WARNING},

		// Here is an example that everything should be logged in a file except CRITICAL, those should be emailed.
		// Each log level can have their own log provider.
		//
		// emailLogger = email.New(email.Options{...})
		// log.Config{Writer: logFile, CriticalWriter:emailLogger},
	)
	if err != nil {
		//...
	}

	// get the log
	log, err := logger.Get("access")
	if err != nil {
		//..
	}

	// log messages
	// The first parameter is the message it self. After that an unlimited number of arguments can follow.
	// It depends on the log provider how this is handled. Please check the provider documentation for more details.
	log.Info("User %v has successfully logged in", "John Doe") //This message will not be logged because the minimum log level is WARNING.
	log.Warning("User xy is locked because of too many login attempts")
}
Output:

Index

Examples

Constants

View Source
const (
	TRACE level = iota + 1
	DEBUG
	INFO
	WARNING
	ERROR
	CRITICAL
)

Log levels

Variables

View Source
var (
	ErrLogLevel        = errors.New("log: LogLevel is unknown %#v")
	ErrMandatoryWriter = errors.New("log: writer is mandatory")
	ErrUnknownLogger   = errors.New("log: %v does not exist")
)

Error messages

Functions

func Register

func Register(name string, c Config) error

Register adds a new log provider to the registry or reconfigure it. If the name already exists, it will be overwritten.

Types

type Config

type Config struct {
	LogLevel       level
	Writer         Interface
	TraceWriter    Interface
	DebugWriter    Interface
	InfoWriter     Interface
	WarningWriter  Interface
	ErrorWriter    Interface
	CriticalWriter Interface
}

Config for the log instance. Writer is mandatory, all others are optional. If the LogLevel is empty, TRACE will be set as default.

type Interface

type Interface interface {
	Write(LogEntry)
}

Interface is used by log providers.

type LogEntry

type LogEntry struct {
	Level     level
	Filename  string
	Line      int
	Timestamp time.Time
	Message   string
	Arguments []interface{}
}

LogEntry is representing the actual log message.

type Logger

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

func Get

func Get(name string) (*Logger, error)

Get the log by its name. If the log was not registered, an error will return.

func (*Logger) Critical

func (l *Logger) Critical(msg string, args ...interface{})

Critical log message

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...interface{})

Debug log message

func (*Logger) Error

func (l *Logger) Error(msg string, args ...interface{})

Error log message

func (*Logger) Info

func (l *Logger) Info(msg string, args ...interface{})

Info log message

func (*Logger) Trace

func (l *Logger) Trace(msg string, args ...interface{})

Trace log message

func (*Logger) Warning

func (l *Logger) Warning(msg string, args ...interface{})

Warning log message

Directories

Path Synopsis
Package console implements the log.Interface.
Package console implements the log.Interface.
Package file implements the log.Interface.
Package file implements the log.Interface.

Jump to

Keyboard shortcuts

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