douhe2305 2016-04-16 14:49
浏览 32
已采纳

使用“ logrus”包时如何消除日志之间的空间

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

  • 写回答

2条回答 默认 最新

  • drm16022 2016-04-16 16:47
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算