How to hide loglevel and time in logrus

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

How to hide loglevel and time in logrus

问题

我正在尝试以CSV格式编写日志。我正在尝试使用logrus将CSV写入日志。但是我看到日志中添加了日志级别(info)和时间。例如:

time="2015-11-18T01:27:38-04:00" level=info msg="100,Click,Android"

如何只写入我的消息而不包括logrus的其他信息?
或者我应该使用golang中其他可用的日志记录器吗?

注意:我是go的新手

英文:

I am trying to write the logs in CSV format. I am trying to use logrus to write the CSV as logs. But I see the log level (info) and the time getting added to the CSV content in logs. Like,

time="2015-11-18T01:27:38-04:00" level=info msg="100,Click,Android"

How can I write only my message using logrus?
Or should I use any other loggers available in golang?

Note: I am a newbie to go

答案1

得分: 0

你可以这样隐藏时间戳:

var logger = logrus.New()
logger.Formatter.(*logrus.TextFormatter).DisableTimestamp = true
logger.Info("不显示时间戳")
英文:

You can hide the timestamp like this:

var logger = logrus.New()
logger.Formatter.(*logrus.TextFormatter).DisableTimestamp = true
logger.Info("no timestamp is shown")

答案2

得分: -2

我正在尝试使用logrus将CSV写入日志。但是我发现日志中添加了日志级别(info)和时间到CSV内容中。

看起来你应该不应该使用logrus,因为它存在的主要原因是创建结构化日志,即键值对形式。不确定为什么你不直接这样做:

fmt.Println("100,Click,Android")

或者

fmt.Fprintln(os.Stderr, "100,Click,Android")

如果你希望将其写入标准错误(日志记录器的默认值)。

如果出于任何原因你需要一个日志记录器,你可以通过禁用标志来使用标准日志记录器来做基本相同的事情:

log.SetFlags(0)
log.Println("100,Click,Android")

// 输出:
// 100,Click,Android

然而,如果你正在自己创建CSV输出(即你已经有了单独的字段而不是完整的CSV行),可以使用标准库中的csv包

w := csv.NewWriter(os.Stdout)
w.Write([]string{"100", "Click", "Android"})
w.Flush()

// 输出:
// 100,Click,Android

除非你只是_想要_使用logrus,那么遵循该包的概念并坚持使用结构化输出:

logrus.WithFields(logrus.Fields{
	"id":    "100",
	"event": "Click",
	"os":    "Android",
}).Info("Hooray, we have some users!")

// 输出:
// time="2015-11-19T15:20:32+01:00" level=info msg="Hooray, we have some users!" event=Click id=100 os=Android
英文:

> I am trying to use logrus to write the CSV as logs. But I see the log level (info) and the time getting added to the CSV content in logs.

It looks like you definitely shouldn't be using logrus, as the main reason it exists is creating structured logs, i.e. key-value style. Not sure why don't you simply do

fmt.Println("100,Click,Android")

or

fmt.Fprintln(os.Stderr, "100,Click,Android")

if you want it to be written into the standard error (default for loggers).

If for any reason you need a logger, you can pretty much do the same thing with the standard logger by disabling flags:

log.SetFlags(0)
log.Println("100,Click,Android")

// Output:
// 100,Click,Android

However, if you're creating a CSV output yourself (i.e. you have spearate fields rather than full CSV lines already), use csv package from the standard library:

w := csv.NewWriter(os.Stdout)
w.Write([]string{"100", "Click", "Android"})
w.Flush()

// Output:
// 100,Click,Android

Unless you just want to use logrus, then follow the concept of the package and stick with structured output:

logrus.WithFields(logrus.Fields{
	"id": "100",
	"event": "Click",
	"os": "Android",
}).Info("Hooray, we have some users!")

// Output:
// time="2015-11-19T15:20:32+01:00" level=info msg="Hooray, we have some users!" event=Click id=100 os=Android

huangapple
  • 本文由 发表于 2015年11月18日 21:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/33781161.html
匿名

发表评论

匿名网友

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

确定