编程方式关闭 Revel 框架的日志记录。

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

Programmatically turn off logging revel framework

问题

你好!以下是翻译好的内容:

如何以编程方式关闭如下所示的日志记录。我需要这样做以便能够在运行测试套件时不会填充测试日志中的警告和信息日志。

revel.INFO.printf("")

谢谢你的帮助。

英文:

How can I programatically turn of logs like the one below. I need this to be able to run my testsuites with out filling the test logs with warnings and info logs.

revel.INFO.printf("")

Thanks for your help.

答案1

得分: 2

revel包中,你有:

var (
    // Loggers
    TRACE = log.New(ioutil.Discard, "TRACE ", log.Ldate|log.Ltime|log.Lshortfile)
    INFO  = log.New(ioutil.Discard, "INFO  ", log.Ldate|log.Ltime|log.Lshortfile)
    WARN  = log.New(ioutil.Discard, "WARN  ", log.Ldate|log.Ltime|log.Lshortfile)
    ERROR = log.New(&error_log, "ERROR ", log.Ldate|log.Ltime|log.Lshortfile)
)

log包中,你有:

func New

    func New(out io.Writer, prefix string, flag int) *Logger

New函数创建一个新的Loggerout变量设置日志数据将被写入的目标prefix出现在每个生成的日志行的开头flag参数定义了日志的属性

ioutil包中,你有:

var Discard io.Writer = devNull(0)

Discard是一个io.Writer所有的写入调用都成功而不做任何操作

因此,要关闭revel.INFO日志,请尝试:

revel.INFO = log.New(ioutil.Discard, "INFO  ", log.Ldate|log.Ltime|log.Lshortfile)
英文:

From package revel you have:

> var (
> // Loggers
> TRACE = log.New(ioutil.Discard, "TRACE ", log.Ldate|log.Ltime|log.Lshortfile)
> INFO = log.New(ioutil.Discard, "INFO ", log.Ldate|log.Ltime|log.Lshortfile)
> WARN = log.New(ioutil.Discard, "WARN ", log.Ldate|log.Ltime|log.Lshortfile)
> ERROR = log.New(&error_log, "ERROR ", log.Ldate|log.Ltime|log.Lshortfile)
> )

From package log you have:

> func New
>
> func New(out io.Writer, prefix string, flag int) *Logger
>
> New creates a new Logger. The out variable sets the destination to
> which log data will be written. The prefix appears at the beginning of
> each generated log line. The flag argument defines the logging
> properties.

From package ioutil you have:

> var Discard io.Writer = devNull(0)
>
> Discard is an io.Writer on which all Write calls succeed without doing
> anything.

Therefore, to turn off the revel.INFO log try:

revel.INFO = log.New(ioutil.Discard, "INFO  ", log.Ldate|log.Ltime|log.Lshortfile)

huangapple
  • 本文由 发表于 2015年5月1日 20:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/29986674.html
匿名

发表评论

匿名网友

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

确定