Documentation ¶
Index ¶
- func Context(ctx context.Context, l Logger) context.Context
- func LogWriter(logger log.Logger) io.Writer
- func PrettyFormat(values ...interface{}) (r string)
- func SetStdLogOutput(logger Logger)
- func WithLogger(logger Logger) func(http.Handler) http.Handler
- type GormLogger
- type Logger
- func (l Logger) Crit() log.Logger
- func (l Logger) Debug() log.Logger
- func (l Logger) Error() log.Logger
- func (l Logger) Info() log.Logger
- func (l Logger) Warn() log.Logger
- func (l Logger) With(keysvals ...interface{}) Logger
- func (l Logger) WithError(err error) log.Logger
- func (l Logger) WrapError(err error) log.Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PrettyFormat ¶
func PrettyFormat(values ...interface{}) (r string)
PrettyFormat accepts log values and returns pretty output string
func SetStdLogOutput ¶
func SetStdLogOutput(logger Logger)
SetStdLogOutput redirect go standard log into this logger
Types ¶
type GormLogger ¶
type GormLogger struct {
Logger
}
func (GormLogger) Print ¶
func (l GormLogger) Print(values ...interface{})
type Logger ¶
func ForceContext ¶
ForceContext extracts a Logger from a (possibly nil) context, or returns a log.Default().
func FromContext ¶
FromContext extracts a Logger from a (possibly nil) context.
func NewNopLogger ¶
func NewNopLogger() Logger
NewNopLogger returns a logger that doesn't do anything. This just a wrapper of `go-kit/log.nopLogger`.
func NewTestLogger ¶
func NewTestLogger() Logger
NewTestLogger returns a logger that won't log timestamps or caller, to make output stable for use in tests.
func Start ¶
Example (Log) ¶
log.Start will try to get logger from context, and log from there, every log with the instance will print duration=301.356ms field with the log, the time the log time duration since log.Start
Example output:
``` 15:16:20.34 hello app=testapp method=TestLogger store_id=100 duration=0.001ms 15:16:20.64 app=testapp method=TestLogger store_id=100 request_id=123 duration=300.542ms 15:16:20.64 debug app=testapp method=TestLogger store_id=100 duration=300.616ms 15:16:20.64 info app=testapp method=TestLogger store_id=100 duration=300.635ms 15:16:20.64 WrapError error: WrapError error app=testapp method=TestLogger store_id=100 duration=300.746ms ```
package main import ( "context" "errors" "time" "github.com/theplant/appkit/log" ) var testContext = log.Context(context.TODO(), log.Default().With("app", "testapp")) func main() { l := log.Start(testContext).With("method", "TestLogger", "store_id", 100) l.Log("msg", "hello") time.Sleep(100 * time.Millisecond) l.With("request_id", "123").Log() l.Debug().Log("msg", "debug") time.Sleep(200 * time.Millisecond) l.Info().Log("msg", "info") l.WrapError(errors.New("WrapError error")).Log() l.WithError(errors.New("WithError error")).Log() }
Output: