README

This repository contains logging packages for Go:

  • stdlog is the main package of this repository, it is a simple and fast logger to the standard output.
  • buflog and golog are customizable logging class which can be used as a standalone or as a building block for other loggers. stdlog is built upon them.
  • log just provides a common interface for logging libraries.

You are more than welcome to ask questions on the Go mailing-list and open issues here if you find bugs.

stdlog

Documentation

Package stdlog provides simple and fast logging to the standard output (stdout) and is optimized for programs launched via a shell or cron. It can also be used to log to a file by redirecting the standard output to a file. This package is thread-safe.

Basic examples:

logger := stdlog.GetFromFlags()
logger.Info("Connecting to the server...")
logger.Errorf("Connection failed: %q", err)

Will output:

2014-04-02 18:09:15.862 INFO Connecting to the API...
2014-04-02 18:10:14.347 ERROR Connection failed (Server is unavailable).

Log*() functions can be used to avoid evaluating arguments when it is expensive and unnecessary:

logger.Debug("Memory usage: %s", getMemoryUsage())
if LogDebug() { logger.Debug("Memory usage: %s", getMemoryUsage()) }

If debug logging is off the getMemoryUsage() will be executed on the first line while it will not be executed on the second line.

List of command-line arguments:

-log=info
    Log events at or above this level are logged.
-stderr=false
    Logs are written to standard error (stderr) instead of standard
    output.
-flushlog=none
    Until this level is reached nothing is output and logs are stored
    in the memory. Once a log event is at or above this level, it
    outputs all logs in memory as well as the future log events. This
    feature should not be used with long-running processes.

The available levels are the eight ones described in RFC 5424 (debug, info, notice, warning, error, critical, alert, emergency) and none.

Some use cases:

  • By default, all logs except debug ones are output to the stdout. Which is useful to follow the execution of a program launched via a shell.
  • A program launched by a crontab where the variable MAILTO is set with -debug -flushlog=error will send all logs generated by the program only if an error happens. When there is no error the email will not be sent.
  • my_program > /var/log/my_program/my_program-$(date+%Y-%m-%d-%H%M%S).log will create a log file in /var/log/my_program each time it is run.

buflog

Documentation

Package buflog provides a buffered logging class that accumulates logs in memory until the flush threshold is reached which release stored logs, the buffered logger then act as a normal logger.

Basic example:

logger := buflog.New(os.Stdout, log.Info, log.Error)
logger.Info("Connecting to the server...")   // Outputs nothing
logger.Error("Connection failed")            // Outputs both lines

golog

Documentation

Package golog provides a customizable logging class which can be used as a standalone or as a building block for other loggers.

Basic example:

logger := golog.New(os.Stdout, log.Info)
logger.Info("Connecting to the server...")
logger.Errorf("Connection failed: %q", err)

Will output:

2014-04-02 18:09:15.862 INFO Connecting to the API...
2014-04-02 18:10:14.347 ERROR Connection failed (Server is unavailable).

Log*() functions can be used to avoid evaluating arguments when it is expensive and unnecessary:

logger.Debug("Memory usage: %s", getMemoryUsage())
if logger.LogDebug() { logger.Debug("Memory usage: %s", getMemoryUsage()) }

If debug logging is off getMemoryUsage() will be executed on the first line while it will not be executed on the second line.

log

Documentation

Package log provides a common interface for logging libraries.

Expand ▾ Collapse ▴

Documentation

Overview

    Package log provides a common interface for logging libraries.

    Index

    Constants

    This section is empty.

    Variables

    View Source
    var NullLogger = nullLogger{}

      NullLogger is a no-op instance of the Logger interface.

      Functions

      This section is empty.

      Types

      type Level

      type Level int8

        Level specifies a level of verbosity. The available levels are the eight severities described in RFC 5424 and none.

        const (
        	None Level = iota - 1
        	Emergency
        	Alert
        	Critical
        	Error
        	Warning
        	Notice
        	Info
        	Debug
        )

        type Logger

        type Logger interface {
        	// Emergency logs with an emergency level.
        	Emergency(args ...interface{})
        
        	// Emergencyf logs with an emergency level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Emergencyf(format string, args ...interface{})
        
        	// Alert logs with an alert level.
        	Alert(args ...interface{})
        
        	// Alertf logs with an alert level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Alertf(format string, args ...interface{})
        
        	// Critical logs with a critical level.
        	Critical(args ...interface{})
        
        	// Criticalf logs with a critical level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Criticalf(format string, args ...interface{})
        
        	// Error logs with an error level.
        	Error(args ...interface{})
        
        	// Errorf logs with an error level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Errorf(format string, args ...interface{})
        
        	// Warning logs with a warning level.
        	Warning(args ...interface{})
        
        	// Warningf logs with a warning level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Warningf(format string, args ...interface{})
        
        	// Notice logs with a notice level.
        	Notice(args ...interface{})
        
        	// Noticef logs with a notice level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Noticef(format string, args ...interface{})
        
        	// Info logs with an info level.
        	Info(args ...interface{})
        
        	// Infof logs with an info level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Infof(format string, args ...interface{})
        
        	// Debug logs with a debug level.
        	Debug(args ...interface{})
        
        	// Debugf logs with a debug level.
        	// Arguments are handled in the manner of fmt.Printf.
        	Debugf(format string, args ...interface{})
        
        	// Log logs at the level passed in argument.
        	Log(level Level, args ...interface{})
        
        	// Logf logs at the level passed in argument.
        	// Arguments are handled in the manner of fmt.Printf.
        	Logf(level Level, format string, args ...interface{})
        
        	// LogEmergency returns true if the log level is at emergency or lower.
        	LogEmergency() bool
        
        	// LogAlert returns true if the log level is at alert or lower.
        	LogAlert() bool
        
        	// LogCritical returns true if the log level is at critical or lower.
        	LogCritical() bool
        
        	// LogError returns true if the log level is at error or lower.
        	LogError() bool
        
        	// LogWarning returns true if the log level is at warning or lower.
        	LogWarning() bool
        
        	// LogNotice returns true if the log level is at notice or lower.
        	LogNotice() bool
        
        	// LogInfo returns true if the log level is at info or debug.
        	LogInfo() bool
        
        	// LogDebug returns true only if the log level is at debug.
        	LogDebug() bool
        
        	// LogLevel returns true if the log level is at or below the level argument.
        	LogLevel(level Level) bool
        
        	io.Closer
        }

          Logger is a common interface for logging libraries.

          Source Files

          Directories

          Path Synopsis
          Package buflog provides a buffered logging class that accumulates logs in memory until the flush threshold is reached which release stored logs, the buffered logger then act as a normal logger.
          Package buflog provides a buffered logging class that accumulates logs in memory until the flush threshold is reached which release stored logs, the buffered logger then act as a normal logger.
          Package golog provides a customizable logging class which can be used as a standalone or as a building block for other loggers.
          Package golog provides a customizable logging class which can be used as a standalone or as a building block for other loggers.
          Package logtest provides utilities for logging testing.
          Package logtest provides utilities for logging testing.
          Package stdlog provides simple and fast logging to the standard output (stdout) and is optimized for programs launched via a shell or cron.
          Package stdlog provides simple and fast logging to the standard output (stdout) and is optimized for programs launched via a shell or cron.