Code Monkey home page Code Monkey logo

log's People

Contributors

oiooj avatar ziyel avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

log's Issues

support package TAG

需求描述

现在想把logger写在struct中,例如:

type Store struct {
    raftDir  string
    raftBind string
    dbPath   string

    logger *log.Logger
}

然后我可以在new 这个struct的时候同时new一个 logger ,然后给他set tag, 标准库的log是这样的:


return &Store{
....
Logger:  log.New(os.Stderr, "[store] ", log.LstdFlags),

}

这样,store的log我可以这样写 store.logger.Errorf("message"), sore 打出来的log都会有[store]前缀。每个包的最终日志写到一起不变。原有的log方式希望也能保持,只是新增支持打tag。

问题:

现在是一个全局logger, 如果一个struct设置tag会不会影响其他struct的。

log panic

fatal error: concurrent map read and map write

goroutine 9 [running]:
runtime.throw(0x82dcfb, 0x21)
        /usr/local/go/src/runtime/panic.go:566 +0x95 fp=0xc426131788 sp=0xc426131768
runtime.mapaccess2(0x7b41a0, 0xc420168960, 0xc420776610, 0xc42387a1a0, 0xc420eda0b8)
        /usr/local/go/src/runtime/hashmap.go:340 +0x249 fp=0xc4261317d0 sp=0xc426131788
reflect.mapaccess(0x7b41a0, 0xc420168960, 0xc420776610, 0xc420168960)
        /usr/local/go/src/runtime/hashmap.go:1008 +0x3f fp=0xc426131808 sp=0xc4261317d0
reflect.Value.MapIndex(0x7b41a0, 0xc420168960, 0x15, 0x794b00, 0xc420776610, 0x98, 0x78ef00, 0xc42387a1a0, 0x97)
        /usr/local/go/src/reflect/value.go:1040 +0x128 fp=0xc426131890 sp=0xc426131808
fmt.(*pp).printValue(0xc422874000, 0x7b41a0, 0xc420168960, 0x15, 0x76, 0x0)
        /usr/local/go/src/fmt/print.go:738 +0x120f fp=0xc426131a80 sp=0xc426131890
fmt.(*pp).printArg(0xc422874000, 0x7b41a0, 0xc420168960, 0x76)
        /usr/local/go/src/fmt/print.go:668 +0x1f5 fp=0xc426131b78 sp=0xc426131a80
fmt.(*pp).doPrintf(0xc422874000, 0x825250, 0x10, 0xc426131e98, 0x1, 0x1)
        /usr/local/go/src/fmt/print.go:985 +0x123d fp=0xc426131c60 sp=0xc426131b78
fmt.Fprintf(0x9dc3c0, 0xc420bf4540, 0x825250, 0x10, 0xc426131e98, 0x1, 0x1, 0xb54, 0xc428aa6b54, 0xc426131d58)
        /usr/local/go/src/fmt/print.go:181 +0x76 fp=0xc426131cc8 sp=0xc426131c60
github.com/lodastack/log.(*Logger).printfDepth(0xa038c0, 0x3, 0x1, 0x825250, 0x10, 0xc426131e98, 0x1, 0x1)
        /data/golang/src/github.com/lodastack/log/log.go:210 +0xaf fp=0xc426131d30 sp=0xc426131cc8
github.com/lodastack/log.(*Logger).printf(0xa038c0, 0x3, 0x825250, 0x10, 0xc426131e98, 0x1, 0x1)
        /data/golang/src/github.com/lodastack/log/log.go:190 +0x73 fp=0xc426131d80 sp=0xc426131d30
github.com/lodastack/log.Infof(0x825250, 0x10, 0xc426131e98, 0x1, 0x1)
        /data/golang/src/github.com/lodastack/log/log.go:408 +0x68 fp=0xc426131dc8 sp=0xc426131d80
github.com/lodastack/router/loda.PurgeAll()

log.Fatal改进

Fatal 级别的日志需要打出整个stack么,协程很多会打出一堆东西,反而你fata的信息是不会打出来的,还不如 panic 好用,是不是日志记录下fata信息,直接退出程序好一些, 现在fata信息好像是打不到日志上的。

Implementations io.Writer

实现 io.Writer , 这样可以直接兼容到标准库,func New(out io.Writer, prefix string, flag int) *Logger

好多第三方库的logger类型是标准库的Logger,我们无法用现在的logger直接覆盖到第三方库的logger(类型不同),但是我们可以用 func New(out io.Writer, prefix string, flag int) *Logger 封装一层,生成一个标准库的。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.