
var bunyan = require('bunyan'),
    syslog = require('bunyan-syslog'),
    _      = require('underscore')

var log 

exports.init = function(config) {
  config = _.defaults(config.syslog || {}, {
    type:     'udp',
    host:     '127.0.0.1',
    port:     514,
    facility: 'local0',
  })

  log = bunyan.createLogger({
    name: 'cloudwatch',
    streams: [{
      level: 'info',
      type: 'raw',
      stream: syslog.createBunyanStream({
        type: config.type,
        facility: syslog[config.facility],
        host: config.host,
        port: Number(config.port)
      })
    }]
  })

  return log ? 1 : 0
}

exports.send = function(time, metric, value) {
  console.log('[syslog]', time, metric.MetricName, value)

  var message = {
    time: time.toISOString(),
    Namespace:  metric.Namespace,
    MetricName: metric.MetricName,
    Unit:       metric.Unit,
    Statistic:  metric.Statistic,
    Value:      value,
  }

  _.each(metric.Dimensions, function(d) {
    message[d.Name] = d.Value
  })

  log.info(message)
}

@@ -209,88 +209,242 @@
    '
-udp
+sys
 ',%0A    
-host:     '127.0.0.1',%0A    port:     514,%0A    facility: 'local0',%0A  %7D)
+facility: 'local0',%0A  %7D)%0A%0A  var streamOptions = %7B%0A    type: config.type,%0A    facility: syslog%5Bconfig.facility%5D,%0A  %7D%0A%0A  if (config.type != 'sys') %7B%0A    streamOptions.host = config.host%0A    streamOptions.port = config.port%0A  %7D
 %0A%0A  
@@ -595,148 +595,21 @@
 eam(
-%7B%0A        type: config.type,%0A        facility: syslog%5Bconfig.facility%5D,%0A        host: config.host,%0A        port: Number(config.port)%0A      %7D
+streamOptions
 )%0A  

support other syslog types than just udp