all files / graygelf/ index.js

98.28% Statements 114/116
100% Branches 50/50
93.33% Functions 14/15
99% Lines 99/100
2 statements, 2 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                                 48× 24×     24× 24×   24× 24× 24×     24× 24× 24× 24×   24× 23× 23× 23×     24× 24×       24×   24× 264×   264× 11× 11×       11× 11×     264×         34× 33×       22×               22× 22×   22× 22×   22× 22×     24× 24×   24×     17×               18× 18× 18× 18× 18× 18× 18×   18× 18× 18×                        
var zlib = require('zlib')
var dgram = require('dgram')
var os = require('os')
var crypto = require('crypto')
var Stream = require('stream')
var format = require('util').format
var split = require('split')
 
/**
 * Prefer microtime accuracy but fallback if unable to use
 */
var timestamp
try { timestamp = require('microtime').nowDouble }
catch (e) { timestamp = function() { return Date.now() / 1000.0 } }
 
/**
 * Available logging levels
 * @type {Array}
 */
var LOG_LEVELS = {
  emerg: 0,
  panic: 0,
  alert: 1,
  crit: 2,
  error: 3,
  err: 3,
  warn: 4,
  warning: 4,
  notice: 5,
  info: 6,
  debug: 7,
}
 
var GrayGelf = function(opts) {
  /* eslint complexity:0 */
  if (!(this instanceof GrayGelf)) {
    return new GrayGelf(opts)
  }
 
  if (typeof opts === 'string') opts = {host: opts}
  if (!opts) opts = {}
 
  this.graylogHost = opts.host || 'localhost'
  this.graylogPort = opts.port || 12201
  this.fields = Object.create(null)
 
 
  this.chunkSize = opts.chunkSize || GrayGelf.CHUNK_WAN
  this.compressType = (opts.compressType || '') === 'gzip' ? 'gzip' : 'deflate'
  this.alwaysCompress = opts.alwaysCompress || false
  this.hostname = os.hostname()
 
  if (!opts.mock) {
    this._udp = dgram.createSocket('udp4')
    this._udp.on('error', this._emitError.bind(this))
    this._udp.unref()
  }
 
  this.writable = true // writable stream
  this._setupLevels()
}
 
GrayGelf.prototype = Object.create(Stream.prototype)
 
GrayGelf.prototype._setupLevels = function() {
  var graygelf = this
 
  Object.keys(LOG_LEVELS).forEach(function(name) {
    var level = LOG_LEVELS[name]
 
    graygelf[name] = function() {
      var gelf
      if (arguments.length === 1 && arguments[0] instanceof Error) {
        gelf = graygelf._prepGelf(level, arguments[0].message, arguments[0].stack)
      }
      else {
        gelf = graygelf._prepGelf(level, format.apply(null, arguments))
      }
      graygelf._send(gelf)
      return gelf
    }
 
    graygelf[name].a = function(short, long, fields) {
      var gelf = graygelf._prepGelf(level, short, long, fields)
      graygelf._send(gelf)
      return gelf
    }
  })
}
 
GrayGelf.prototype.write = function(chunk) {
  if (!this._udp) return
  this._udp.send(chunk, 0, chunk.length, this.graylogPort, this.graylogHost)
}
 
GrayGelf.prototype._emitError = function(er) { this.emit('error', er) }
 
GrayGelf.prototype._prepGelf = function(level, short, long, fields) {
  var gelf = {
    version: '1.1',
    host: this.hostname,
    short_message: short,
    timestamp: timestamp(),
    level: level,
  }
 
  if (!fields) fields = {}
  if (long) gelf.full_message = long
 
  for (var i in this.fields) gelf['_' + i] = this.fields[i]
  for (var j in fields) gelf['_' + j] = fields[j]
 
  this.emit('message', gelf)
  return gelf
}
 
GrayGelf.prototype._send = function(gelf) {
  var gelfbuf = new Buffer(JSON.stringify(gelf))
  var graygelf = this
 
  if (gelfbuf.length < graygelf.chunkSize && !graygelf.alwaysCompress) {
    // The buffer fits within the nominal chunksize,
    // so compression can be bypassed and sent directly
    return graygelf.write(gelfbuf)
  }
 
  zlib[this.compressType](gelfbuf, function(er, message) {
    /* istanbul ignore if */
    Iif (er) return graygelf.emit('error', er)
 
    if (message.length > graygelf.chunkSize) {
      var total = Math.ceil(message.length / graygelf.chunkSize)
      var offset = 0
 
      crypto.randomBytes(6, function(er2, idBuf) {
        /* istanbul ignore if */
        Iif (er2) return graygelf.emit('error', er2)
 
        for (var i = 0; i < total; i++) {
          var bytesToSend = offset + graygelf.chunkSize < message.length ? graygelf.chunkSize : message.length - offset
          var chunk = new Buffer(bytesToSend + 12)
          chunk[0] = 0x1e
          chunk[1] = 0x0f
          idBuf.copy(chunk, 2, 0, 6)
          chunk[10] = i
          chunk[11] = total
 
          message.copy(chunk, 12, offset, offset + bytesToSend)
          offset += bytesToSend
          graygelf.write(chunk)
        }
      })
    }
    else graygelf.write(message)
  })
}
 
GrayGelf.prototype.stream = function(name) {
  if (!(name in LOG_LEVELS)) throw new Error('invalid stream name')
  var stream = new Stream()
  var lines = split()
  stream.writable = true
  stream.write = this[name].bind(this)
  stream.end = function() {} // 'end' event is noop, stream always stays open
  lines.pipe(stream)
  return lines
}
 
GrayGelf.prototype.raw = function(raw) {
  if (!raw.version) raw.version = '1.1'
  if (!raw.host) raw.host = this.hostname
  if (!raw.timestamp) raw.timestamp = timestamp()
  this._send(raw)
  return raw
}
 
GrayGelf.CHUNK_WAN = 1240
GrayGelf.CHUNK_LAN = 8154
GrayGelf.LOG_LEVELS = LOG_LEVELS
 
module.exports = GrayGelf