Skip to content

Commit

Permalink
Merge branch 'logger' of https://github.com/easybib/statsd into logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schauenberg committed Jul 20, 2012
2 parents f7e7801 + 0638da7 commit 70e688b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
5 changes: 5 additions & 0 deletions exampleConfig.js
Expand Up @@ -39,6 +39,11 @@ 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 [string, default: stdout]
application: name of the application for syslog [string, default: statsd]
level: log level for [node-]syslog [string, default: LOG_INFO]
*/
{
graphitePort: 2003
Expand Down
38 changes: 38 additions & 0 deletions lib/logger.js
@@ -0,0 +1,38 @@
var Logger = function (config) {
this.config = config;
this.backend = this.config.backend || 'stdout'
this.level = this.config.level || "LOG_INFO"
if (this.backend == 'stdout') {
this.util = require('util');
} else {
if (this.backend == 'syslog') {
this.util = require('node-syslog');
this.util.init(config.application || '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, type) {
if (this.backend == 'stdout') {
if (!type) {
type = 'DEBUG: ';
}
this.util.log(DEBUG + msg);
} else {
if (!type) {
type = this.level
if (!this.util[type]) {
throw "Undefined log level: " + type;
}
} else if (type == 'debug') {
type = "LOG_DEBUG";
}
this.util.log(this.util[type], msg);
}
}
}

exports.Logger = Logger
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -18,6 +18,9 @@
"underscore": "1.2.x",
"temp": "0.4.x"
},
"optionalDependencies": {
"node-syslog":"1.1.3"
},
"engine": {
"node" : ">=0.4"
},
Expand Down
26 changes: 18 additions & 8 deletions stats.js
Expand Up @@ -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 = {};
Expand All @@ -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, 'debug');
}

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);
}
};
Expand Down Expand Up @@ -70,18 +71,25 @@ 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); }
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));
"\nGauges:\n" + util.inspect(gauges), 'debug');
}, config.debugInterval || 10000);
}

Expand All @@ -95,7 +103,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, '_')
Expand All @@ -117,7 +127,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;
Expand Down Expand Up @@ -189,7 +199,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);
Expand Down

0 comments on commit 70e688b

Please sign in to comment.