如何在使用Logrus记录日志到文件时禁用字段名称?

huangapple go评论117阅读模式
英文:

How do I disable field name in logrus while logging to file

问题

我正在使用golang中的logrus将日志记录到文件中。现在,logrus正在使用字段名记录到文件中。

例如

time="26-03-2017 03:37:58" level=error msg="Not fatal. An error. Won't stop execution"

如何删除字段名,使得日志条目变为

ERRO 26-03-2017 03:37:58 Not fatal. An error. Won't stop execution

就像在stderr的情况下一样?

英文:

I am using logrus in golang to log to file. Right now, logrus is logging to file with field name.

ie

time="26-03-2017 03:37:58" level=error msg="Not fatal. An error. Won't stop execution"

How do I remove the field names so that the log entry becomes

ERRO 26-03-2017 03:37:58 Not fatal. An error. Won't stop execution

like how it behaves in case of stderr?

答案1

得分: 3

以下是您请求的内容的翻译:

这是如何获取您请求的内容的方法:

  1. package main
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. "fmt"
  5. )
  6. type PlainFormatter struct {
  7. TimestampFormat string
  8. LevelDesc []string
  9. }
  10. func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
  11. timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat))
  12. return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil
  13. }
  14. func main() {
  15. plainFormatter := new(PlainFormatter)
  16. plainFormatter.TimestampFormat = "2006-01-02 15:04:05"
  17. plainFormatter.LevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"}
  18. log.SetFormatter(plainFormatter)
  19. log.Errorln("Not fatal. An error. Won't stop execution")
  20. log.Infoln("Just some info")
  21. }
  • 创建 PlainFormatter 结构体(包含时间戳格式和级别字段)
  • 创建 Format 函数/方法(其中您的结构体是接收者)
  • 设置 logrus 使用您的自定义格式化程序

输出:

  1. ERRO 2017-07-01 18:22:21 Not fatal. An error. Won't stop execution
  2. INFO 2017-07-01 18:22:21 Just some info

在我的书《在 Go 中学习函数式编程》中,您可以在“使用装饰添加功能”章节中看到如何操作应用程序日志记录器的更实际的示例。

英文:

Here's how to get what you requested:

  1. package main
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. "fmt"
  5. )
  6. type PlainFormatter struct {
  7. TimestampFormat string
  8. LevelDesc []string
  9. }
  10. func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
  11. timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat))
  12. return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil
  13. }
  14. func main() {
  15. plainFormatter := new(PlainFormatter)
  16. plainFormatter.TimestampFormat = "2006-01-02 15:04:05"
  17. plainFormatter.LevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"}
  18. log.SetFormatter(plainFormatter)
  19. log.Errorln("Not fatal. An error. Won't stop execution")
  20. log.Infoln("Just some info")
  21. }
  • Create the PlainFormatter struct (with timestamp format and level fields)
  • Create a Format function/method (where your struct is the receiver)
  • Set logrus to use your custom formatter

Output:

  1. ERRO 2017-07-01 18:22:21 Not fatal. An error. Won't stop execution
  2. INFO 2017-07-01 18:22:21 Just some info

In my book Learn Functional Programming in Go book you can see a more practical example of how you can manipulate your application logger in Adding Functionality with Decoration chapter.

答案2

得分: 1

你需要编写一个自定义格式化程序,只输出你想要看到的字段。

logrus readme 中有一个示例:

  1. type MyJSONFormatter struct {
  2. }
  3. log.SetFormatter(new(MyJSONFormatter))
  4. func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
  5. // 注意,这里不包括时间、级别和消息,它们在 Entry 上是可用的。
  6. // 请查阅 `godoc` 获取有关这些字段的信息,或阅读官方记录器的源代码。
  7. serialized, err := json.Marshal(entry.Data)
  8. if err != nil {
  9. return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
  10. }
  11. return append(serialized, '\n'), nil
  12. }
英文:

You need to write a custom formatter that emits only the fields you want to see.

There is an example in the logrus readme:

<!-- language: go -->

  1. type MyJSONFormatter struct {
  2. }
  3. log.SetFormatter(new(MyJSONFormatter))
  4. func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
  5. // Note this doesn&#39;t include Time, Level and Message which are available on
  6. // the Entry. Consult `godoc` on information about those fields or read the
  7. // source of the official loggers.
  8. serialized, err := json.Marshal(entry.Data)
  9. if err != nil {
  10. return nil, fmt.Errorf(&quot;Failed to marshal fields to JSON, %v&quot;, err)
  11. }
  12. return append(serialized, &#39;\n&#39;), nil
  13. }

huangapple
  • 本文由 发表于 2017年3月26日 06:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/43022607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定