logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2015 License: GPL-2.0 Imports: 3 Imported by: 5

README

Logger GoDoc

This is a simple layer on top of the standard log package. Its main purpose is to give the developer separate log streams and logging severity levels.

The goal of this library is to be as simple as possible to use. It only adds the feature mentioned in the previous paragraph and nothing else.

You can see this package's documentation at godoc.org. It is much more than what you will find in this README.

Usage

Logging is as simple as

import (
    "github.com/ironsmile/logger"
)

func main() {
    logger.Errorf("I have an %d errors", 5)
    logger.SetLevel(logger.LevelDebug)
    logger.Debugln("A debug with a new line")
}

The above example uses the default logger. You can create as many loggers as you want:

func main() {
    lgStdOut := logger.New()
    lgStdOut.Log("Log object which logs to the stdout")

    outFile, _ := os.Create("/tmp/file.log")
    lgFile := logger.New()
    lgFile.SetLogOutput(outFile)
    lgFile.Log("This will be written in the log file")

    lgFile.SetErrorOutput(os.Stdout)
    lgFile.Errorln("This will be in the standard output")
}

You can take a look at the example file which showcases different usage scenarios.

Interface

Loggers support the Debug(f|ln)?, Log(f|ln)?, Error(f|ln)? and Fatal(f|ln)? functions which handle their arguments in the way fmt.Print(f|ln)? do. For more info regarding their arguments see the documentation.

A Logger is made up of 3 different exported log.Logger instances. Every one of them represents a logging stream. The Logger structure looks like this:

type Logger struct {
    Debugger *log.Logger
    Logger   *log.Logger
    Errorer  *log.Logger
    Level    int
}

As expected Debugger represents the debug stream, Logger - the logging and Errorer - the log stream. Being exported they can be used directly, possibly for configuration.

Logging Levels

Every Logger has a log level. There are 4 possible levels - LevelDebug, LevelLog, LevelError, LevelNoLog. See the package documentation for information on their usage.

Configuration

Use SetDebugOutput, SetLogOutput and SetErrorOutput to change the stream destination.

You can change the logging level by setting the Level property of a logger:

lg := logger.New()
lg.Level = logger.LevelDebug

For everything else you can access the underlying log.Logger instances directly.

Fatal functions

They do not have their own output stream but use the error stream. So there is no function SetFatalOutput and there is no property Fataller in the logger struct.

Underneath they actually use the log.Fatal functions which means a call to logger.Fatal(f|ln)? will print the message and halt the program.

Documentation

Overview

Package logger delivers a tiered loggin mechanics. It is a thin wrapper above the standard library's log package. Its main purpose is to give the developer separate log streams and logging severity levels. There are 3 types of streams: debug, log and error and 4 levels of logging: LevelDebug, LevelLog, LevelError and LevelNoLog.

Users of the package will interact with its Logger type. It supports the Debug(f|ln)?, Log(f|ln)?, Error(f|ln)? and Fatal(f|ln)? functions which handle their arguments in the way fmt.Print(f|ln)? do.

Every Logger has its own log level. See the constants section for information how to use this level.

For fine tuning loggers users will need to import the log package and use the Logger.Debugger, Logger.Logger and Logger.Errorer which are just instances of log.Logger.

The package supplies a default logger and a shorthand functions for using it. The default logger is created autmatically with its debug, log and error streams using stdout, stound and stderr respectively. Its logging level is set to LevelLog.

Index

Examples

Constants

View Source
const (
	LevelDebug = iota // Will show all messages
	LevelLog          // Will show error and log messages
	LevelError        // Will show error messages only
	LevelNoLog        // No logs will be emitted whatsoever
)

These constants are used to configer a Logger's output level. They are ordered in a strict ascending order. If the level of a logger is set at particular constant it will only emit messages in the streams for this constant's level and above.

For example if a logger has its Level set to LevelLog it will emit messages in its log and error streams but will not emit anything in the debug stream.

A special level LevelNoLog may be used in order to silence the logger completely.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug calls the default logger's Debug function. Arguments are handled in the manner of fmt.Print.

func Debugf

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

Debugf calls the default logger's Debugf function. Arguments are handled in the manner of fmt.Printf.

func Debugln

func Debugln(v ...interface{})

Debugln calls the default logger's Debugln function. Arguments are handled in the manner of fmt.Println.

func Error

func Error(v ...interface{})

Error calls the default logger's Error function. Arguments are handled in the manner of fmt.Print.

func Errorf

func Errorf(format string, args ...interface{})

Errorf calls the default logger's Errorf function. Arguments are handled in the manner of fmt.Printf.

func Errorln

func Errorln(v ...interface{})

Errorln calls the default logger's Errorln function. Arguments are handled in the manner of fmt.Println.

func Fatal

func Fatal(v ...interface{})

Fatal calls the default logger's Fatal function. Arguments are handled in the manner of fmt.Print.

func Fatalf

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

Fatalf calls the default logger's Fatalf function. Arguments are handled in the manner of fmt.Printf.

func Fatalln

func Fatalln(v ...interface{})

Fatalln calls the default logger's Fatalln function. Arguments are handled in the manner of fmt.Println.

func Log

func Log(v ...interface{})

Log calls the default logger's Log function. Arguments are handled in the manner of fmt.Print.

func Logf

func Logf(format string, args ...interface{})

Logf calls the default logger's Logf function. Arguments are handled in the manner of fmt.Printf.

func Logln

func Logln(v ...interface{})

Logln calls the default logger's Logln function. Arguments are handled in the manner of fmt.Println.

func SetDebugOutput

func SetDebugOutput(w io.Writer)

SetDebugOutput replaces the debug stream output of the default logger.

func SetErrorOutput

func SetErrorOutput(w io.Writer)

SetErrorOutput replaces the error stream output of the default logger.

func SetLevel

func SetLevel(level int)

SetLevel sets the log level of the default logger.

func SetLogOutput

func SetLogOutput(w io.Writer)

SetLogOutput replaces the log stream output of the default logger.

Types

type Logger

type Logger struct {

	/*
	   The debug stream. It is a pointer to the underlying log.Logger strcture
	   which is used when Debug, Debugf and Debugln are called.
	*/
	Debugger *log.Logger

	/*
	   The log stream. Used by Log, Logf and Logln.
	*/
	Logger *log.Logger

	/*
	   The error stream. Used by Error, Errorf and Errorln.
	*/
	Errorer *log.Logger

	/*
	   Which Log Level this Logger is using. See the descrption of the
	   log level constants to understend what this means.
	*/
	Level int
}

Logger is a type which actually consistes of 3 log.Logger object. One for every stream - debug, log and error.

Example
package main

import (
	"github.com/ironsmile/logger"
)

func main() {

	// Create a new logger
	lg := logger.New()

	// Using the underlying standard's library log.Logger instances
	// to reconfiger the debug stream.
	lg.Debugger.SetPrefix("")
	lg.Debugger.SetFlags(0)

	// Now all types of messages will be outputed.
	lg.Level = logger.LevelDebug

	// Emit something to the debug stream
	lg.Debugf("debug yourself and you will be %d time stronger", 42)
}
Output:

debug yourself and you will be 42 time stronger
Example (Configuration)
package main

import (
	"os"

	"github.com/ironsmile/logger"
)

func main() {
	lg := logger.New()

	// Increase this logger's verbosity
	lg.Level = logger.LevelDebug

	// Lets be sure all error messages have the '[ERROR] ' string in front of them
	lg.Errorer.SetPrefix("[ERROR] ")

	// We do not want anything in the message except our prefix.
	lg.Errorer.SetFlags(0)

	// We want errors to be seen in the standard output.
	lg.SetErrorOutput(os.Stdout)

	// Emit something to the error stream
	lg.Error("error to stdout")
}
Output:

[ERROR] error to stdout

func Default

func Default() *Logger

Default returns a pointer to the default Logger. Using it users can configure the behaviour of it.

Example
package main

import (
	"os"

	"github.com/ironsmile/logger"
)

func main() {

	// Reconfiguring the default logger
	logger.Default().Debugger.SetFlags(0)
	logger.Default().Debugger.SetPrefix("level.debug: ")

	// Setting its level
	logger.SetLevel(logger.LevelDebug)

	// Setting its output destination
	logger.SetDebugOutput(os.Stdout)

	// Actually printing something in the output destination
	logger.Debugln("the default may be more than you need")
}
Output:

level.debug: the default may be more than you need

func New

func New() *Logger

New creates and returns a new Logger. Its debug and log streams are stdout and its error stream is stderr. The returned logger will have log level set to LevelLog.

func (*Logger) Debug

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

Debug emits this message to the debug stream.

func (*Logger) Debugf

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

Debugf emits this message to the debug stream. Supports fmt.Printf formatting.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln emits this message to the debug streamand adds a new line at the end of the message. Similar to fmt.Println.

func (*Logger) Error

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

Error emits this message to the error stream.

func (*Logger) Errorf

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

Errorf emits this message to the error stream. Supports fmt.Printf formatting.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Error emits this message to the error stream and adds a new line at the end of the message. Similar to fmt.Println.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal will print the message to the error stream and halt the program.

func (*Logger) Fatalf

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

Fatalf is similar to Fatal but supports formatting. See fmt.Printf for format instructions.

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln is similar to Fatal but adds a new line at the end of the output.

func (*Logger) Log

func (l *Logger) Log(v ...interface{})

Log emits this message to the log stream.

func (*Logger) Logf

func (l *Logger) Logf(format string, args ...interface{})

Logf emits this message to the log stream. It supports formatting. See fmt.Printf for details on the formatting options.

func (*Logger) Logln

func (l *Logger) Logln(v ...interface{})

Logln emits this message to the log stream and adds a new line at the end of the message. Similar to fmt.Println.

func (*Logger) SetDebugOutput

func (l *Logger) SetDebugOutput(w io.Writer)

SetDebugOutput changes the debug output stream. It preserves the flags and prefix of the old stream.

func (*Logger) SetErrorOutput

func (l *Logger) SetErrorOutput(w io.Writer)

SetErrorOutput changes the error output stream. It preserves the flags and prefix of the old stream.

func (*Logger) SetLogOutput

func (l *Logger) SetLogOutput(w io.Writer)

SetLogOutput changes the log output stream. It preserves the flags and prefix of the old stream.

Jump to

Keyboard shortcuts

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