Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support package TAG #3

Closed
oiooj opened this issue Oct 11, 2016 · 3 comments
Closed

support package TAG #3

oiooj opened this issue Oct 11, 2016 · 3 comments
Assignees

Comments

@oiooj
Copy link
Contributor

oiooj commented Oct 11, 2016

需求描述

现在想把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的。

@ziyel
Copy link
Collaborator

ziyel commented Oct 11, 2016

关于问题

golang自带的log库, 也是一个logger指定一个tag, 我们的log也是支持建立一个独立的logger

func NewLogger(level interface{}, backend Backend) *Logger {
    l := new(Logger)
    l.SetSeverity(level)
    l.backend = backend
    return l
}

@ziyel
Copy link
Collaborator

ziyel commented Oct 11, 2016

    logger, _ := log.NewLoggerFromConfig(log.LogConfig{
        Type:   "std",
        Level:  "INFO",
        Prefix: "pre",
    })

    logger.Info("test")
    logger.Println("test")
    logger.Warning("test")
    logger.Printf("test")
    logger.Error("test")
2016-10-12 00:31:57.373672 [pre] INFO play/main.go:15 test
2016-10-12 00:31:57.373835 [pre] INFO play/main.go:16 test
2016-10-12 00:31:57.373841 [pre] WARNING play/main.go:17 test
2016-10-12 00:31:57.373846 [pre] INFO play/main.go:18 test
2016-10-12 00:31:57.373855 [pre] ERROR play/main.go:19 test

@ziyel
Copy link
Collaborator

ziyel commented Oct 11, 2016

review下pr, #7
pr有加prefix, 还整理了一下代码

@oiooj oiooj closed this as completed Oct 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants