log

package
v0.0.0-...-d4f462a Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2015 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package log contains NullLogger, StdLogger and the Logger interface.

Logging

Interface Logger is used all over the place and there are no other dependencies. Default Logger is a null logger. You must take care to implement a logger which is also thread safe.

Convention: Because recording a human-meaningful message is common and good practice, the first argument to every logging method is the value to the *implicit* key 'msg'. You may supply any additional context as a set of key/value pairs to the logging function.

To initialize your own logger you must somewhere set the logging object to the util/log package.

import "github.com/corestoreio/csfw/utils/log"

func init() {
	log.Set(NewMyCustomLogger())
}

Level guards exists to avoid the cost of building arguments. Get in the habit of using guards.

import "github.com/corestoreio/csfw/utils/log"

if log.IsDebug() {
	log.Debug("some message", "key1", expensive())
}

Standardizes on key-value pair argument sequence:

import "github.com/corestoreio/csfw/utils/log"

log.Debug("message from inside Fn()", "key1", value1, "key2", value2)

// instead of this
log.WithFields(logrus.Fields{"m": "pkg", "key1": value1, "key2": value2}).Debug("inside fn()")

Please consider the key-value pairs when implementing your own logger.

Recommended Loggers are https://github.com/mgutz/logxi and https://github.com/Sirupsen/logrus and https://github.com/inconshreveable/log15

Standard Logger

CoreStore provides a leveled logger based on Go's standard library without any dependencies. This StdLogger obeys to the interface Logger of this package.

import "github.com/corestoreio/csfw/utils/log"

func init() {
	log.Set(log.NewStdLogger())
}

log.NewStdLogger() accepts a wide range of optional arguments. Please see the functions Std*Option().

Index

Constants

View Source
const (
	StdLevelFatal int = iota + 1
	StdLevelError
	StdLevelWarn
	StdLevelInfo
	StdLevelDebug
	StdLevelTrace
)

Variables

View Source
var AssignmentChar = ": "

The assignment character between key-value pairs

View Source
var (
	ErrLoggerSet = errors.New("Logger already initialized")
)
View Source
var Separator = " "

Separator is the separator to use between key value pairs

Functions

func Debug

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

func Error

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

func Fatal

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

func Info

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

func IsDebug

func IsDebug() bool

func IsInfo

func IsInfo() bool

func IsTrace

func IsTrace() bool

func IsWarn

func IsWarn() bool

func Log

func Log(level int, msg string, args []interface{})

func Set

func Set(l Logger)

Set sets your preferred Logger to be used in CoreStore. Default Logger is a null-logger. Panics if called twice.

func SetLevel

func SetLevel(l int)

func SetNull

func SetNull()

SetNullLogger resets the logger to the null logger aka. black hole.

func Trace

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

func Warn

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

Types

type Logger

type Logger interface {
	// New returns a new Logger that has this logger's context plus the given context
	New(ctx ...interface{}) Logger

	Trace(msg string, args ...interface{})
	Debug(msg string, args ...interface{})
	Info(msg string, args ...interface{})
	Warn(msg string, args ...interface{})

	// Error logs an error entry. Returns the first argument as an error OR
	// if the 2nd index of args (that is args[1] ;-) ) contains the error
	// then that error will be returned.
	Error(msg string, args ...interface{}) error
	Fatal(msg string, args ...interface{})
	Log(level int, msg string, args []interface{})

	SetLevel(int)
	IsTrace() bool
	IsDebug() bool
	IsInfo() bool
	IsWarn() bool
}

Logger defines the minimum requirements for logging. See doc.go for more details. Interface may be extended ...

type NullLogger

type NullLogger struct{}

NullLogger is the default logger for this package.

func (*NullLogger) Debug

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

Debug logs a debug entry.

func (*NullLogger) Error

func (l *NullLogger) Error(msg string, args ...interface{}) error

Error logs an error entry. Returns the first argument as an error OR if the 2nd index of args (that is args[1] ;-) ) contains the error then that error will be returned.

func (*NullLogger) Fatal

func (l *NullLogger) Fatal(msg string, args ...interface{})

Fatal logs a fatal entry then panics.

func (*NullLogger) Info

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

Info logs an info entry.

func (*NullLogger) IsDebug

func (l *NullLogger) IsDebug() bool

IsDebug determines if this logger logs a debug statement.

func (*NullLogger) IsInfo

func (l *NullLogger) IsInfo() bool

IsInfo determines if this logger logs an info statement.

func (*NullLogger) IsTrace

func (l *NullLogger) IsTrace() bool

IsTrace determines if this logger logs a trace statement.

func (*NullLogger) IsWarn

func (l *NullLogger) IsWarn() bool

IsWarn determines if this logger logs a warning statement.

func (*NullLogger) Log

func (l *NullLogger) Log(level int, msg string, args []interface{})

Log logs a leveled entry.

func (*NullLogger) New

func (l *NullLogger) New(ctx ...interface{}) Logger

New returns a new Logger that has this logger's context plus the given context

func (*NullLogger) SetLevel

func (l *NullLogger) SetLevel(level int)

SetLevel sets the level of this logger.

func (*NullLogger) Trace

func (l *NullLogger) Trace(msg string, args ...interface{})

Trace logs a trace entry.

func (*NullLogger) Warn

func (l *NullLogger) Warn(msg string, args ...interface{})

Warn logs a warn entry.

type StdLogger

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

StdLogger implements logging with Go's standard library

func NewStdLogger

func NewStdLogger(opts ...StdOption) *StdLogger

NewStdLogger creates a new logger with 6 different sub loggers. You can use option functions to modify each logger independently. Default output goes to Stderr.

func (*StdLogger) Debug

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

Debug logs a debug entry.

func (*StdLogger) Error

func (l *StdLogger) Error(msg string, args ...interface{}) error

Error logs an error entry. Returns the first argument as an error OR if the 2nd index of args (that is args[1] ;-) ) contains the error then that error will be returned.

func (*StdLogger) Fatal

func (l *StdLogger) Fatal(msg string, args ...interface{})

Fatal logs a fatal entry then panics.

func (*StdLogger) Info

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

Info logs an info entry.

func (*StdLogger) IsDebug

func (l *StdLogger) IsDebug() bool

IsDebug determines if this logger logs a debug statement.

func (*StdLogger) IsInfo

func (l *StdLogger) IsInfo() bool

IsInfo determines if this logger logs an info statement.

func (*StdLogger) IsTrace

func (l *StdLogger) IsTrace() bool

IsTrace determines if this logger logs a trace statement.

func (*StdLogger) IsWarn

func (l *StdLogger) IsWarn() bool

IsWarn determines if this logger logs a warning statement.

func (*StdLogger) Log

func (l *StdLogger) Log(level int, msg string, args []interface{})

Log logs a leveled entry.

func (*StdLogger) New

func (l *StdLogger) New(iOpts ...interface{}) Logger

New returns a new Logger that has this logger's context plus the given context This function panics if an argument is not of type StdOption.

func (*StdLogger) SetLevel

func (l *StdLogger) SetLevel(level int)

SetLevel sets the level of this logger.

func (*StdLogger) Trace

func (l *StdLogger) Trace(msg string, args ...interface{})

Trace logs a trace entry.

func (*StdLogger) Warn

func (l *StdLogger) Warn(msg string, args ...interface{})

Warn logs a warn entry.

type StdOption

type StdOption func(*StdLogger)

StdOption function to modify a logger

func StdDebugOption

func StdDebugOption(out io.Writer, prefix string, flag int) StdOption

StdDebugOption applies options for debug logging

func StdErrorOption

func StdErrorOption(out io.Writer, prefix string, flag int) StdOption

StdErrorOption applies options for error logging

func StdFatalOption

func StdFatalOption(out io.Writer, prefix string, flag int) StdOption

StdFatalOption applies options for fatal logging

func StdGlobalFlagOption

func StdGlobalFlagOption(f int) StdOption

StdWriterOption sets the global flag for all loggers. This global flag can be overwritten by individual level options. Please see http://golang.org/pkg/log/#pkg-constants

func StdGlobalWriterOption

func StdGlobalWriterOption(w io.Writer) StdOption

StdGlobalWriterOption sets the global writer for all loggers. This global writer can be overwritten by individual level options.

func StdInfoOption

func StdInfoOption(out io.Writer, prefix string, flag int) StdOption

StdInfoOption applies options for info logging

func StdLevelOption

func StdLevelOption(level int) StdOption

StdLevelOption sets the log level. See constants Level*

func StdTraceOption

func StdTraceOption(out io.Writer, prefix string, flag int) StdOption

StdTraceOption applies options for trace logging

func StdWarnOption

func StdWarnOption(out io.Writer, prefix string, flag int) StdOption

StdWarnOption applies options for warn logging

Jump to

Keyboard shortcuts

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