如何将Winston的日志记录集成到Logstash-Kibana中

时间:2019-07-22 08:34:23

标签: node.js elasticsearch logstash winston

我正在尝试将winston.js(3+)的较新版本与logstash(kibana)一起使用。

我得到了一些使用log4Net的C#项目,并且在log4Net.config文件中,将地址+端口添加到了logstash服务器(Kibana)中,并设法将日志文件集成到其中。

但是现在在我的Node.js项目中,我没有尝试过

我有一个单独的js文件用于记录,它看起来如下:

const config = require('config');
const winston = require('winston');
require('winston-daily-rotate-file');

const elasticsearch = require('winston-elasticsearch');

const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: my_logstash_ip_address })

const esTransportOpts = {
    client: client
  };

const transport = new(winston.transports.DailyRotateFile)({
    filename: config.log.absPath,
    datePattern: 'YYYY-MM-DD',
    prepend: true,
    level: config.log.logLevel
});

const logger = winston.createLogger({
    transports: [
        transport,
        new elasticsearch(esTransportOpts)
    ]
});

module.exports = {
    writeToLog(level, message) {
        if (message) {
            const date = new Date();
            if (level === 'debug')
                logger.debug(`${date.toJSON()} ${message}`);
            else if (level === 'info')
                logger.info(`${date.toJSON()} ${message}`);
            else if (level === 'error')
                logger.error(`${date.toJSON()} ${message}`);
            else
                logger.error(`${date.toJSON()} not a valid log level for: ${message}`);
        } else {
            logger.error(`${new Date()} log message cannot be empty!`);
        }
    }
}

我收到以下错误:

null: ResponseError: Response Error
body: Object
headers: Object
message: "Response Error"
meta: Object {body: Object, statusCode: 404, headers: Object, …}
name: "ResponseError"
stack: "ResponseError: Response Error
    at IncomingMessage.response.on (p:\...\node_modules\@elastic\elasticsearch\lib\Transport.js:302:25)
    at IncomingMessage.emit (events.js:194:15)
    at endReadableNT (_stream_readable.js:1103:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)"
statusCode: 404
__proto__: ElasticsearchClientError {constructor: , body: <accessor>, statusCode: <accessor>, …}

我在winston.js github页面上阅读了它们使用传输逻辑进行的更改,现在我可能需要使用“格式”功能?

但是后来我也看到了类似上面添加的代码的内容,这对我也没有帮助。

我的目标是设法将Winston记录器连接到Logstash服务器(Kibana),以便在Logstash / Kibana服务器上看到我的日志

1 个答案:

答案 0 :(得分:0)

> npm install winston@2.4.1
> npm install winston-logstash@0.4.0

然后

const winston = require('winston');
require('winston-logstash');

winston.add(winston.transports.Logstash,
{
    port: your-port,
    host: 'your-logstash-host',
    ssl_enable: true,
    max_connect_retries: -1,
});

...

winston.error('This is a test error log message', { custom: 'my custom field', Environment: 'local' });
相关问题