这个日志文件是什么格式的?有没有办法将其转换成其他格式?

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

What format does this log file is and is there a way to convert this?

问题

这可能是一个无用的问题,但由于我不知道该搜索什么和如何搜索,所以我决定向公众提问。如果您认为这个问题需要改进或需要更多信息,请留下评论。我会尽快回复并进行编辑。

我有一个简单的 Go 程序,使用了一个名为 zerolog 的第三方日志库,并打印日志。

package main

import (
	"os"
	"time"

	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
)

func init() {
	zerolog.SetGlobalLevel(zerolog.DebugLevel)
	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano})
}

func main() {
	log.Debug().Msg("This if debugging message")
	log.Info().Msg("This is information message")
}

如果你简单地用 $ go run main.go 运行这个程序,它会打印出以下结果。

2021-06-23T12:45:29.409Z DBG This if debugging message
2021-06-23T12:45:29.41Z INF This is information message

然而,如果我使用 nohup 运行这个程序 $ nohup go run main.go &,那么它会创建一个名为 nohup.out 的输出文件,并写入不同的日志格式。为什么会这样呢?

[90m2021-06-23T12:59:37.755Z[0m [33mDBG[0m This if debugging message
[90m2021-06-23T12:59:37.756Z[0m [32mINF[0m This is information message

有人可以友好地解释一下这是什么格式,以及如何将其转换为像第一个结果那样的可读格式吗?

英文:

This may be a noop question to ask but since I have no idea what and how to search for, I decided to to ask this to public. Please leave a comment if you think this question needs an improvement or need more information. I'll try to respond and edit as soon as possible.

I have this simple Go program that uses thirty party logging library called zerolog and prints logs.

package main

import (
	"os"
	"time"

	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
)

func init() {
	zerolog.SetGlobalLevel(zerolog.DebugLevel)
	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339Nano})
}

func main() {
	log.Debug().Msg("This if debugging message")
	log.Info().Msg("This is information message")
}

If you simply run this program with $ go run main.go, it prints out the following result.

2021-06-23T12:45:29.409Z DBG This if debugging message
2021-06-23T12:45:29.41Z INF This is information message

However, if I use nohup to run this program $ nohup go run main.go &, then it creates an output file called nohup.out and writes different log format. Why is that?

[90m2021-06-23T12:59:37.755Z[0m [33mDBG[0m This if debugging message
[90m2021-06-23T12:59:37.756Z[0m [32mINF[0m This is information message

Can anyone kindly explain to me what format this is and how do I convert this to human readable format like the first result?

答案1

得分: 4

它正在使用带有颜色的zerolog ConsoleWriter,所以你在第二个输出中看到的是用于彩色输出的转义序列。你可以关闭颜色(从zerolog示例中复制):


func main() {
	out := zerolog.NewConsoleWriter()
	out.NoColor = true 
	log := zerolog.New(out)

	log.Debug().Str("foo", "bar").Msg("Hello World")
}
英文:

It is using the zerolog ConsoleWriter with color, so what you see in the second output is the escape sequences for colored output. You can turn color off (copied from zerolog examples):


func main() {
	out := zerolog.NewConsoleWriter()
	out.NoColor = true 
	log := zerolog.New(out)

	log.Debug().Str("foo", "bar").Msg("Hello World")
}

</details>



huangapple
  • 本文由 发表于 2021年6月23日 21:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68100428.html
匿名

发表评论

匿名网友

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

确定