如何在使用’logrus’包时消除日志之间的空格

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

How to get rid of Spaces between logs when using 'logrus' package

问题

所以我开始使用'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

如何去掉这些空行?
在文档/谷歌和这里都找不到解决方案,所以我认为我可能是漏掉了一些非常愚蠢的东西...

谢谢。

英文:

So I started using 'logrus'.
I set it in my own logger package which looks like this:

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

(it goes like this for all logrus log functions, dont see the
point of pasting all of them here, but you get the idea...)

Use it across my project as:

import(
    log "logger"
)

And everything is peachy except logs being printed with line spaces between them:

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

How can I get rid of those spaces?
Couldn't find any solution in documentation/google nor here
so I assume its something very silly that I am missing...

Thanks

答案1

得分: 3

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

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
英文:

Seems like you are passing newlines when you pass your string formatting.

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")
  }
}

Returns:

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

答案2

得分: 2

你确定你没有意外地在发送给日志记录器包的某些消息中包含换行符吗?

英文:

Are you sure you aren't accidentally including new lines in some of the messages you are sending to your logger package?

huangapple
  • 本文由 发表于 2016年4月16日 22:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/36665634.html
匿名

发表评论

匿名网友

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

确定