Permalink
Browse files

Log to syslog:

 * replace 'most' log-related instances of util with a logger object
 * logger object sets up syslog logging (if configured)
 * listed 'node-syslog' as an optional dependency
  • Loading branch information...
1 parent d0999e6 commit 626277067bbf54da93a6df11f9f1dc68dbcceb14 @till till committed Jul 11, 2012
Showing with 46 additions and 6 deletions.
  1. +3 −0 exampleConfig.js
  2. +26 −0 lib/logger.js
  3. +3 −0 package.json
  4. +14 −6 stats.js
View
@@ -39,6 +39,9 @@ Optional Variables:
prettyprint: whether to prettyprint the console backend
output [true or false, default: true]
+ log: log settings [object, default: undefined]
+ backend: where to log: stdout or syslog [default: stdout]
+
*/
{
graphitePort: 2003
View
@@ -0,0 +1,26 @@
+var Logger = function (config) {
+ this.config = config;
+ this.backend = this.config.backend || 'stdout'
+ if (this.backend == 'stdout') {
+ this.util = require('util');
+ } else {
+ if (this.backend == 'syslog') {
+ this.util = require('node-syslog');
+ this.util.init("statsd", this.util.LOG_PID | this.util.LOG_ODELAY, this.util.LOG_LOCAL0);
+ } else {
+ throw "Logger: Should be 'stdout' or 'syslog'."
+ }
+ }
+}
+
+Logger.prototype = {
+ log: function (msg) {
+ if (this.backend == 'stdout') {
+ this.util.log(msg);
+ } else {
+ this.util.log(this.util.LOG_INFO, msg);
+ }
+ }
+}
+
+exports.Logger = Logger
View
@@ -18,6 +18,9 @@
"underscore": "1.2.x",
"temp": "0.4.x"
},
+ "optionalDependencies": {
+ "node-syslog":"1.1.3"
+ },
"engine": {
"node" : ">=0.4"
},
View
@@ -4,6 +4,7 @@ var dgram = require('dgram')
, config = require('./config')
, fs = require('fs')
, events = require('events')
+ , logger = require('./lib/logger')
// initialize data structures with defaults for statsd stats
var keyCounter = {};
@@ -25,12 +26,12 @@ function loadBackend(config, name) {
var backendmod = require(name);
if (config.debug) {
- util.log("Loading backend: " + name);
+ l.log("Loading backend: " + name);
}
var ret = backendmod.init(startup_time, config, backendEvents);
if (!ret) {
- util.log("Failed to load backend: " + name);
+ l.log("Failed to load backend: " + name);
process.exit(1);
}
};
@@ -70,16 +71,21 @@ var stats = {
}
};
+// Global for the logger
+var l;
+
config.configFile(process.argv[2], function (config, oldConfig) {
if (! config.debug && debugInt) {
clearInterval(debugInt);
debugInt = false;
}
+ l = new logger.Logger(config.log || {});
+
if (config.debug) {
if (debugInt !== undefined) { clearInterval(debugInt); }
debugInt = setInterval(function () {
- util.log("Counters:\n" + util.inspect(counters) +
+ l.log("Counters:\n" + util.inspect(counters) +
"\nTimers:\n" + util.inspect(timers) +
"\nGauges:\n" + util.inspect(gauges));
}, config.debugInterval || 10000);
@@ -95,7 +101,9 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var metrics = msg.toString().split("\n");
for (midx in metrics) {
- if (config.dumpMessages) { util.log(metrics[midx].toString()); }
+ if (config.dumpMessages) {
+ l.log(metrics[midx].toString());
+ }
var bits = metrics[midx].toString().split(':');
var key = bits.shift()
.replace(/\s+/g, '_')
@@ -117,7 +125,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var sampleRate = 1;
var fields = bits[i].split("|");
if (fields[1] === undefined) {
- util.log('Bad line: ' + fields);
+ l.log('Bad line: ' + fields);
counters["statsd.bad_lines_seen"]++;
stats['messages']['bad_lines_seen']++;
continue;
@@ -189,7 +197,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
// Let each backend contribute its status
backendEvents.emit('status', function(err, name, stat, val) {
if (err) {
- util.log("Failed to read stats for backend " +
+ l.log("Failed to read stats for backend " +
name + ": " + err);
} else {
stat_writer(name, stat, val);

0 comments on commit 6262770

Please sign in to comment.