Skip to content
Browse files

Get the basic thing working

  • Loading branch information...
1 parent 995758a commit 1306f39e2c0b59714a5bb1335a9902311e7d4b14 @pquerna committed Mar 19, 2011
Showing with 71 additions and 25 deletions.
  1. +71 −4 lib/logmagic.js
  2. +0 −21 lib/proxy.js
View
75 lib/logmagic.js
@@ -15,7 +15,10 @@
* limitations under the License.
*/
-var LoggerProxy = require('./proxy').LoggerProxy;
+function LoggerProxy(modulename) {
+ this.modulename = modulename;
+ this.loglevel = -1;
+}
/* Based on the Log levels available in Apache HTTP Server. */
exports.EMERG = 0; /* system is unusable */
@@ -35,20 +38,84 @@ exports.TRACE6 = 13; /* trace-level 6 messages */
exports.TRACE7 = 14; /* trace-level 7 messages */
exports.TRACE8 = 15; /* trace-level 8 messages */
+var log_levels = ["EMERG",
+ "ALERT",
+ "CRIT",
+ "ERR",
+ "WARNING",
+ "NOTICE",
+ "INFO",
+ "DEBUG",
+ "TRACE1",
+ "TRACE2",
+ "TRACE3",
+ "TRACE4",
+ "TRACE5",
+ "TRACE6",
+ "TRACE7",
+ "TRACE8"];
+
+var log_aliases = {"WARN": "WARNING",
+ "ERROR": "ERR",
+ "DBG": "DEBUG",
+ "TRACE": "TRACE1"};
+
var known_sinks = {};
var known_loggers = [];
var known_routes = [];
+function buildLogMethod(level, callback) {
+ return function (msg, extra) {
+ callback(level, msg, extra)
+ }
+}
+
+function nullLogger() {
+ /* Intentionally blank. */
+}
+
+function applyRoute(route, logger) {
+ logger.loglevel = route.loglevel;
+ for(var i=0; i<log_levels.length; i++) {
+ var level = log_levels[i];
+ var v = exports[level];
+
+ if (v <= route.loglevel) {
+ logger[level.toLowerCase()] = buildLogMethod(v, route.callback);
+ }
+ else {
+ logger[level.toLowerCase()] = nullLogger;
+ }
+ }
+
+ for (var key in log_aliases) {
+ if (log_aliases.hasOwnProperty(key)) {
+ logger[key.toLowerCase()] = logger[log_aliases[key].toLowerCase()];
+ }
+ }
+}
+
+function routeMatch() {
+ return False;
+}
+
function applyRoutes(logger) {
for(var i=0; i < known_routes.length; i++) {
- var route = known_routes[i];
+ var r = known_routes[i];
+ if (r.route == "__root__") {
+ applyRoute(r, logger);
+ }
+ else if (routeMatch(r.route, logger.modulename)) {
+ applyRoute(r, logger);
+ }
}
}
exports.local = function(modulename) {
- var logger = LoggerProxy(modulename);
+ var logger = new LoggerProxy(modulename);
applyRoutes(logger);
known_loggers.push(logger);
+ return logger;
};
exports.registerSink = function(sinkname, callback) {
@@ -66,7 +133,7 @@ exports.route = function(match, loglevel, sinkname) {
throw new Error("Invalid Sink: " + sinkname);
}
- known_loggers.push({route: match, loglevel: loglevel, callback: known_sinks[sinkname]});
+ known_routes.push({route: match, loglevel: loglevel, callback: known_sinks[sinkname]});
for(var i=0; i<known_loggers.length; i++) {
var logger = known_loggers[i];
View
21 lib/proxy.js
@@ -1,21 +0,0 @@
-/*
- * 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 1306f39

Please sign in to comment.
Something went wrong with that request. Please try again.