如何在Golang日志记录器中对齐列?

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

How to align columns in Golang logger?

问题

配置Golang日志记录器以显示代码中的位置(如文档中的代码片段),消息没有对齐。

例如:

$ go run .
main.go:11: From main
afilewithalongname.go:4: From function

如何垂直对齐消息“From main”和“From function”?

下面是最小可复现示例(MCVE)。

文件main.go

package main

import(
        "log"
        "os"
)

var logger = log.New(os.Stderr, "", log.Lshortfile)

func main() {
        logger.Println("From main")
        SomeFunction()
}

文件afilewithalongname.go

package main

func SomeFunction() {
        logger.Println("From function")
}

文件go.mod

module testlog

go 1.17
英文:

When configuring Golang logger to show the location in code (like the snippet in the doc), the messages are not aligned.

For example:

$ go run .
main.go:11: From main
afilewithalongname.go:4: From function

How to vertically align the messages "From main" and "From function"?

Below, the MCVE.

File main.go:

package main

import(
        "log"
        "os"
)

var logger = log.New(os.Stderr, "", log.Lshortfile)

func main() {
        logger.Println("From main")
        SomeFunction()
}

File afilewithalongname.go:

package main

func SomeFunction() {
        logger.Println("From function")
}

File go.mod:

module testlog

go 1.17

答案1

得分: 2

你对标准日志包的控制能力有限。如果你只想将日志写入标准输出,可以使用fmt.Printf,它允许你对字符串的某些部分进行填充。你可以使用runtime.Caller函数获取调用函数的文件名。示例如下:

func log(msg string) {
    _, file, no, ok := runtime.Caller(1)
    if ok {
        fmt.Printf("%30s:%d: %s\n", file, no, msg)
    }
}

其中的%30s表示将字符串填充为30个字符宽,这个值取决于你的最大文件大小。

我还可以推荐使用专门的日志库,比如logrus,它提供了更多的灵活性,可以自定义日志的格式和输出位置等。

英文:

You don't have that much control over the standard log package. If you simply want to write to stdout you can use fmt.Printf which allows you to pad parts of your string. You can get the file name of the calling function with the runtime.Caller function. To get something like:

func log(msg string) {
    _, file, no, ok := runtime.Caller(1)
    if ok {
        fmt.Printf("%30s:%d: %s\n", file, no, msg)
    }
}

The %30s part means we will pad the string to 30 chars wide, this value will depend on your max file size.

I can also recommend a dedicated logging library like logrus which offers you more flexibility in the way our logs are formatted and where they are written to ect.

答案2

得分: 0

标准库中的log包虽然有一些标志(如LdateLshortfile),但在打印到控制台或文件时没有影响垂直对齐的选项。

您需要从标准库之外选择一个日志记录器。当输出设备是TTY时,logrus的默认配置会执行一些垂直对齐操作。

英文:

While the log package in the standard library has some flags such as Ldate and Lshortfile, there is no option to affect the vertical alignment when printed to a console or file.

You would need to choose a logger from outside of the standard library. The default configuration of logrus will perform some vertical alignment when the output device is a TTY.

huangapple
  • 本文由 发表于 2021年11月4日 00:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/69828776.html
匿名

发表评论

匿名网友

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

确定