Skip to content

Commit

Permalink
Sketch the basics of the module out.
Browse files Browse the repository at this point in the history
  • Loading branch information
pquerna committed Mar 19, 2011
1 parent 2e2dabd commit aad892d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ making it easy to change log levels for specific modules dynamically.

var logmagic = require('logmagic');
logmagic.registerSink("mysink", function(level, message) { console.log(message); });

/* Send Info an higher in the root logger to stdout */
logmagic.route("__root__", logmagic.INFO, "stdout")

/* Reconfigure all children of mylib to log all debug messages to your custom sink */
logmagic.route("mylib.*", logmagic.DEBUG, "mysink")


Builtin sinks include:

* Standard Out, Standard Error
Expand Down
70 changes: 70 additions & 0 deletions lib/logmagic.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,73 @@
* limitations under the License.
*/

var LoggerProxy = require('./proxy').LoggerProxy;

/* Based on the Log levels available in Apache HTTP Server. */
exports.EMERG = 0; /* system is unusable */
exports.ALERT = 1; /* action must be taken immediately */
exports.CRIT = 2; /* critical conditions */
exports.ERR = 3; /* error conditions */
exports.WARNING = 4; /* warning conditions */
exports.NOTICE = 5; /* normal but significant condition */
exports.INFO = 6; /* informational */
exports.DEBUG = 7; /* debug-level messages */
exports.TRACE1 = 8; /* trace-level 1 messages */
exports.TRACE2 = 9; /* trace-level 2 messages */
exports.TRACE3 = 10; /* trace-level 3 messages */
exports.TRACE4 = 11; /* trace-level 4 messages */
exports.TRACE5 = 12; /* trace-level 5 messages */
exports.TRACE6 = 13; /* trace-level 6 messages */
exports.TRACE7 = 14; /* trace-level 7 messages */
exports.TRACE8 = 15; /* trace-level 8 messages */

var known_sinks = {};
var known_loggers = [];
var known_routes = [];

function applyRoutes(logger) {
for(var i=0; i < known_routes.length; i++) {
var route = known_routes[i];
}
}

exports.local = function(modulename) {
var logger = LoggerProxy(modulename);
applyRoutes(logger);
known_loggers.push(logger);
};

exports.registerSink = function(sinkname, callback) {
known_sinks[sinkname] = callback;
};

exports.route = function(match, loglevel, sinkname) {

if (!(loglevel >= exports.EMERG && loglevel <= exports.TRACE8)) {
throw new Error("Invalid Log level: " + loglevel);
}

/* TODO: Maybe it is okay to route before we have a sink loaded (?) */
if (known_sinks[sinkname] === undefined) {
throw new Error("Invalid Sink: " + sinkname);
}

known_loggers.push({route: match, loglevel: loglevel, callback: known_sinks[sinkname]});

for(var i=0; i<known_loggers.length; i++) {
var logger = known_loggers[i];
applyRoutes(logger);
}
};

(function() {
/* Default Sinks */

/* This is just here for initial dev work, REMOVE ME */
exports.registerSink("console", function(level, message) {
console.log(message);
});

/* Default loggers */
exports.route("__root__", exports.INFO, "console");
})();
21 changes: 21 additions & 0 deletions lib/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Paul Querna under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* Paul Querna licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

function LoggerProxy(modulename) {
this.modulename = modulename;
this.loglevel = -1;
}

0 comments on commit aad892d

Please sign in to comment.