Permalink
Browse files

Move to full JavaScript

  • Loading branch information...
1 parent ab5dc82 commit 3f708c0856a13f5b136af5ceed46007a554dd567 @mhart committed Jan 17, 2013
Showing with 138 additions and 280 deletions.
  1. +4 −0 LICENSE.txt
  2. +2 −3 README.md
  3. +7 −5 example.js
  4. +0 −98 gelfling.coffee
  5. +115 −170 gelfling.js
  6. +10 −4 package.json
View
@@ -0,0 +1,4 @@
+Copyright 2013 Michael Hart (michael.hart.au@gmail.com)
+
+This project is free software released under the MIT license:
+http://www.opensource.org/licenses/mit-license.php
View
@@ -3,9 +3,6 @@
Includes chunked messages, so messages can be any size
(couldn't find another node.js lib that does this)
-More to come soon...
-(will move from CoffeeScript to pure JS once it's a bit more stable)
-
```javascript
var gelfling = require('gelfling')
@@ -15,6 +12,8 @@ client.send('Message', function(err) { console.log('Sent') })
client.send({ short_message: 'Message', facility: 'myApp', level: gelfling.INFO })
+client.send('Close when finally done', function() { client.close() })
+
var complexClient = gelfling('localhost', 12201, {
defaults: {
facility: 'myApp',
View
@@ -1,7 +1,7 @@
-var gelfling = require('./gelfling.js')
+var gelfling = require('./') // require('gelfling')
var msg = {
- short_message: "Message at " + +new Date,
+ short_message: "Message at " + (+new Date),
full_message: ("start/" + new Buffer(20000).toString('base64') + "/end").replace(/\//g, "\n"),
id: 34,
some_other_field: "Dude!\nIt's a multi line\nMessage!",
@@ -11,7 +11,9 @@ var msg = {
//line: 345
}
-//gelfling().send("Message at #{+new Date}", (err) -> console.log "all done")
-gelfling('localhost', 12201, { defaults: { line: function(msg) { return msg.id } } })
- .send(msg, function(err) { console.log("all done") })
+var client = gelfling('log.adslot.com', 12201, { defaults: { line: function(msg) { return msg.id } } })
+client.send(msg, function(err) {
+ console.log("all done")
+ client.close()
+})
View
@@ -1,98 +0,0 @@
-zlib = require 'zlib'
-dgram = require 'dgram'
-crypto = require 'crypto'
-
-exports = module.exports = (host, port, options) ->
- new Gelfling host, port, options
-
-# From https://github.com/Graylog2/graylog2-docs/wiki/GELF
-# and https://github.com/Graylog2/gelf-php/blob/master/GELFMessage.php
-exports.EMERGENCY = 0
-exports.ALERT = 1
-exports.CRITICAL = 2
-exports.ERROR = 3
-exports.WARNING = 4
-exports.NOTICE = 5
-exports.INFO = 6
-exports.DEBUG = 7
-
-exports.Gelfling = class Gelfling
-
- constructor: (@host = 'localhost', @port = 12201, options = {}) ->
- @maxChunkSize = @getMaxChunkSize options.maxChunkSize
- @defaults = options.defaults ? {}
- @udpClient = dgram.createSocket 'udp4'
- @udpClient.on 'error', options.errHandler ? console.error
-
- send: (data, callback = ->) ->
- data = [data] if Buffer.isBuffer data
-
- unless Array.isArray data
- return @encode @convert(data), (err, chunks) =>
- return callback err if err
- @send chunks, callback
-
- remaining = data.length
- for chunk in data
- @udpClient.send chunk, 0, chunk.length, @port, @host, (err) ->
- return callback err if err
- if --remaining is 0
- callback()
-
-
- close: ->
- @udpClient.close()
-
-
- encode: (msg, callback = ->) ->
- zlib.gzip new Buffer(JSON.stringify msg), (err, compressed) =>
- return callback err if err
- callback null, @split(compressed)
-
-
- GELF_ID = [0x1e, 0x0f]
-
- split: (data, chunkSize = @maxChunkSize) ->
- return [data] if data.length <= chunkSize
-
- msgId = Array::slice.call crypto.randomBytes(8)
- numChunks = Math.ceil data.length / chunkSize
- for chunkIx in [0...numChunks]
- dataStart = chunkIx * chunkSize
- dataSlice = Array::slice.call data, dataStart, dataStart + chunkSize
- new Buffer GELF_ID.concat msgId, chunkIx, numChunks, dataSlice
-
-
- GELF_KEYS = ['version', 'host', 'short_message', 'full_message', 'timestamp', 'level', 'facility', 'line', 'file']
- ILLEGAL_KEYS = ['_id']
-
- convert: (msg) ->
- msg = {short_message: msg} if typeof msg isnt 'object'
-
- gelfMsg = {}
-
- # Default fields
- for own key, val of @defaults
- gelfMsg[key] = if typeof val is 'function' then val(msg) else val
-
- # Msg fields
- for own key, val of msg
- key = '_' + key unless key in GELF_KEYS
- key = '_' + key if key in ILLEGAL_KEYS
- gelfMsg[key] = val
-
- # Required fields
- gelfMsg.version ?= '1.0'
- gelfMsg.host ?= require('os').hostname()
- gelfMsg.timestamp ?= +new Date / 1000
- gelfMsg.short_message ?= JSON.stringify msg
-
- gelfMsg
-
-
- getMaxChunkSize: (size = 'wan') ->
- switch size.toLowerCase()
- when 'wan' then 1420
- when 'lan' then 8154
- else parseInt size
-
Oops, something went wrong.

0 comments on commit 3f708c0

Please sign in to comment.