node-gelf pro
node-gelf - Graylog2 client library for Node.js. Pro - because of code-quality. GELF - The Graylog Extended Log Format.
Installation
"dependencies": {
"gelf-pro": "~1.2" // see the "releases" section
}
npm install gelf-pro
(ALL node.js versions are supported :)
Initialization
var log = require('gelf-pro');
Adapters
- UDP (with deflation and chunking)
- TCP
- Input:
GELF TCP
(with Null frame delimiter
)
- TCP via TLS(SSL)
- Input:
GELF TCP
(with Null frame delimiter
and Enable TLS
)
Configuration
log.setConfig({host: 'my.glog-server.net'});
log.setConfig({
fields: {facility: "example", owner: "Tom (a cat)"},
filter: [],
transform: [],
broadcast: [],
levels: {},
aliases: {},
adapterName: 'udp',
adapterOptions: {
host: '127.0.0.1',
port: 12201,
family: 4,
timeout: 1000,
protocol: 'udp4',
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
ca: [fs.readFileSync('server-cert.pem')]
}
});
log.setConfig
merges the data. Therefore you can call it multiple times.
Basic functionality
var extra = {tom: 'cat', jerry: 'mouse', others: {spike: 1, tyke: 1}};
log.info("Hello world", extra, function (err, bytesSent) {});
log.info("Hello world", function (err, bytesSent) {});
log.info("Hello world", extra);
log.info("Hello world");
log.error('Oooops.', new Error('An error message'));
log.error(new Error('An error message'));
log.message(new Error('An error message'), 3);
In case extra
is a plain object,
the library converts it to a readable format. Other values are converted to string.
Acceptable format of a key is: ^[\w-]$
log.info(
'a new msg goes here',
{me: {fname: 'k', lname: 'k', bdate: new Date(2000, 01, 01)}}
);
Filtering
Sometimes we have to discard a message which is not suitable for the current environment. It is NOT
possible to modify the data.
log.setConfig({
filter: [
function (message) {
return (message.level < 7);
}
]
});
Transforming
transforming
happens after filtering
. It is possible to modify the data.
log.setConfig({
transform: [
function (message) {
if (_.isError(message.error)) {
message.error = {message: message.error.message, stack: message.error.stack};
}
return message;
}
]
});
Broadcasting
broadcasting
happens after transforming
. It is NOT
possible to modify the data.
log.setConfig({
broadcast: [
function (message) {
console[message.level > 3 ? 'log' : 'error'](message.short_message, message);
}
]
});
Levels (1, 2, 3)
Default:
{emergency: 0, alert: 1, critical: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7}
Example: log.emergency(...)
, log.critical(...)
, etc.
Custom example: {alert: 0, notice: 1, ...}
Aliases
Default: {log: 'debug', warn: 'warning'}
Example: log.log(...) -> log.debug(...)
, log.warn(...) -> log.warning(...)
, etc.
Custom example: {red: 'alert', yellow: 'notice', ...}
Tests
Cli
npm install
npm test
Docker
[sudo] docker build --no-cache -t node-gelf-pro .
[sudo] docker run -ti --rm -v "${PWD}:/opt/app" -w "/opt/app" node-gelf-pro
Contributors
License
The MIT License (MIT)
Copyright (c) 2013-2018 Kanstantsin Kamkou