Documentation
¶
Index ¶
- type Encoding
- type Level
- type Logger
- type LoggerImp
- func (l *LoggerImp) DPanic(msg string, keysAndValues ...interface{})
- func (l *LoggerImp) Debug(msg string, keysAndValues ...interface{})
- func (l *LoggerImp) Error(msg string, keysAndValues ...interface{})
- func (l *LoggerImp) Fatal(msg string, keysAndValues ...interface{})
- func (l *LoggerImp) Info(msg string, keysAndValues ...interface{})
- func (l *LoggerImp) Panic(msg string, keysAndValues ...interface{})
- func (l *LoggerImp) Sync()
- func (l *LoggerImp) Warn(msg string, keysAndValues ...interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Encoding ¶
type Encoding uint8
Encoding specifies the logging output formatter: console or json
type Level ¶
type Level uint8
Level is the logging priority. Higher levels are more important.
const ( // DebugLevel are typically used on development environemt or for debug purpose. // Is more voluminous and are usually disabled in production. DebugLevel Level = iota // InfoLevel is the default logging priority. InfoLevel // WarnLevel logs are more important than Info, but not critical WarnLevel // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. ErrorLevel // DPanicLevel logs are particularly important errors. In development the // logger panics after writing the message. DPanicLevel // PanicLevel logs a message, then panics. PanicLevel // FatalLevel logs a message, then calls os.Exit(1). FatalLevel )
type Logger ¶
type Logger interface { // Debug logs a message with some additional context. // The variadic key-value pairs are treated as they are in With. // Assumes log severity as 'DEBUG' Debug(msg string, keyAndValues ...interface{}) // Info logs a message with some additional context. // The variadic key-value pairs are treated as they are in With. // Assumes log severity as 'INFO' Info(msg string, keysAndValues ...interface{}) // Warn logs a message with some additional context. // The variadic key-value pairs are treated as they are in With. // Assumes log severity as 'WARNING' Warn(msg string, keyAndValues ...interface{}) // Error logs a message with some additional context. // The variadic key-value pairs are treated as they are in With. // Assumes log severity as 'ERROR' and includes the stacktrace. Error(msg string, keyAndValues ...interface{}) // DPanic logs a message with some additional context. // In development, the logger then panics. (See DPanicLevel for details.) // The variadic key-value pairs are treated as they are in With. // Assumes log severity as 'CRITICAL' and includes the stacktrace. DPanic(msg string, keysAndValues ...interface{}) // Panic logs a message with some additional context, then panics. // The variadic key-value pairs are treated as they are in With. // Assumes severity as 'ALERT' and includes the stacktrace. Panic(msg string, keysAndValues ...interface{}) // Fatal logs a message with some additional context, then calls os.Exit. // The variadic key-value pairs are treated as they are in With. // Assumes severity as 'EMERGENCY' and includes the stacktrace. Fatal(msg string, keysAndValues ...interface{}) // Sync flushes any buffered log entries. Sync() }
Example ¶
package main import ( "github.com/maykonlf/logger" ) func main() { log := logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DebugLevel) defer log.Sync() log.Debug("my log message") log.Debug("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) log.Info("my log message") log.Info("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) log.Warn("my log message") log.Warn("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) log.Error("my log message") log.Error("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) log.DPanic("my log message") log.DPanic("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) log.Panic("my log message") log.Panic("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) log.Fatal("my log message") log.Fatal("my log message", "key1", "value 1", "key2", 1, "key3", 1.2, "key4", true, "key5", map[string]interface{}{ "key": "value", "another_key": 1, }) }
Output:
func New ¶
func New() Logger
New returns a new logger with json encoding and default info level.
Example ¶
package main import ( "github.com/maykonlf/logger" ) func main() { log := logger.New() defer log.Sync() log.Info("info message") }
Output:
func NewLogger ¶
NewLogger returns a new logger with the given encoding(see Encoding for details) and default info level.
Example ¶
package main import ( "github.com/maykonlf/logger" ) func main() { jsonLogger := logger.NewLogger(logger.JSONEncoding) defer jsonLogger.Sync() jsonLogger.Info("info message") consoleLogger := logger.NewLogger(logger.ConsoleEncoding) defer consoleLogger.Sync() consoleLogger.Info("info message") }
Output:
func NewLoggerWithLevel ¶
NewLoggerWithLevel returns a new logger with the given encoding(see Encoding for details) and level (see Level for details).
Example ¶
package main import ( "github.com/maykonlf/logger" ) func main() { _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DebugLevel) _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.InfoLevel) _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.WarnLevel) _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.ErrorLevel) _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DPanicLevel) _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.PanicLevel) _ = logger.NewLoggerWithLevel(logger.JSONEncoding, logger.FatalLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.DebugLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.InfoLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.WarnLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.ErrorLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.DPanicLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.PanicLevel) _ = logger.NewLoggerWithLevel(logger.ConsoleEncoding, logger.FatalLevel) }
Output:
type LoggerImp ¶
type LoggerImp struct {
// contains filtered or unexported fields
}
func (*LoggerImp) DPanic ¶
DPanic logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.
Assumes log severity as 'CRITICAL' and includes the stacktrace.
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.DPanic("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output:
func (*LoggerImp) Debug ¶
Debug logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
Assumes log severity as 'DEBUG'
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.Debug("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output:
func (*LoggerImp) Error ¶
Error logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
Assumes log severity as 'ERROR' and includes the stacktrace.
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.Error("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output:
func (*LoggerImp) Fatal ¶
Fatal logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.
Assumes severity as 'EMERGENCY' and includes the stacktrace.
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.Fatal("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output:
func (*LoggerImp) Info ¶
Info logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
Assumes log severity as 'INFO'
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.Info("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output:
func (*LoggerImp) Panic ¶
Panic logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.
Assumes severity as 'ALERT' and includes the stacktrace.
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.Panic("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output:
func (*LoggerImp) Sync ¶
func (l *LoggerImp) Sync()
Sync flushes any buffered log entries.
Example ¶
package main import ( "github.com/maykonlf/logger" ) func main() { l := logger.NewLoggerWithLevel(logger.JSONEncoding, logger.DebugLevel) defer l.Sync() l.Info("info message") l.Debug("debug info") }
Output:
func (*LoggerImp) Warn ¶
Warn logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
Assumes log severity as 'WARNING'
Example ¶
package main import ( "github.com/maykonlf/logger" ) var loggerExample logger.Logger func main() { loggerExample.Warn("my log message", "additional_data", "some info", "my_int_field", 1, "my_float_field", 1.2, "my_boolean_field", true, "map_field", map[string]interface{}{ "a_key": "some info", "b_key": 1, "c_key": 1.2, "d_key": false, }) }
Output: