我正在尝试使用较新版本的winston.js (3+)和logstash(kibana)。
我得到了C#项目,其中我使用了log4Net,并在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服务器上看到我的日志
发布于 2019-09-07 02:34:47
> 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' });https://stackoverflow.com/questions/57142025
复制相似问题