Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 69bd99636e0ae817d4d4ec0da900245d104f920f 0 parents
Igor Urminček authored
2  .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+
47 README.md
@@ -0,0 +1,47 @@
+node-logger
+===========
+
+node-logger is a Node.js logging library that prints module name, current line number and also your messages :) Furthermore it gives you an option to print messages in color.
+
+
+Usage
+-----
+Download and add to your code:
+
+ var logger = require('node-logger').logger(module [, useColor]);
+
+*module* is object defined automatically by nodejs. Don't bother with it, just always type *module*.
+
+Examples
+--------
+
+ var logger = require('node-logger').logger(module);
+ logger.info('Info message');
+ logger.debug('Debug message');
+ logger.warn('Warning message');
+ logger.error('Error message');
+ logger.trace('Trace message');
+
+To print messages in color init logger with useColor set to true:
+
+ var logger = require('node-logger').logger(module, true);
+
+
+Output samples
+--------------
+
+ 2010-10-02 20:39:03.570 INFO main:5 - Info message
+ 2010-10-02 20:39:03.588 DEBUG main:6 - Debug message
+ 2010-10-02 20:39:03.589 WARN main:7 - Warning message
+ 2010-10-02 20:39:03.590 ERROR main:8 - Error message
+ 2010-10-02 20:39:03.590 TRACE main:9 - Trace message
+
+ 2010-10-02 20:59:12.496 INFO my-modules/first:3 - Message from first module from line #3
+ 2010-10-02 20:59:12.514 INFO my-modules/second:10 - Message from second module from line #10
+ 2010-10-02 20:59:12.515 INFO fake-module-name:3 - Message from third module from line #3
+ 2010-10-02 20:59:12.516 INFO <unknown>:3 - Message from fourth module from line #3
+
+
+License
+-------
+Released under MIT License. Enjoy and Fork!
9 examples/colors/app.js
@@ -0,0 +1,9 @@
+require.paths.unshift('../../lib');
+
+var logger = require('node-logger').logger(module, true);
+
+logger.info('Info message');
+logger.debug('Debug message');
+logger.warn('Warning message');
+logger.error('Error message');
+logger.trace('Trace message');
13 examples/modules/app.js
@@ -0,0 +1,13 @@
+require.paths.unshift('../../lib');
+
+var logger = require('node-logger').logger(module);
+var first = require('./my-modules/first');
+var second = require('./my-modules/second');
+var third = require('./my-modules/third');
+var fourth = require('./my-modules/fourth');
+
+setTimeout(function() {
+ logger.debug('Timed log');
+}, 1000);
+
+logger.info('Info message');
3  examples/modules/my-modules/first.js
@@ -0,0 +1,3 @@
+var logger = require('node-logger').logger(module);
+
+logger.info('Message from first module from line #3');
3  examples/modules/my-modules/fourth.js
@@ -0,0 +1,3 @@
+var logger = require('node-logger').logger();
+
+logger.info('Message from fourth module from line #3');
10 examples/modules/my-modules/second.js
@@ -0,0 +1,10 @@
+var logger = require('node-logger').logger(module);
+
+// some dummy empty lines
+
+
+
+
+
+
+logger.info('Message from second module from line #10');
3  examples/modules/my-modules/third.js
@@ -0,0 +1,3 @@
+var logger = require('node-logger').logger('fake-module-name');
+
+logger.info('Message from third module from line #3');
2  index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/node-logger');
113 lib/node-logger.js
@@ -0,0 +1,113 @@
+/*
+ node-logger library
+ http://github.com/igo/node-logger
+
+ Copyright (c) 2010 by Igor Urmincek
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+var sys = require('sys');
+
+
+function padZero(number) {
+ var n = String(number);
+ if (number < 10) {
+ return '0' + n;
+ } else {
+ return n;
+ }
+}
+
+function pad2Zeros(number) {
+ var n = String(number);
+ if (number < 10) {
+ return '00' + n;
+ } else if (number < 100) {
+ return '0' + n;
+ } else {
+ return n;
+ }
+}
+
+function getDate() {
+ var now = new Date();
+ return now.getFullYear() + '-' + padZero(now.getMonth()) + '-' + padZero(now.getDate()) + ' ' +
+ padZero(now.getHours()) + ':' + padZero(now.getMinutes()) + ':' + padZero(now.getSeconds()) + '.' + pad2Zeros(now.getMilliseconds());
+}
+
+function getLine(module) {
+ try {
+ throw new Error();
+ } catch(e) {
+ // now magic will happen: get line number from callstack
+ var line = e.stack.split('\n')[3].split(':')[1];
+ return line;
+ }
+}
+
+function getClass(module) {
+ if (module) {
+ if (module.id) {
+ if (module.id == '.') {
+ return 'main';
+ } else {
+ return module.id;
+ }
+ } else {
+ return module;
+ }
+ } else {
+ return '<unknown>';
+ }
+}
+
+function getMessage(msg) {
+ if (typeof msg == 'string') {
+ return msg;
+ } else {
+ return sys.inspect(msg, false, 10);
+ }
+}
+
+exports.logger = function(module, useColor) {
+ var useColor = useColor || false;
+ var methods = {'trace': 32, 'debug': 34, 'info': 30, 'warn': 35, 'error': 31};
+ var logger = {};
+
+ var defineMethod = function(level) {
+ var levelStr = level.toUpperCase();
+ if (levelStr.length == 4) levelStr += ' ';
+ if (useColor) {
+ logger[level] = function(msg) {
+ sys.puts('\x1B[' + methods[level] + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine(module) + ' - ' + getMessage(msg) + '\x1B[0m');
+ };
+ } else {
+ logger[level] = function(msg) {
+ sys.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine(module) + ' - ' + getMessage(msg));
+ };
+ }
+ }
+
+ for (var level in methods) {
+ defineMethod(level);
+ }
+
+ return logger;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.