Documentation
¶
Index ¶
- Variables
- type Format
- type Hook
- type Logger
- func (l *Logger) AddHook(h Hook) *Logger
- func (l *Logger) Context() interface{}
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Println(v ...interface{})
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) UpdateContext(update Update) error
- func (l *Logger) WithContext(v interface{}) *Logger
- func (l *Logger) WithFormatter(f Format) *Logger
- func (l *Logger) Write(p []byte) (n int, err error)
- type Update
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrLogger is the general logger error ErrLogger = errors.New("logger error") // ErrSkip tells the logger to skip a log entry ErrSkip = errors.New("skip") )
Functions ¶
This section is empty.
Types ¶
type Hook ¶
Hook is called before each write to update the context or message
If err is ErrSkip, just return without writing anything
Example (Context) ¶
buf := bytes.Buffer{}
context := Context{}
logger := New(&buf).
WithContext(context).
WithFormatter(func(v interface{}, msg string) (string, error) {
context, ok := v.(Context)
if !ok {
return "", errors.New("not Context type")
}
return fmt.Sprintf("%s %d %s", context.S, context.N, msg), nil
})
_ = logger.UpdateContext(updateContext(
func(context Context) Context {
context.S = "A"
context.N = 1
return context
}))
logger.Print("mylog")
fmt.Print(buf.String())
Output: A 1 mylog
Example (ContextUpdateTime) ¶
var context time.Time = time.Date(2021, 2, 1, 12, 30, 0, 0, time.UTC)
hookUpdateTime := func(v interface{}, msg string) (interface{}, string, error) {
context := time.Date(2022, 2, 1, 12, 30, 0, 0, time.UTC)
return context, msg, nil
}
formatter := func(v interface{}, msg string) (string, error) {
context, ok := v.(time.Time)
if !ok {
return "", fmt.Errorf("%T: not time type", v)
}
return fmt.Sprintf("%s: %s", context, msg), nil
}
buf := bytes.Buffer{}
logger := New(&buf).
WithContext(context).
WithFormatter(formatter).
AddHook(hookUpdateTime)
logger.Println("message")
fmt.Println(buf.String())
Output: 2022-02-01 12:30:00 +0000 UTC: message
Example (MultiFormatter) ¶
Write to the io.Writer of the logger with no format and to console using a hook
var context string
// To replace with os.Stdout
var console bytes.Buffer
hookWriteConsole := func(v interface{}, msg string) (interface{}, string, error) {
context, ok := v.(string)
if !ok {
return nil, "", fmt.Errorf("%T: not string type", v)
}
fmt.Fprintf(&console, "%s: %s", context, msg)
return v, msg, nil
}
logger := New(io.Discard).
WithContext(context).
AddHook(hookWriteConsole)
_ = logger.UpdateContext(func(v interface{}) (interface{}, error) {
context = "mycontext"
return context, nil
})
logger.Println("message")
fmt.Println(console.String())
Output: mycontext: message
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func (*Logger) AddHook ¶
AddHook adds a hook function to the list of hooks of the logger.
Hooks are called in the added order
func (*Logger) Print ¶
func (l *Logger) Print(v ...interface{})
Print uses fmt.Print to write to the logger
func (*Logger) Println ¶
func (l *Logger) Println(v ...interface{})
Println uses fmt.Println to write to the logger
Example ¶
buf := bytes.Buffer{}
logger := New(&buf).
WithFormatter(func(v interface{}, msg string) (string, error) {
return msg, nil
})
logger.Println("mylog1")
logger.Println("mylog2")
logger.Println("mylog3")
logger.Println("mylog4")
fmt.Print(&buf)
Output: mylog1 mylog2 mylog3 mylog4
func (*Logger) SetOutput ¶ added in v1.0.13
SetOutput changes the output writer of the logger
Example ¶
logger := New(io.Discard).
WithFormatter(func(v interface{}, msg string) (string, error) {
return msg, nil
})
logger.Println("mylog1")
buf := bytes.Buffer{}
logger.SetOutput(&buf)
logger.Println("mylog2")
fmt.Print(&buf)
Output: mylog2
func (*Logger) UpdateContext ¶
UpdateContext updates the logger context with the update function
func (*Logger) WithContext ¶
WithContext adds a context to the logger
func (*Logger) WithFormatter ¶
WithFormatter adds a formatter function to the logger
Click to show internal directories.
Click to hide internal directories.