douzhantanju1849 2017-03-25 22:19
浏览 22

登录文件时如何禁用日志中的字段名称

I am using logrus in golang to log to file. Right now, logrus is logging to file with field name.

ie

time="26-03-2017 03:37:58" level=error msg="Not fatal. An error. Won't stop execution"

How do I remove the field names so that the log entry becomes

ERRO 26-03-2017 03:37:58 Not fatal. An error. Won't stop execution

like how it behaves in case of stderr?

  • 写回答

2条回答 默认 最新

  • dongrui6787 2017-03-25 22:42
    关注

    You need to write a custom formatter that emits only the fields you want to see.

    There is an example in the logrus readme:

    type MyJSONFormatter struct {
    }
    
    log.SetFormatter(new(MyJSONFormatter))
    
    func (f *MyJSONFormatter) Format(entry *Entry) ([]byte, error) {
      // Note this doesn't include Time, Level and Message which are available on
      // the Entry. Consult `godoc` on information about those fields or read the
      // source of the official loggers.
      serialized, err := json.Marshal(entry.Data)
        if err != nil {
          return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
        }
      return append(serialized, '
    '), nil
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向