Documentation
¶
Index ¶
- Constants
- Variables
- func SetLogger(ctx context.Context, inst Logger) (context.Context, error)
- func SetLoggerIfAbsent(ctx context.Context, inst Logger) (context.Context, error)
- func WithLogger(ctx context.Context, fn func(Logger))
- type Fields
- type Level
- type Logger
- func FromLogger(ctx context.Context) Logger
- func GetLogger(ctx context.Context) (Logger, error)
- func MustGetLogger(ctx context.Context) Logger
- func NewDefaultLogger() Logger
- func NewZapLogger() Logger
- func NewZapLoggerWithFile(logfile string) Logger
- func TryGetLogger(ctx context.Context) (Logger, bool)
Constants ¶
const ContextKeyLogger = "gohacks/logger@v1"
Key used to store the instance in the context's user value.
Variables ¶
var ErrValueNotLogger = errors.Base("value is not Logger")
Signalled if the instance associated with the context key is not of type Logger.
Functions ¶
func SetLoggerIfAbsent ¶ added in v1.0.6
SetLoggerIfAbsent sets only if not already present.
func WithLogger ¶ added in v1.0.6
WithLogger calls fn with the instance or fallback.
Types ¶
type Logger ¶
type Logger interface {
// Sync a log file to disk, if available.
Sync() error
// Set whether the logger prints in human-readable 'debug' output or
// machine-readable JSON format.
SetDebug(bool)
// Set the log file to which the logger will write.
SetLogFile(string)
// Set sampling options.
//
// This is only used by the Zap logger.
SetSampling(initial, threshold int)
// Set logger level.
SetLevel(Level)
// Log a Go error.
GoError(error, ...any)
// Log a debug message.
Debug(string, ...any)
// Log an error message.
Error(string, ...any)
// Log a warning message.
Warn(string, ...any)
// Log an informational message.
Info(string, ...any)
// Log a fatal error condition and exit.
Fatal(string, ...any)
// Log a panic condition and exit.
Panic(string, ...any)
// Log a debug message with printf formatting.
Debugf(string, ...any)
// Log an error message with printf formatting.
Errorf(string, ...any)
// Log a warning message with printf formatting.
Warnf(string, ...any)
// Log an informational message with printf formatting.
Infof(string, ...any)
// Log a fatal message with printf formatting.
Fatalf(string, ...any)
// Log a panic message with printf formatting.
Panicf(string, ...any)
// Encapsulate user-specified metadata fields.
WithFields(Fields) Logger
}
Logging structure.
To use,
1) Create a logger:
```go
lgr := logger.NewLogger()
```
2) Do things with it:
```go
lgr.Warn("Not enough coffee!")
lgr.Info("Water is heating up.")
// and so on.
```
If an empty string is passed to `NewLogger`, then the log facility will display messages on standard output.
func FromLogger ¶ added in v1.0.6
FromLogger returns the instance or the fallback.
func GetLogger ¶ added in v1.0.6
Get the logger from the given context.
Will return ErrValueNotLogger if the value in the context is not of type Logger.
func MustGetLogger ¶ added in v1.0.6
Attempt to get the instance from the given context. Panics if the operation fails.
func NewZapLoggerWithFile ¶ added in v0.4.0
Create a new logger with the given log file.