log

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2015 License: BSD-3-Clause Imports: 9 Imported by: 2

README

A Simple Go Logger

A simplified log library with support for logging to console (stderr), file, and syslog. It supports three log levels: debug, info, and error.

Build Status

Usage

In its simplest form log will output use the info log level and output messages to the console. For example:

logger := log.New()
logger.Info("Hello, world!")

It may be configured to log elsewhere by configuring it with a target:

target := log.NewTargetOpt("file:///var/log/example.log")
logger := log.New(target)
logger.Info("Hello, world!")

By default a logger has its level set to info which will log info and error messages. To change the level:

level := log.NewLevelOpt("debug")
logger := log.New(level)
logger.Debug("Too much information here.")

You can combine the two:

target := log.SyslogOpt
level := log.NewLevelOpt("error")
logger := log.New(target, level)
logger.Error("Oops! Something went wrong.")

The log.SyslogOpt value is a target which points to the local syslog server.

The library's Writer implements io.Writer and can be used to write logs at a specific level:

wrt := log.NewWriter(log.LevelDebug, logger)
wrt.Write(data)

Targets

The NewTargetOpt function takes the URI (as a string) of a file or network socket to log to. If the file is a socket it is assumed to be attached to a running syslog server. Network sockets assume the same. The special values "syslog", "stderr", and "stdout" will cause the target to log to syslog, stderr, and stdout respectively. The log.SyslogOpt and log.ConsoleOpt values are shortcuts to calling log.NewTargetOpt("syslog") and log.NewTargetOpt("stderr").

Levels

The NewLevelOpt function takes the log level (as as string) as its only argument. Case is ignored. Valid levels are "debug", "info", and "error". Log messages at or greater than that level will be logged. The special values log.Debug, log.Info, and log.Error are equivelent to log.NewLevelOpt("debug"), log.NewLevelOpt("info"), and log.NewLevelOpt("error").

Logging

The following methods log to the logger:

  • Print: Log a message at the provided level.
  • Printf: Log a formatted message at the provided level.
  • Panic: Log a message at the error level and call panic().
  • Panicf: Log a formatted message at the error level and call panic().
  • Fatal: Log a message at the error level and call os.Exit(1).
  • Fatalf: Log a formatted message at the error level and call os.Exit(1).
  • Debug: Log a message at the debug level.
  • Debugf: Log a formatted message at the debug level.
  • Info: Log a message at the info level.
  • Infof: Log a formatted message at the info level.
  • Error: Log a message at the error level.
  • Errorf: Log a formatted message at the error level.

License

Copyright (c) 2014 Ryan Bourgeois. Licensed under BSD-Modified. See the LICENSE file for a copy of the license.

Documentation

Index

Constants

View Source
const (
	TargetStderr = "stderr"
	TargetStdout = "stdout"
	TargetSyslog = "syslog"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FileTarget

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

func NewFileTarget

func NewFileTarget(writer io.Writer) *FileTarget

Create a new file writer pointing to the provided open file.

func OpenFileTarget

func OpenFileTarget(file string) (*FileTarget, error)

Create a new file writer pointing to the file at the provided path.

func (*FileTarget) Close

func (s *FileTarget) Close() error

Close the writer.

func (*FileTarget) Write

func (s *FileTarget) Write(level Level, message string)

Print a log message to the writer.

type Level

type Level int
const (
	LevelDebug Level = iota
	LevelInfo
	LevelError
)

func NewLevel

func NewLevel(level string) Level

Convert a string to a level value.

type Logger

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

func New

func New(options ...Option) (*Logger, error)

Return a new logger.

func NewOrExit

func NewOrExit(options ...Option) *Logger

Return a new logger. If an error occurs print it to stderr and call os.Exit(1).

func (*Logger) Close

func (l *Logger) Close() error

Close the logger.

func (*Logger) Debug

func (l *Logger) Debug(message string)

Log a message at the `debug` level.

func (*Logger) Debugf

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

Log a formatted message at the `debug` level.

func (*Logger) Error

func (l *Logger) Error(message string)

Log a message at the `error` level.

func (*Logger) Errorf

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

Log a formatted message at the `error` level.

func (*Logger) Fatal

func (l *Logger) Fatal(message string)

Log a message at the `error` level and call os.Exit(1).

func (*Logger) Fatalf

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

Log a formatted message at the `error` level and call os.Exit(1).

func (*Logger) Info

func (l *Logger) Info(message string)

Log a message at the `info` level.

func (*Logger) Infof

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

Log a formatted message at the `info` level.

func (*Logger) Panic

func (l *Logger) Panic(message string)

Log a message at the `error` level and call panic().

func (*Logger) Panicf

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

Log a formatted message at the `error` level and call panic().

func (*Logger) Print

func (l *Logger) Print(level Level, message string)

Log a message at the provided level.

func (*Logger) Printf

func (l *Logger) Printf(level Level, format string, a ...interface{})

Log a formatted message at the provided level.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

Set the level of the logger.

func (*Logger) SetTarget

func (l *Logger) SetTarget(target Target)

Set the target used by the logger.

type Option

type Option func(logger *Logger) error
var ConsoleOpt Option = func(logger *Logger) error {
	logger.target = NewFileTarget(os.Stderr)
	return nil
}

Create a console target option to configure the logger.

var SyslogOpt Option = func(logger *Logger) error {
	target, err := NewSyslogTarget()
	if err == nil {
		logger.target = target
	}
	return err
}

Create a syslog option to configure the logger.

func LevelOpt

func LevelOpt(level Level) Option

Create a level option to configure the logger.

func NewLevelOpt

func NewLevelOpt(level string) Option

Create a level option to configure the logger.

func NewTargetOpt

func NewTargetOpt(uri string) Option

Create a target option to configure the logger.

func TargetOpt

func TargetOpt(target Target) Option

Create a target option to configure the logger.

type SyslogTarget

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

Write log messages to syslog.

func NewRemoteTarget

func NewRemoteTarget(network, raddr string) (*SyslogTarget, error)

Create a remote syslog writer.

func NewSyslogTarget

func NewSyslogTarget() (*SyslogTarget, error)

Create a local syslog writer.

func (*SyslogTarget) Close

func (s *SyslogTarget) Close() error

Close the writer.

func (*SyslogTarget) Write

func (s *SyslogTarget) Write(level Level, message string)

Print a log message to the writer.

type Target

type Target interface {
	Write(level Level, message string)
	Close() error
}

Targets are where the logger sends filtered log messages. They can be created by a TargetOpt.

func NewTarget

func NewTarget(uri string) (Target, error)

type Writer added in v1.1.0

type Writer struct {
	Level  Level
	Logger *Logger
}

Writer is an io.Writer which logs to Logger at the given Level.

func NewWriter added in v1.1.0

func NewWriter(level Level, logger *Logger) *Writer

NewWriter creates a new Writer.

func (*Writer) Write added in v1.1.0

func (w *Writer) Write(msg []byte) (int, error)

Write msg to the logger.

Jump to

Keyboard shortcuts

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