logger

package module
v0.0.0-...-25259d2 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2018 License: BSD-3-Clause Imports: 10 Imported by: 3

README

logger: logging handler

Logging handler for Go.

GoDoc

Features

  • File and syslog logging backend
  • Colors when printing to stderr
  • Multiple logging backend support
  • Logging contexts

Example

The following code:

logger, err := logger.NewLogger(
	logger.FileConfig{
		Level: "debug",
		Path:  "/path/to/file.log",
	},
	logger.SyslogConfig{
		Tag:      "myapp",
		Level:    "warning",
		Facility: "local7",
	},
)
if err != nil {
	log.Fatalf("failed to initialize logger: %s", err)
}

logger.Info("begin")

ctx := logger.Context("test")
ctx.Info("entering context")
ctx.Debug("start time: %s", time.Now())
ctx.Info("leaving context")

logger.Warning("this is a sample warning")

logger.Info("end")

will output in /path/to/file.log:

2016/08/21 12:39:23.093357 INFO: begin
2016/08/21 12:39:23.093438 INFO: test: entering context
2016/08/21 12:39:23.093458 DEBUG: test: start time: 2016-08-21 12:39:23.093447326 +0200 CEST
2016/08/21 12:39:23.093475 INFO: test: leaving context
2016/08/21 12:39:23.093480 WARNING: this is a sample warning
2016/08/21 12:39:23.093485 INFO: end

and only sends "this is a sample warning" to syslog on local7 facility.

Documentation

Overview

Package logger is a simple wrapper around log.Logger with usual logging levels "error", "warning", "notice", "info" and "debug".

Index

Constants

View Source
const (

	// LevelError represents the error logging level.
	LevelError
	// LevelWarning represents the warning logging level.
	LevelWarning
	// LevelNotice represents the notice logging level.
	LevelNotice
	// LevelInfo represents the info logging level.
	LevelInfo
	// LevelDebug represents the debug logging level.
	LevelDebug
)

Variables

View Source
var (
	// ErrInvalidFacility represents an invalid syslog facility error.
	ErrInvalidFacility = errors.New("invalid syslog facility")
	// ErrInvalidLevel represents an invalid logging level error.
	ErrInvalidLevel = errors.New("invalid logging level")
	// ErrUnsupportedBackend represents an unsupported backend error.
	ErrUnsupportedBackend = errors.New("unsupported backend")
)

Functions

func Debug

func Debug(format string, v ...interface{})

Debug prints a debug message using the default logger.

func Error

func Error(format string, v ...interface{})

Error prints an error message using the default logger.

func Info

func Info(format string, v ...interface{})

Info prints an information message using the default logger.

func Notice

func Notice(format string, v ...interface{})

Notice prints a notice message using the default logger.

func Warning

func Warning(format string, v ...interface{})

Warning prints a warning message using the default logger.

Types

type FileConfig

type FileConfig struct {
	// Logging output severity level. Messages with higher severity value will be discarded.
	Level string

	// File path of the logging output. If path is either empty or "-", logging will be output to os.Stderr.
	Path string
}

FileConfig represents a file backend configuration.

type Logger

type Logger struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Logger represents a logger instance.

func NewLogger

func NewLogger(configs ...interface{}) (*Logger, error)

NewLogger returns a new Logger instance initialized with the given configuration. If no configs are passed are parameter, log messages will effectively be discarded.

func (*Logger) Close

func (l *Logger) Close()

Close closes the logger output file.

func (*Logger) Context

func (l *Logger) Context(context string) *Logger

Context clones the Logger instance and sets the context to the provided string.

func (*Logger) CurrentContext

func (l *Logger) CurrentContext() string

CurrentContext returns the current logger context.

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...interface{}) *Logger

Debug prints a debug message in the logging system.

func (*Logger) Error

func (l *Logger) Error(format string, v ...interface{}) *Logger

Error prints an error message in the logging system.

func (*Logger) Info

func (l *Logger) Info(format string, v ...interface{}) *Logger

Info prints an information message in the logging system.

func (*Logger) Logger

func (l *Logger) Logger(level int) *log.Logger

Logger returns a log.Logger instance for a given logging level.

func (*Logger) Notice

func (l *Logger) Notice(format string, v ...interface{}) *Logger

Notice prints a notice message in the logging system.

func (*Logger) Warning

func (l *Logger) Warning(format string, v ...interface{}) *Logger

Warning prints a warning message in the logging system.

type SyslogConfig

type SyslogConfig struct {
	// Logging output severity level. Messages with higher severity value will be discarded.
	Level string

	// syslog facility to send messages to.
	Facility string

	// syslog tag to specify in messages.
	Tag string

	// syslog service address and transport type (either "udp", "tcp" or "unix"). If not sepcified, local syslog will
	// be used.
	Address   string
	Transport string
}

SyslogConfig represents a syslog backend configuration.

Jump to

Keyboard shortcuts

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