无法将 logrus 输出发送到 /dev/null

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

unable to send logrus output /dev/null

问题

我正在使用Logrus包来记录我的项目日志。

一切都很好,但是为了测试目的,我不希望logrus的输出显示出来。

我正在查看logrus.SetOutput以获取答案。

所以基本上我希望日志被写入/dev/null

但是由于某种原因,我仍然在标准输出(STDOUT)上看到日志。

以下是我的日志设置代码:

// logger.go

package logger

import (
	"os"
	"io/ioutil"
	"github.com/Sirupsen/logrus"
)

var log *logrus.Logger

const (
	DEBUG   = 0
	INFO    = 1
	WARNING = 2
	ERROR   = 3
)

func init() {
	env := os.Getenv("ENV")
	if env != "test" {
		return
	}
	logrus.SetOutput(ioutil.Discard)
}


func Init(logLevel int) {
	log = logrus.New()
	log.Formatter = &logrus.TextFormatter{FullTimestamp: true}
	switch logLevel {
	case DEBUG:
		log.Level = logrus.DebugLevel
	case INFO:
		log.Level = logrus.InfoLevel
	case WARNING:
		log.Level = logrus.WarnLevel
	case ERROR:
		log.Level = logrus.ErrorLevel
	}
}

func Get() *logrus.Logger {
	return log
}

func I(args ...interface{}) {
	log.Info(args)
}

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

func D(args ...interface{}) {
	log.Debug(args)
}

func W(args ...interface{}) {
	log.Warn(args)
}

以下是我如何使用logger包:

import "logger"

func init() {
  logger.Init(0)
}

logger.D("Dummy string")

希望这能帮到你!

英文:

I'm using Logrus package for logging on my project.

All is fine but for testing purpose I do not need the logrus output to be displayed.

I looking at logrus.SetOuput for answer.

So basically I want the log to be written to /dev/null

But for some reason I still see the logs on STDOUT

   // This code run right at the start
   if env == "test" {
		fmt.Println("Entering test mode ....")
		logrus.SetOutput(ioutil.Discard)
		return
	}

Here how my logger setup looks like

// logger.go

package logger

import(
	"os"
    "io/ioutil"
	"github.com/Sirupsen/logrus"
)

var log *logrus.Logger

const (
	DEBUG   = 0
	INFO    = 1
	WARNING = 2
	ERROR   = 3
)

func init() {
    env := os.GetEnv("ENV")
    if env != "test" {
        return
    }
    logrus.SetOutput(ioutil.Discard)
}


func Init(logLevl int) {
	log = logrus.New()
	log.Formatter = &logrus.TextFormatter{FullTimestamp: true}
	switch logLevl {
	case DEBUG:
		log.Level = logrus.DebugLevel
	case INFO:
		log.Level = logrus.InfoLevel
	case WARNING:
		log.Level = logrus.WarnLevel
	case ERROR:
		log.Level = logrus.ErrorLevel
	}
}

func Get() *logrus.Logger {
	return log
}

func I(args ...interface{}) {
	log.Info(args)
}

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

func D(args ...interface{}) {
	log.Debug(args)
}

func W(args ...interface{}) {
	log.Warn(args)
}

And here how I use the logger package

import "logger"

func init() {
  logger.Init(0)
}

logger.D("Dummy string")

答案1

得分: 1

你的代码中使用了两个不同的logrus.Logger变量。第一个是在init()函数中调用的全局默认Logger,它是从logrus导出的。然后,在你的自定义初始化函数Init(logLevel int)中,你通过logrus.New()创建了一个新的Logger,从那时起用于日志记录,但它没有配置为使用不同的输出。

我建议你摒弃init()函数,并在你的Init(logLevel int)函数中进行所有的初始化(请注意设置输出的不同API)。

func Init(logLevel int) {
    log = logrus.New()
    log.Formatter = &logrus.TextFormatter{FullTimestamp: true}
    switch logLevel {
    case DEBUG:
        log.Level = logrus.DebugLevel
    case INFO:
        log.Level = logrus.InfoLevel
    case WARNING:
        log.Level = logrus.WarnLevel
    case ERROR:
        log.Level = logrus.ErrorLevel
    }

    env := os.Getenv("ENV")
    if env != "test" {
        return
    }
    log.Out = ioutil.Discard
}
英文:

You're using two different logrus.Logger variables in your code. The first, which is called in init() is the global default Logger that gets exported from logrus. Later, in your custom initialization function Init(logLevel int), you're creating a new Logger via logrus.New() which is from then on used for logging — but hasn't been configured to use a different output.

I'd suggest to get rid of the init() function and do all the initialization in your Init(logLevel int) (please note the different API for setting the output).

func Init(logLevl int) {
    log = logrus.New()
    log.Formatter = &logrus.TextFormatter{FullTimestamp: true}
    switch logLevl {
    case DEBUG:
        log.Level = logrus.DebugLevel
    case INFO:
        log.Level = logrus.InfoLevel
    case WARNING:
        log.Level = logrus.WarnLevel
    case ERROR:
        log.Level = logrus.ErrorLevel
    }

    env := os.Getenv("ENV")
    if env != "test" {
        return
    }
    log.Out = ioutil.Discard
}

huangapple
  • 本文由 发表于 2017年6月28日 14:24:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/44795041.html
匿名

发表评论

匿名网友

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

确定