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

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

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

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

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

package main

import (
	log "github.com/sirupsen/logrus"
	"fmt"
)

type PlainFormatter struct {
	TimestampFormat string
	LevelDesc []string
}

func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
	timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat))
	return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil
}

func main() {
	plainFormatter := new(PlainFormatter)
	plainFormatter.TimestampFormat = "2006-01-02 15:04:05"
	plainFormatter.LevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"}
	log.SetFormatter(plainFormatter)
	log.Errorln("Not fatal. An error. Won't stop execution")
	log.Infoln("Just some info")
}
  • 创建 PlainFormatter 结构体(包含时间戳格式和级别字段)
  • 创建 Format 函数/方法(其中您的结构体是接收者)
  • 设置 logrus 使用您的自定义格式化程序

输出:

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

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

英文:

Here's how to get what you requested:

package main

import (
	log "github.com/sirupsen/logrus"
	"fmt"
)

type PlainFormatter struct {
	TimestampFormat string
	LevelDesc []string
}

func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
	timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat))
	return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil
}

func main() {
	plainFormatter := new(PlainFormatter)
	plainFormatter.TimestampFormat = "2006-01-02 15:04:05"
	plainFormatter.LevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"}
	log.SetFormatter(plainFormatter)
	log.Errorln("Not fatal. An error. Won't stop execution")
	log.Infoln("Just some info")
}
  • 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:

ERRO 2017-07-01 18:22:21 Not fatal. An error. Won't stop execution
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 中有一个示例:

type MyJSONFormatter struct {
}

log.SetFormatter(new(MyJSONFormatter))

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

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 -->

type MyJSONFormatter struct {
}

log.SetFormatter(new(MyJSONFormatter))

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

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:

确定