gpt4 book ai didi

go - 使用 'logrus' 包时如何去掉日志之间的空格

转载 作者:IT王子 更新时间:2023-10-29 02:27:43 24 4
gpt4 key购买 nike

所以我开始使用“logrus”。我将它设置在我自己的记录器包中,如下所示:

    package logger

import(
log "github.com/Sirupsen/logrus"

)


func InitLogger() {
var textFormatter = new(log.TextFormatter)
textFormatter.TimestampFormat = "2006-01-02 15:04:05"
textFormatter.ForceColors = true
textFormatter.FullTimestamp = true

log.SetFormatter(textFormatter)
}


func Printf(format string, v ...interface{}) {
log.Printf(format, v...)
}

func Fatalf(format string, v ...interface{}) {
log.Fatalf(format, v...)
}

func Panicf(format string, v ...interface{}) {
log.Panicf(format, v...)
}

func Debugf(format string, v ...interface{}) {
log.Debugf(format, v...)

(所有 logrus 日志函数都是这样,没看到 将所有这些都粘贴到这里,但你明白了......)

在我的项目中使用它作为:

import(
log "logger"
)

一切都很好,除了打印的日志之间有行间距:

INFO[2016-04-16 17:32:51] pathPrefix: /app/
pathValue: {js ./app/}

INFO[2016-04-16 17:32:51] pathPrefix: /node_modules/
pathValue: {js ./node_modules/}

INFO[2016-04-16 17:32:51] Listening for HTTP on tcp (0.0.0.0:8080)
INFO[2016-04-16 17:38:02] Starting HomeHandler
INFO[2016-04-16 17:38:02] GET / Index 4.769735ms

INFO[2016-04-16 17:38:02] Starting AuthCheckHandler
ERRO[2016-04-16 17:38:02] Failed getting cookie from user: http: named cookie not present

INFO[2016-04-16 17:38:02] GET /auth AuthCheckHandler 67.79µs

INFO[2016-04-16 17:38:02] Starting AuthCheckHandler
ERRO[2016-04-16 17:38:02] Failed getting cookie from user: http: named cookie not present

INFO[2016-04-16 17:38:02] GET /auth AuthCheckHandler 82.195µs

我怎样才能去掉那些空格?无法在文档/谷歌或此处找到任何解决方案所以我认为我错过了一些非常愚蠢的事情......

谢谢

最佳答案

似乎您在传递字符串格式时传递了换行符。

func main() {

x := 0
logger.InitLogger()

logger.Printf("%s", "TEST1")
logger.Printf("%s", "TEST2")

if x == 0 {
logger.Printf("%s", "TEST3")
logger.Printf("%s", "TEST4")
logger.Printf("%s", "TEST5")
}
}

返回:

go run main.go
INFO[2016-04-16 12:40:30] TEST1
INFO[2016-04-16 12:40:30] TEST2
INFO[2016-04-16 12:40:30] TEST3
INFO[2016-04-16 12:40:30] TEST4
INFO[2016-04-16 12:40:30] TEST5

关于go - 使用 'logrus' 包时如何去掉日志之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36665634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com