Permalink
Browse files

Compatibility with latest node.js 0.12.x

  • Loading branch information...
1 parent 61680d6 commit 152c2f788b7895acb4b012074058ddf3337227fe @igo committed Feb 22, 2015
Showing with 52 additions and 38 deletions.
  1. +42 −32 README.md
  2. +9 −5 lib/nlogger.js
  3. +1 −1 package.json
View
@@ -13,61 +13,71 @@ Usage
-----
Use npm or download. Then add to your code:
- var logger = require('nlogger').logger(module);
+```javascript
+var logger = require('nlogger').logger(module);
+```
*module* is object defined automatically by Node.js. If you don't want automatic module names, replace it with your desired string name.
- logger.info(message);
- logger.info(message, variable, message, variable...);
+```javascript
+logger.info(message);
+logger.info(message, variable, message, variable...);
+```
Examples
--------
- var logger = require('nlogger').logger(module);
- logger.info('Info message');
- logger.debug('Debug message');
- logger.warn('Warning message');
- logger.error('Error message');
- logger.trace('Trace message');
- logger.info('Array = ', [1, 2, 3, 4], ', Object = ', {one: 1, two: 2});
-
+```javascript
+var logger = require('nlogger').logger(module);
+logger.info('Info message');
+logger.debug('Debug message');
+logger.warn('Warning message');
+logger.error('Error message');
+logger.trace('Trace message');
+logger.info('Array = ', [1, 2, 3, 4], ', Object = ', {one: 1, two: 2});
+```
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:39:03.590 INFO main:10 - Array = [ 1, 2, 3, 4 ], Object = { one: 1, two: 2 }
-
- 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
-
+```
+2015-02-23 20:39:03.570 INFO main:5 - Info message
+2015-02-23 20:39:03.588 DEBUG main:6 - Debug message
+2015-02-23 20:39:03.589 WARN main:7 - Warning message
+2015-02-23 20:39:03.590 ERROR main:8 - Error message
+2015-02-23 20:39:03.590 TRACE main:9 - Trace message
+2015-02-23 20:39:03.590 INFO main:10 - Array = [ 1, 2, 3, 4 ], Object = { one: 1, two: 2 }
+
+2015-02-23 20:59:12.496 INFO my-modules/first:3 - Message from first module from line #3
+2015-02-23 20:59:12.514 INFO my-modules/second:10 - Message from second module from line #10
+2015-02-23 20:59:12.515 INFO fake-module-name:3 - Message from third module from line #3
+2015-02-23 20:59:12.516 INFO <unknown>:3 - Message from fourth module from line #3
+```
Configuration
-------------
nlogger can load optional configuration file nlogger.json which looks like:
- {
- "color": "auto",
- "level": {
- "*": "debug",
- "my-modules/first": "info"
- }
+```javascript
+{
+ "color": "auto",
+ "level": {
+ "*": "debug",
+ "my-modules/first": "info"
}
+}
+```
* `color` - print message in color? [true, false, "auto"]
* `level.*` - default debug level
-* `level.yourModuleName` - debug level for specified module
+* `level.yourModuleName` - log level for specified module
-Possible debug levels are `trace`, `debug`, `info`, `warn`, `error`.
+Possible log levels are `trace`, `debug`, `info`, `warn`, `error`.
Changes
-------
+1.0.0 - Compatibility with latest node.js 0.12.x
+
0.4.0 - Logging methods parameters changed
0.3.1 - Fixed util dependencies
@@ -81,4 +91,4 @@ Changes
License
-------
-Released under MIT License. Enjoy and Fork!
+Commercial license available at http://iostock.com/nlogger
View
@@ -2,7 +2,7 @@
nlogger library (formerly node-logger)
http://github.com/igo/nlogger
- Copyright (c) 2010 by Igor Urmincek
+ Copyright (c) 2010-2015 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
@@ -24,7 +24,8 @@
*/
var util = require('util'),
- fs = require('fs');
+ fs = require('fs'),
+ path = require('path');
function padZero(number) {
@@ -66,6 +67,9 @@ function getClass(module) {
if (module.id == '.') {
return 'main';
} else {
+ if (fs.existsSync(module.id)) { // module.id is file path
+ return path.relative(path.dirname(require.main.filename), path.dirname(module.id)) + path.sep + path.basename(module.id, '.js');
+ }
return module.id;
}
} else {
@@ -92,7 +96,7 @@ try {
var file = fs.readFileSync('./nlogger.json', 'binary'),
config = JSON.parse(file);
} catch(e) {
- util.puts(getDate() + ' WARN nlogger - Config file not found. Using default configuration.');
+ console.log(getDate() + ' WARN nlogger - Config file not found. Using default configuration.');
config = {};
}
@@ -120,13 +124,13 @@ exports.logger = function(module) {
if (useColor) {
logger[level] = function(msg) {
if (methods[level].priority >= priority) {
- util.puts('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments) + '\x1B[0m');
+ console.log('\x1B[' + methods[level].color + 'm' + getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments) + '\x1B[0m');
}
};
} else {
logger[level] = function(msg) {
if (methods[level].priority >= priority) {
- util.puts(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments));
+ console.log(getDate() + ' ' + levelStr + ' ' + getClass(module) +':' + getLine() + ' - ' + getMessage(arguments));
}
};
}
View
@@ -1,7 +1,7 @@
{
"name": "nlogger",
"description": "Logging lib that prints module name and line number",
- "version": "0.4.0",
+ "version": "1.0.0",
"homepage": "https://github.com/igo/nlogger",
"keywords": ["log", "logging", "logger"],
"author": "Igor Urminček",

0 comments on commit 152c2f7

Please sign in to comment.