logging

package
v0.0.0-...-8b786c7 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2015 License: BSD-2-Clause Imports: 10 Imported by: 64

Documentation

Overview

A simple logging module that mimics the behavior of Python's logging module.

All it does basically is wrap Go's logger with nice multi-level logging calls, and allows you to set the logging level of your app in runtime.

Logging is done just like calling fmt.Sprintf:

logging.Info("This object is %s and that is %s", obj, that)

example output:

2013/05/07 01:20:26 INFO @ db.go:528: Registering plugin REPLICATION
2013/05/07 01:20:26 INFO @ db.go:562: Registered 6 plugins and 22 commands
2013/05/07 01:20:26 INFO @ slave.go:277: Running replication watchdog loop!
2013/05/07 01:20:26 INFO @ redis.go:49: Redis adapter listening on 0.0.0.0:2000
2013/05/07 01:20:26 WARN @ main.go:69: Starting adapter...
2013/05/07 01:20:26 INFO @ db.go:966: Finished dump load. Loaded 2 objects from dump
2013/05/07 01:22:26 INFO @ db.go:329: Checking persistence... 0 changes since 2m0.000297531s
2013/05/07 01:22:26 INFO @ db.go:337: No need to save the db. no changes...
2013/05/07 01:22:26 DEBUG @ db.go:341: Sleeping for 2m0s

Index

Constants

View Source
const (
	DEBUG    = 1
	INFO     = 2
	WARNING  = 4
	WARN     = 4
	ERROR    = 8
	NOTICE   = 16 //notice is like info but for really important stuff ;)
	CRITICAL = 32
	QUIET    = ERROR | NOTICE | CRITICAL               //setting for errors only
	NORMAL   = INFO | WARN | ERROR | NOTICE | CRITICAL // default setting - all besides debug
	ALL      = 255
	NOTHING  = 0
)

Variables

View Source
var LevlelsByName = map[string]int{
	"DEBUG":    DEBUG,
	"INFO":     INFO,
	"WARNING":  WARN,
	"WARN":     WARN,
	"ERROR":    ERROR,
	"NOTICE":   NOTICE,
	"CRITICAL": CRITICAL,
	"QUIET":    QUIET,
	"NORMAL":   NORMAL,
	"ALL":      ALL,
	"NOTHING":  NOTHING,
}

Functions

func BridgeStdLog

func BridgeStdLog(level int)

BridgeStdLog bridges all messages written using the standard library's log.Print* and makes them output through this logger, at a given level.

func Critical

func Critical(msg string, args ...interface{})

Output a CRITICAL level message while showing a stack trace

func Criticalf

func Criticalf(msg string, args ...interface{}) error

Same as critical but also returns an error object with the message regardless of logging level

func Debug

func Debug(msg string, args ...interface{})

Output debug logging messages

func Error

func Error(msg string, args ...interface{})

Output ERROR level messages

func Errorf

func Errorf(msg string, args ...interface{}) error

Same as Error() but also returns a new formatted error object with the message regardless of logging level

func Info

func Info(msg string, args ...interface{})

output INFO level messages

func Notice

func Notice(msg string, args ...interface{})

Output NOTICE level messages

func Panic

func Panic(msg string, args ...interface{})

Raise a PANIC while writing the stack trace to the log

func SetHandler

func SetHandler(h LoggingHandler)

Set the current handler of the library. We currently support one handler, but it might be nice to have more

func SetLevel

func SetLevel(l int)

Set the logging level.

Contrary to Python that specifies a minimal level, this logger is set with a bit mask of active levels.

e.g. for INFO and ERROR use:

SetLevel(logging.INFO | logging.ERROR)

For everything but debug and info use:

SetLevel(logging.ALL &^ (logging.INFO | logging.DEBUG))

func SetMinimalLevel

func SetMinimalLevel(l int)

Set a minimal level for loggin, setting all levels higher than this level as well.

the severity order is DEBUG, INFO, WARNING, ERROR, CRITICAL

func SetMinimalLevelByName

func SetMinimalLevelByName(l string) error

Set minimal level by string, useful for config files and command line arguments. Case insensitive.

Possible level names are DEBUG, INFO, WARNING, ERROR, NOTICE, CRITICAL

func SetOutput

func SetOutput(w io.Writer)

Set the output writer. for now it just wraps log.SetOutput()

func Warning

func Warning(msg string, args ...interface{})

Output WARNING level messages

func Warningf

func Warningf(msg string, args ...interface{}) error

Same as Warning() but return a formatted error object, regardless of logging level

Types

type Formatter

type Formatter interface {
	Format(ctx *MessageContext, message string, args ...interface{}) string
}

A formatting interface- it is responsible of taking the arguments and composing a message

var DefaultFormatter Formatter = &SimpleFormatter{
	FormatString: "[%[1]s %[2]s, %[3]s:%[4]d] %[5]s",
}

type LoggingHandler

type LoggingHandler interface {
	SetFormatter(Formatter)
	Emit(ctx *MessageContext, message string, args ...interface{}) error
}

a pluggable logger interface

func CurrentHandler

func CurrentHandler() LoggingHandler

type MessageContext

type MessageContext struct {
	Level     string
	File      string
	Line      int
	TimeStamp time.Time
}

type SimpleFormatter

type SimpleFormatter struct {
	FormatString string
}

func (*SimpleFormatter) Format

func (f *SimpleFormatter) Format(ctx *MessageContext, message string, args ...interface{}) string

Directories

Path Synopsis
This is a Scribe logging handler, that emits its messages to a scribe server
This is a Scribe logging handler, that emits its messages to a scribe server

Jump to

Keyboard shortcuts

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