Documentation ¶
Overview ¶
This package provide logger abstraction and default implementation. This package also provide global logger which use the default implementation by defaults. Libraries might use the global logger to log their internal event or debuging purpose, libraries that use global logger should use Debug level to avoid cluter the user logs. SimpleLogger which is the default implementation should be replaced by more reliable logging library implementation such as `zaplog`, you can set global logger by calling: log.SetLogger(logger)
Index ¶
- func Debug(msg string, optfields ...LogField)
- func Error(msg string, err error, optfields ...LogField)
- func Fatal(msg string, err error, optfields ...LogField)
- func Info(msg string, optfields ...LogField)
- func SetLogger(logger Logger)
- func Warn(msg string, optfields ...LogField)
- type LogField
- type LogLevel
- type Logger
- type NoOpLogger
- func (l *NoOpLogger) Debug(msg string, optfields ...LogField)
- func (l *NoOpLogger) Error(msg string, err error, optfields ...LogField)
- func (l *NoOpLogger) Fatal(msg string, err error, optfields ...LogField)
- func (l *NoOpLogger) Info(msg string, optfields ...LogField)
- func (l *NoOpLogger) Warn(msg string, optfields ...LogField)
- func (l *NoOpLogger) WithContext(ctx context.Context) Logger
- type SimpleLogger
- func (l *SimpleLogger) Debug(msg string, optfields ...LogField)
- func (l *SimpleLogger) Error(msg string, err error, optfields ...LogField)
- func (l *SimpleLogger) Fatal(msg string, err error, optfields ...LogField)
- func (l *SimpleLogger) Info(msg string, optfields ...LogField)
- func (l *SimpleLogger) Warn(msg string, optfields ...LogField)
- func (l *SimpleLogger) WithContext(ctx context.Context) Logger
- type WriterFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
Debug logs a message at DebugLevel using global logger. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func Error ¶
Error logs a message at ErrorLevel using global logger, put the passed error in "error" field. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func Fatal ¶
Fatal logs a message at FatalLevel using global logger, then calls os.Exit(1). This should only be use with extra care, ideally Fatal should only be used in main where the apps encountered an error and have noway to continue. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
Types ¶
type Logger ¶
type Logger interface { // Fatal logs a message at FatalLevel, then calls os.Exit(1). This should only be use with extra care, ideally Fatal // should only be used in main where the apps encountered an error and have noway to continue. // optfields is optional, when supplied it will be added as new field using // Key as field name, and Value as it's value. Fatal(msg string, err error, optfields ...LogField) // Error logs a message at ErrorLevel, put the passed error in "error" field. // optfields is optional, when supplied it will be added as new field using // Key as field name, and Value as it's value. Error(msg string, err error, optfields ...LogField) // Warn logs a message at WarnLevel. // optfields is optional, when supplied it will be added as new field using // Key as field name, and Value as it's value. Warn(msg string, optfields ...LogField) // Info logs a message at InfoLevel. // optfields is optional, when supplied it will be added as new field using // Key as field name, and Value as it's value. Info(msg string, optfields ...LogField) // Debug logs a message at DebugLevel. // optfields is optional, when supplied it will be added as new field using // Key as field name, and Value as it's value. Debug(msg string, optfields ...LogField) // WithContext return Logger instance that will use passed context to log additional info, // such as opentelemetry's SpanID and TraceID if applicable. WithContext(ctx context.Context) Logger }
Logger log message to underlying logger. Any logging driver should implements Logger interface. The optfield should be optional, and when it's not nil/empty it should be logged as structured key/value.
func GetLogger ¶
func GetLogger() Logger
GetLogger get global logger, by default global logger use SimpleLogger and use standard log default writer (log.Default().Writer()) as it's writer. Use SetLogger to set global logger.
func WithContext ¶
WithContext return Logger instance that will use passed context to log additional info, such as opentelemetry's SpanID and TraceID if applicable.
type NoOpLogger ¶
type NoOpLogger struct{}
NoOpLogger will not writes out logs to any output. All NoOpLogger method basically doesn't do anything except for Fatal, it will simply call os.Exit(1).
func (*NoOpLogger) Debug ¶
func (l *NoOpLogger) Debug(msg string, optfields ...LogField)
func (*NoOpLogger) Fatal ¶
func (l *NoOpLogger) Fatal(msg string, err error, optfields ...LogField)
Fatal call os.Exit(1)
func (*NoOpLogger) Info ¶
func (l *NoOpLogger) Info(msg string, optfields ...LogField)
func (*NoOpLogger) Warn ¶
func (l *NoOpLogger) Warn(msg string, optfields ...LogField)
func (*NoOpLogger) WithContext ¶
func (l *NoOpLogger) WithContext(ctx context.Context) Logger
type SimpleLogger ¶
type SimpleLogger struct {
// contains filtered or unexported fields
}
func NewSimpleLogger ¶
func NewSimpleLogger(w io.Writer, lv LogLevel) *SimpleLogger
func (*SimpleLogger) Debug ¶
func (l *SimpleLogger) Debug(msg string, optfields ...LogField)
func (*SimpleLogger) Error ¶
func (l *SimpleLogger) Error(msg string, err error, optfields ...LogField)
func (*SimpleLogger) Fatal ¶
func (l *SimpleLogger) Fatal(msg string, err error, optfields ...LogField)
Fatal call os.Exit(1)
func (*SimpleLogger) Info ¶
func (l *SimpleLogger) Info(msg string, optfields ...LogField)
func (*SimpleLogger) Warn ¶
func (l *SimpleLogger) Warn(msg string, optfields ...LogField)
func (*SimpleLogger) WithContext ¶
func (l *SimpleLogger) WithContext(ctx context.Context) Logger
type WriterFunc ¶
WriterFunc takes Logger's log method signature to implement io.Writer, this is useful when you want to use Logger as standard log's output. ie. stdlog.SetOutput(log.WriterFunc(logger.Debug))