首页 > 解决方案 > winston-elasticsearch 和自定义列

问题描述

我正在使用 winston-logger 和 winston-elasticsearch 创建日志并将它们推送到 ELK。

const esTransportOpts = {
  level: 'debug'  
};
const logFormat = winston.format.printf((info) => {
  let message = `${dateFormat()}, ${global.sessionId}, ${info.level}, ${info.message}`
  message = info.obj ? message + `data:${JSON.stringify(info.obj)},` : message
  message = this.log_data ? message + `log_data:${JSON.stringify(this.log_data)},` : message
  
  return message
});
const Logger = new createLogger({
  transports: [
    new transports.Console({
      level: 'silly',
      format: combine(
          timestamp(),
          colorize({level: true}),
          consoleFormat
      ),
    }),
    new transports.File({
      level: 'silly',
      format: logFormat,
      filename: path.join(__dirname, './uiServiceLog.log'),
      maxsize: 2000000,
      maxFiles: 20

    }),
    new Elasticsearch(esTransportOpts),
 
  ]
});

我正在以正确的格式获取我的日志文件..

2021-11-15 13:39:20, sessionid, 信息, 我的消息

但我的 ELK 没有获得 sessionid .. Elasticsearch DEBUG: 2021-11-15T20:20:35Z

starting request {
    "method": "POST",
    "bulkBody": true,
    "path": "/_bulk",
    "body": [
      {
        "index": {
          "_index": "logs-2021.11.15",
          "_type": "_doc",
          "pipeline": null
        }
      },
      {
        "@timestamp": "2021-11-15T20:20:35.457Z",
        "message": "\u001b[1m\u001b[31m=============================================\u001b[39m\u001b[22m",
        "severity": "info",
        "fields": {}
      },
      {
        "index": {
          "_index": "logs-2021.11.15",
          "_type": "_doc",
          "pipeline": null
        }
      },
      {
        "@timestamp": "2021-11-15T20:20:35.457Z",
        "message": "\",
        "severity": "info",
        "fields": {}
      },
      {
        "index": {
          "_index": "logs-2021.11.15",
          "_type": "_doc",
          "pipeline": null
        }
      },
      {
        "@timestamp": "2021-11-15T20:20:35.458Z",
        "message": "\u001b\u001b[22m",
        "severity": "info",
        "fields": {}
      }
    ],
    "query": {
      "wait_for_active_shards": "1",
      "timeout": "2000ms"
    }
  }

我尝试使用转换,但它不起作用。无论如何我可以转换 ElK 消息,以便我的日志文件看起来像 ELK 消息。

标签: node.jswinstonwinston-elasticsearch

解决方案


推荐阅读