You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@5nn Golog supports message hijacking, so you can output with your own format like @celsosz , a good example of this is the below (you can modify it to your specific needs):
import (
"runtime""fmt""github.com/kataras/golog"
)
// [...]golog.Handle(func(l*golog.Log) bool { /* or myLogger:= golog.New(...); myLogger.Handle or golog.Child(...).Handle */prefix:=golog.GetTextForLevel(l.Level, true)
pc, fn, line, _:=runtime.Caller(6)
message:=fmt.Sprintf("%s line %d (%s) (%s) %s: %s",
prefix, line, runtime.FuncForPC(pc).Name(), fn, l.FormatTime(), l.Message)
ifl.NewLine {
message+="\n"
}
fmt.Print(message)
returntrue
})
// [...]golog.Warnf("This is a warning message")
It's not a good idea to hard code the stack length pc, fn, line, _ := runtime.Caller(6), because the it is not fixed, for example
package main
import (
"runtime""fmt""github.com/kataras/golog"
)
funcmain () {
golog.Handle(func(l*golog.Log) bool {
prefix:=golog.GetTextForLevel(l.Level, true)
pc, fn, line, _:=runtime.Caller(7)
message:=fmt.Sprintf("%s line %d (%s) (%s) %s: %s",
prefix, line, runtime.FuncForPC(pc).Name(), fn, l.FormatTime(), l.Message)
ifl.NewLine {
message+="\n"
}
fmt.Print(message)
returntrue
})
golog.Warnf("This is a warning message")
golog.Warn("This is a warning message")
}
Output is
[WARN] line 21 (main.main) (/tmp/main.go) 2019/05/29 08:18: This is a warning message
[WARN] line 200 (runtime.main) (/usr/local/Cellar/go/1.12.1/libexec/src/runtime/proc.go) 2019/05/29 08:18: This is a warning message
Activity
kataras commentedon Jan 8, 2018
@5nn Golog supports message hijacking, so you can output with your own format like @celsosz , a good example of this is the below (you can modify it to your specific needs):
virusdefender commentedon May 29, 2019
It's not a good idea to hard code the stack length
pc, fn, line, _ := runtime.Caller(6), because the it is not fixed, for exampleOutput is
A better way is to manually check the stack frame
Output is