This commit is contained in:
Daniel Schauenberg 2012-07-20 13:35:50 +02:00
Родитель f7e7801a20 0638da7c49
Коммит 70e688bdbd
4 изменённых файлов: 64 добавлений и 8 удалений

Просмотреть файл

@ -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

38
lib/logger.js Normal file
Просмотреть файл

@ -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

Просмотреть файл

@ -18,6 +18,9 @@
"underscore": "1.2.x",
"temp": "0.4.x"
},
"optionalDependencies": {
"node-syslog":"1.1.3"
},
"engine": {
"node" : ">=0.4"
},

Просмотреть файл

@ -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, '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);
}
};
@ -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);
}
@ -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, '_')
@ -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;
@ -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);