node-gelf
GrayLog2 GELF UDP logging, streaming, chunking, and more. Production tested. Includes client and server implementations. AFAIK a complete GELF implementation. Based on wavded/graygelf
Install
npm install node-gelf
Example
var log = 'graylog.server.local'log // output messages to console // setup global custom fields to be passed with every messagelogfieldsfacility = 'redicomps' // printf style "hello world"log // concat by space style "hello world"log // stays context awareredis // include a full message and custom fields using .a(ttach)loginfologinfo // if an Error is passed as the only argument...var er = 'oh no!'log// ... it expands to:loginfo // writable streams can be createdvar infostream = logvar rstream = rstream // lines automatically split up and sent seperately // raw gelf: version, host, and timestamp will be supplied if missinglog
Setup
By host
string (uses defaults below for other options):
var log = 'graylog.server.local'
By options
object:
var log = { host: 'graylog.server.local' port: 23923)
Available options
are:
host
- graylog host (default: 'localhost')
port
- graylog port (default: 12201)
chunkSize
- size of chunked messages in bytes (default: 1240)
compressType
- compression 'gzip' or 'deflate' (default: 'deflate')
mock
- don't send messages to GrayLog2 (default: false)
API
event: error
Emits errors that may occur while parsing and sending GELF messages.
event: message
Emits GELF JSON messages that will be send over UDP. Useful for redirecting output to stdout in development.
log
log.fields
Add global custom fields to be included in every message. Custom fields allow you to more interesting searches and sorting inside GrayLog2 servers.
logfieldsfacility = 'facility'
Note: fields
is plain JavaScript object.
log{level}(message)
GrayGelf maps the syslog levels to functions. All functions have the same semantics as console.log
(i.e. printf style):
log // 0 - alias: paniclog // 1log // 2log // 3 - alias: errlog // 4 - alias: warninglog // 5log // 6log // 7
log{level}.a(short, long, custom)
There also is an a(ttach)
method to include a full message.
logcrit
The a(ttach)
method can have an optional third argument to define custom fields that will be passed to Graylog2.
loginfo
log.stream(level)
Create a writable stream to pipe log messages into:
var stream = log
Streams automatically break lines up and pass each line to GrayLog2 at the specified level.
log.raw(gelf)
Pass a raw GELF message. The following fields will be populated if absent: version
, host
, and timestamp
.
log
Note: No global custom fields (log.fields
) are included when using log.raw
.
Server
Make your own GrayLog UDP server or proxy messages to GrayLog. A GrayGelf server handles zlib
, gzip
and GELF chunked messages.
Example
var gelfserver = var server = serverserver
event: message
Emits parsed GELF JSON messages.
event: data
Emits raw GELF buffers (useful for proxying).
event: error
Emits errors captured from udp or parsing.
server.listen(port = 12201, address = "0.0.0.0")
Start listening on a port and bind address. Both parameters are optional. Defaults to typical GrayLog2 server defaults.
server.close()
Close down a server and stop receiving messages.
server.unref()
Allow the Node process to terminate if the server is the only thing keeping it alive.
server.pipe(client)
var server = var client = 'proxy-dest.graylog.local' server // establish proxy (straight UDP transfer)