mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
2.1 KiB
2.1 KiB
Logstash UDP Appender
This appender sends log events to a logstash server via UDP. It uses the node.js core UDP support, and so requires no extra dependencies. Remember to call log4js.shutdown in your application if you want the UDP socket closed cleanly.
Configuration
type-logstashUDPhost-string- hostname (or IP-address) of the logstash serverport-integer- port of the logstash serverlogType-string(optional) - used for thetypefield in the logstash datacategory-string(optional) - used for thetypefield of the logstash data iflogTypeis not definedfields-object(optional) - extra fields to log with each event. User-defined fields can be either a string or a function. Functions will be passed the log event, and should return a string.layout- (optional, defaults to dummyLayout) - used for themessagefield of the logstash data (see layouts)args- (optional, defaults to both) - determines how to log arguments and configuration fields:directlogs them as direct properties of the log object,fieldslogs them as child properties of thefieldsproperty, andbothlogs both.
Example
log4js.configure({
appenders: {
logstash: {
type: 'logstashUDP',
host: 'log.server',
port: '12345',
logType: 'application',
fields: { biscuits: 'digestive', tea: 'tetley', user: function(logEvent) {
return AuthLibrary.currentUser();
}
}
}
},
categories: {
default: { appenders: ['logstash'], level: 'info' }
}
});
const logger = log4js.getLogger();
logger.info("important log message", { cheese: 'gouda', biscuits: 'hobnob' });
This will result in a JSON message being sent to log.server:12345 over UDP, with the following format:
{
'@version': '1',
'@timestamp': '2014-04-22T23:03:14.111Z',
'type': 'application',
'message': 'important log message',
'fields': {
'level': 'INFO',
'category': 'default',
'biscuits': 'hobnob',
'user': 'charlie',
'cheese': 'gouda',
'tea': 'tetley'
}
}