log

package
v0.0.0-...-81c75b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 16 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Context

func Context(ctx context.Context, l Logger) context.Context

Context installs a given Logger in the returned context

func LogWriter

func LogWriter(logger log.Logger) io.Writer

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

func WithLogger

func WithLogger(logger Logger) func(http.Handler) http.Handler

Types

type GormLogger

type GormLogger struct {
	Logger
}

func (GormLogger) Print

func (l GormLogger) Print(values ...interface{})

type Logger

type Logger struct {
	log.Logger
}

func Default

func Default() Logger

func ForceContext

func ForceContext(c context.Context) Logger

ForceContext extracts a Logger from a (possibly nil) context, or returns a log.Default().

func FromContext

func FromContext(c context.Context) (Logger, bool)

FromContext extracts a Logger from a (possibly nil) context.

func Human

func Human() Logger

Human is for create a Logger that print human friendly log

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

func Start(ctx context.Context) Logger
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:

func StartWith

func StartWith(ctx context.Context, start time.Time) Logger

func (Logger) Crit

func (l Logger) Crit() log.Logger

func (Logger) Debug

func (l Logger) Debug() log.Logger

func (Logger) Error

func (l Logger) Error() log.Logger

func (Logger) Info

func (l Logger) Info() log.Logger

func (Logger) Warn

func (l Logger) Warn() log.Logger

func (Logger) With

func (l Logger) With(keysvals ...interface{}) Logger

func (Logger) WithError

func (l Logger) WithError(err error) log.Logger

WithError add an err to structured log

func (Logger) WrapError

func (l Logger) WrapError(err error) log.Logger

WrapError wrap an original error to kerrs and add to the structured log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL