Documentation ¶
Overview ¶
Package log is not another logger but a simple, clean and potentially dependency free logging facade. It combines ideas from Dave Cheney (https://dave.cheney.net/2015/11/05/lets-talk-about-logging) and Rob Pike (https://github.com/golang/glog) to provide a simple yet efficient structured logging API.
The default logger is created at package initialization time and if your application is executed from the IDE it uses the simple.PrintColored and otherwise simple.PrintStructured logger. Note, that the simple loggers use the standard library log.Print function and disables their time printing (log.SetFlags(0)).
Index ¶
- Constants
- func IsDevelopment() bool
- func Println(fields ...interface{})
- func SetDefault(f func(fields ...interface{}))
- func V(key string, val interface{}) field.DefaultField
- func WithFunc(next func(fields ...interface{}), more ...func() interface{}) func(fields ...interface{})
- func WithLogger(ctx context.Context, logger Logger) context.Context
- type Field
- type Logger
- type LoggerFunc
Constants ¶
const Debug = false
Debug is a build tag determined at build time, so that the compiler can remove dead code.
Variables ¶
This section is empty.
Functions ¶
func IsDevelopment ¶
func IsDevelopment() bool
IsDevelopment evaluates the following sources:
- if any _INTELLIJ_* environment variable is defined, returns true
- XPC_SERVICE_NAME contains goland
- if APP_ENV or NODE_ENV environment variable is set to 'production' returns false, otherwise if specified at all returns true
- if any VSCODE_* environment variable is defined, returns true
- otherwise returns false
func Println ¶
func Println(fields ...interface{})
Println delegates directly to the default configured logger. See also Logger, NewLogger and SetDefault.
func SetDefault ¶
func SetDefault(f func(fields ...interface{}))
SetDefault just sets a delegate for NewLogger.
func V ¶
func V(key string, val interface{}) field.DefaultField
V is just a shortcut for a field construction. V is a short version of Value.
Types ¶
type Field ¶
type Field interface { // Key returns the unique key of this structured Field. Key() string // Value returns an arbitrary message, which itself may be structured. Value() interface{} }
Field is an interface to an explicit key/value tuple, to be clear about structured information.
type Logger ¶
type Logger interface { // Println processes and prints the arguments as fields. The interpretation and formatting depends on the // concrete implementation and may range from fmt.Println over log.Println to a full structured logger. // Implementations are encouraged to type-switch on each field. Println(fields ...interface{}) }
Logger provides the abstraction for logging. It is kept as simple as possible and to avoid recursive dependency cycles by using other interfaces or concrete types. It deliberately breaks with the conventional logger APIs, due to the following considerations:
- there are verbose developer specific logs which are not important or even bad for life system. You mostly even want that there is no cost for the log parameter propagation, which would cause even more harm like escaping values and heap-pressure, even if disabled. The only way to avoid this, is a guarded compile time constant evaluation.
- anything else which is so important, that a developer is not sure to turn off in production, must not be guarded. Instead it is up to the administrator or software engineer to filter through the log in a structured way. Any error which does not kill your application, is just another kind of information.
- any error which does break your post-variants and you cannot continue, should be logged (again, still info) either bail out with a runtime panic or an explicit os.Exit.
func FromContext ¶
FromContext returns the contained logger or a new root logger. Context may be nil.
func NewLogger ¶
func NewLogger(fields ...interface{}) Logger
NewLogger uses the factory to create a new logger. The given fields are prepended.
func WithFields ¶
WithFields just prepends the given fields before the actual logger field parameters will be passed.
type LoggerFunc ¶
type LoggerFunc func(fields ...interface{})
LoggerFunc allows a function to become a Logger.
func (LoggerFunc) Println ¶
func (f LoggerFunc) Println(fields ...interface{})
Println prints the fields by delegating to the function itself.
Directories ¶
Path | Synopsis |
---|---|
Package ecs provides helpers and common field functions for the Elastic Common Schema, see also https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html
|
Package ecs provides helpers and common field functions for the Elastic Common Schema, see also https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html |
Package simple provides some naive logging implementations which just do some basic formatting and forward the data to the go standard library log.Print infrastructure.
|
Package simple provides some naive logging implementations which just do some basic formatting and forward the data to the go standard library log.Print infrastructure. |