log

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 11 Imported by: 17

README

blitiri.com.ar/go/log

GoDoc Build Status Go Report Card

log is a Go package implementing a simple logger.

It implements an API somewhat similar to glog, with a focus towards simplicity and integration with standard tools such as systemd.

Examples

log.Init()  // only needed once.

log.Debugf("debugging information: %v", x)
log.Infof("something normal happened")
log.Errorf("something bad happened")
log.Fatalf("tragic")

if log.V(3) {  // only entered if -v was >= 3.
	expensiveDebugging()
}

Status

The API should be considered generally stable, and no backwards-incompatible changes are expected.

Some specific symbols are experimental, and are marked as such in their documentation. Those might see backwards-incompatible changes, including removing them entirely.

Contact

If you have any questions, comments or patches please send them to albertito@blitiri.com.ar.

Documentation

Overview

Package log implements a simple logger.

It implements an API somewhat similar to "github.com/google/glog" with a focus towards simplicity and integration with standard tools such as systemd.

There are command line flags (defined using the flag package) to control the behaviour of the default logger. By default, it will write to stderr without timestamps; this is suitable for systemd (or equivalent) logging.

Command-line flags:

-alsologtostderr
      also log to stderr, in addition to the file
-logfile string
      file to log to (enables logtime)
-logtime
      include the time when writing the log to stderr
-logtosyslog string
      log to syslog, with the given tag
-v int
      verbosity level (1 = debug)
Example
package main

import (
	"blitiri.com.ar/go/log"
)

func main() {
	log.Init() // only needed once.

	log.Debugf("debugging information: %v %v %v", 1, 2, 3)
	log.Infof("something normal happened")
	log.Errorf("something bad happened")

	if log.V(3) { // only entered if -v was >= 3.
		//expensiveDebugging()
	}

}
Output:

Index

Examples

Constants

View Source
const (
	Fatal = Level(-2)
	Error = Level(-1)
	Info  = Level(0)
	Debug = Level(1)
)

Standard logging levels.

Variables

View Source
var Default = &Logger{
	w: os.Stderr,

	Level: Info,

	callerSkip: 1,
	LogCaller:  true,
	LogLevel:   true,
	LogTime:    false,
}

Default logger, used by the top-level functions below.

Functions

func Debugf

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

Debugf is a convenient wrapper to Default.Debugf.

func Errorf

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

Errorf is a convenient wrapper to Default.Errorf.

func Fatalf

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

Fatalf is a convenient wrapper to Default.Fatalf.

func Infof

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

Infof is a convenient wrapper to Default.Infof.

func Init

func Init()

Init the default logger, based on the command-line flags.

func Log

func Log(level Level, skip int, format string, a ...interface{})

Log is a convenient wrapper to Default.Log.

func V

func V(level Level) bool

V is a convenient wrapper to Default.V.

Types

type Level

type Level int

Level is the type of the logging level constants, to prevent confusion.

type Logger

type Logger struct {
	// Minimum level to log. Messages below this level will be dropped.
	// Note this field is NOT thread safe, if you change it, it is strongly
	// recommended to do so right after creating the logger, and before it is
	// used.
	// The use of this field should be considered EXPERIMENTAL, the API for it
	// could change in the future.
	Level Level

	// Include timestamp in the log message.
	// The use of this field should be considered EXPERIMENTAL, the API for it
	// could change in the future.
	LogTime bool

	// Include the log level in the log message.
	// The use of this field should be considered EXPERIMENTAL, the API for it
	// could change in the future.
	LogLevel bool

	// Include the caller in the log message.
	// The use of this field should be considered EXPERIMENTAL, the API for it
	// could change in the future.
	LogCaller bool

	sync.Mutex
	// contains filtered or unexported fields
}

A Logger represents a logging object that writes logs to a writer.

func New

func New(w io.WriteCloser) *Logger

New creates a new Logger, which writes logs to w.

func NewFile

func NewFile(path string) (*Logger, error)

NewFile creates a new Logger, which writes logs to the given file.

func NewSyslog

func NewSyslog(priority syslog.Priority, tag string) (*Logger, error)

NewSyslog creates a new Logger, which writes logs to syslog, using the given priority and tag.

func (*Logger) Close

func (l *Logger) Close()

Close the writer behind the logger.

func (*Logger) Debugf

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

Debugf logs information at a Debug level.

func (*Logger) Errorf

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

Errorf logs information at an Error level. It also returns an error constructed with the given message, in case it's useful for the caller.

func (*Logger) Fatalf

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

Fatalf logs information at a Fatal level, and then exits the program with a non-0 exit code.

func (*Logger) Infof

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

Infof logs information at a Info level.

func (*Logger) Log

func (l *Logger) Log(level Level, skip int, format string, a ...interface{}) error

Log the message into the logger, at the given level. This is low-level and should rarely be needed, but it's available to allow the caller to have more complex logic if needed. skip is the number of frames to skip when computing the file name and line number.

func (*Logger) Reopen

func (l *Logger) Reopen() error

Reopen the file behind the logger, if any. This can be used to implement log rotation.

Only works for loggers created via NewFile, otherwise it is a no-op.

EXPERIMENTAL, this API could change in the future.

func (*Logger) V

func (l *Logger) V(level Level) bool

V returns true if the logger's level is >= the one given, false otherwise. It can be used to decide whether to use or gather debugging information only at a certain level, to avoid computing it needlessly.

Jump to

Keyboard shortcuts

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