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

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

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

问题

所以我开始使用'logrus'。
我将它设置在我自己的日志记录器包中,看起来像这样:

  1. package logger
  2. import (
  3. log "github.com/Sirupsen/logrus"
  4. )
  5. func InitLogger() {
  6. var textFormatter = new(log.TextFormatter)
  7. textFormatter.TimestampFormat = "2006-01-02 15:04:05"
  8. textFormatter.ForceColors = true
  9. textFormatter.FullTimestamp = true
  10. log.SetFormatter(textFormatter)
  11. }
  12. func Printf(format string, v ...interface{}) {
  13. log.Printf(format, v...)
  14. }
  15. func Fatalf(format string, v ...interface{}) {
  16. log.Fatalf(format, v...)
  17. }
  18. func Panicf(format string, v ...interface{}) {
  19. log.Panicf(format, v...)
  20. }
  21. func Debugf(format string, v ...interface{}) {
  22. log.Debugf(format, v...)
  23. }

(对于所有logrus日志函数都是这样的,不必在这里粘贴所有内容,但你明白我的意思...)

在我的项目中使用它:

  1. import (
  2. log "logger"
  3. )

除了日志之间有空行之外,一切都很顺利:

  1. INFO[2016-04-16 17:32:51] pathPrefix: /app/
  2. pathValue: {js ./app/}
  3. INFO[2016-04-16 17:32:51] pathPrefix: /node_modules/
  4. pathValue: {js ./node_modules/}
  5. INFO[2016-04-16 17:32:51] Listening for HTTP on tcp (0.0.0.0:8080)
  6. INFO[2016-04-16 17:38:02] Starting HomeHandler
  7. INFO[2016-04-16 17:38:02] GET / Index 4.769735ms
  8. INFO[2016-04-16 17:38:02] Starting AuthCheckHandler
  9. ERRO[2016-04-16 17:38:02] Failed getting cookie from user: http: named cookie not present
  10. INFO[2016-04-16 17:38:02] GET /auth AuthCheckHandler 67.79μs
  11. INFO[2016-04-16 17:38:02] Starting AuthCheckHandler
  12. ERRO[2016-04-16 17:38:02] Failed getting cookie from user: http: named cookie not present
  13. 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:

  1. package logger
  2. import(
  3. log "github.com/Sirupsen/logrus"
  4. )
  5. func InitLogger() {
  6. var textFormatter = new(log.TextFormatter)
  7. textFormatter.TimestampFormat = "2006-01-02 15:04:05"
  8. textFormatter.ForceColors = true
  9. textFormatter.FullTimestamp = true
  10. log.SetFormatter(textFormatter)
  11. }
  12. func Printf(format string, v ...interface{}) {
  13. log.Printf(format, v...)
  14. }
  15. func Fatalf(format string, v ...interface{}) {
  16. log.Fatalf(format, v...)
  17. }
  18. func Panicf(format string, v ...interface{}) {
  19. log.Panicf(format, v...)
  20. }
  21. func Debugf(format string, v ...interface{}) {
  22. 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:

  1. import(
  2. log "logger"
  3. )

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

  1. INFO[2016-04-16 17:32:51] pathPrefix: /app/
  2. pathValue: {js ./app/}
  3. INFO[2016-04-16 17:32:51] pathPrefix: /node_modules/
  4. pathValue: {js ./node_modules/}
  5. INFO[2016-04-16 17:32:51] Listening for HTTP on tcp (0.0.0.0:8080)
  6. INFO[2016-04-16 17:38:02] Starting HomeHandler
  7. INFO[2016-04-16 17:38:02] GET / Index 4.769735ms
  8. INFO[2016-04-16 17:38:02] Starting AuthCheckHandler
  9. ERRO[2016-04-16 17:38:02] Failed getting cookie from user: http: named cookie not present
  10. INFO[2016-04-16 17:38:02] GET /auth AuthCheckHandler 67.79µs
  11. INFO[2016-04-16 17:38:02] Starting AuthCheckHandler
  12. ERRO[2016-04-16 17:38:02] Failed getting cookie from user: http: named cookie not present
  13. 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

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

  1. func main() {
  2. x := 0
  3. logger.InitLogger()
  4. logger.Printf("%s", "TEST1")
  5. logger.Printf("%s", "TEST2")
  6. if x == 0 {
  7. logger.Printf("%s", "TEST3")
  8. logger.Printf("%s", "TEST4")
  9. logger.Printf("%s", "TEST5")
  10. }
  11. }

返回结果:

  1. go run main.go
  2. INFO[2016-04-16 12:40:30] TEST1
  3. INFO[2016-04-16 12:40:30] TEST2
  4. INFO[2016-04-16 12:40:30] TEST3
  5. INFO[2016-04-16 12:40:30] TEST4
  6. INFO[2016-04-16 12:40:30] TEST5
英文:

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

  1. func main() {
  2. x := 0
  3. logger.InitLogger()
  4. logger.Printf("%s", "TEST1")
  5. logger.Printf("%s", "TEST2")
  6. if x == 0 {
  7. logger.Printf("%s", "TEST3")
  8. logger.Printf("%s", "TEST4")
  9. logger.Printf("%s", "TEST5")
  10. }
  11. }

Returns:

  1. go run main.go
  2. INFO[2016-04-16 12:40:30] TEST1
  3. INFO[2016-04-16 12:40:30] TEST2
  4. INFO[2016-04-16 12:40:30] TEST3
  5. INFO[2016-04-16 12:40:30] TEST4
  6. 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:

确定