[Pkg-javascript-commits] [node-log-driver] 16/49: tests and docs, and a slight interface change.

Bastien Roucariès rouca at moszumanska.debian.org
Thu Feb 22 12:57:01 UTC 2018


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-log-driver.

commit 4a71065ed77bd2ac25750609884516ac8478a68d
Author: cainus <gregg at caines.ca>
Date:   Tue Mar 26 11:48:04 2013 -0700

    tests and docs, and a slight interface change.
---
 README.md     | 53 +++++++++++++++++++++++++++++++++++++++++++++++-
 lib/index.js  | 18 ++++++++---------
 package.json  |  3 ++-
 test/index.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++--------
 4 files changed, 119 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md
index c25aea1..ae8f843 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,57 @@ node yourapp.js 2>&1 | logger
 ```
 
 ##Usage:
-Coming soon
+Getting the default logger:
+```javascript
+var logger = require('log-driver').logger;
+```
+
+This logger has levels 'error', 'warn', 'info', 'debug', and 'trace'.
+If you don't like those levels, change the default:
+
+```javascript
+var logger = require('log-driver')({
+  levels : ['superimportant', 'checkthisout', 'whocares' ]
+})
+logger.whocares("brangelina in lover's quarrel!");
+```
+
+Specifying what log level to log at to make logs less chatty:
+```javascript
+var logger = require('log-driver')({ level : "info" });
+logger.info("info test"); 
+logger.warn("warn test"); 
+logger.error("error test"); 
+logger.trace("trace test"); 
+```
+output:
+```console
+[info] "2013-03-26T18:30:14.570Z"  'info test'
+[warn] "2013-03-26T18:30:14.573Z"  'warn test'
+[error] "2013-03-26T18:30:14.574Z"  'error test'
+```
+(notice the trace() call was omitted because it's less than the info
+level.
+
+Using the same logger everywhere:
+The last logger you created is always available this way:
+```javascript
+var logger = require('log-driver').logger;
+```
+This way, if you use only one logger in your application (like most
+applications), you can just configure it once, and get it this way
+everywhere else.
+
+Don't like the logging format?  Just change it by passing a new
+formatting function like so:
+```javascript
+var logger = require('log-driver')({ 
+  format : function(){
+    // let's do pure JSON:
+    return JSON.stringify(arguments);
+  }
+});
+```
+
 
 ![Log Driver](https://raw.github.com/cainus/logdriver/master/waltz.jpg)
diff --git a/lib/index.js b/lib/index.js
index 8838581..7cd5459 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -36,16 +36,14 @@ LogDriver.prototype.format = function(){
   return out;
 };
 
-logger = function(options){
-  logger.logger = new LogDriver(options);
-  return logger.logger;
+var defaultLogger = null;
+
+factory = function(options){
+  defaultLogger = new LogDriver(options);
+  factory.logger = defaultLogger;
+  return defaultLogger;
 };
 
-module.exports = logger();
+factory();
 
-/*
-var l = logger({"level" : "info"}); //, format : function(){return JSON.stringify(arguments);}});
-l.error("something went horribly wrong");
-l.info("FYI", {asdf:true});
-l.debug("debug", {show:false});
-*/
+module.exports = factory;
diff --git a/package.json b/package.json
index 0f4f5ed..3bc7049 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
       "name": "log-driver",
       "description" : "log-driver is a simple logging framework for logging to stdout",
       "keywords" : ["logging", "logger", "log"],
-      "version": "1.0.0",
+      "version": "1.1.0",
       "bugs": {
        "url": "https://github.com/cainus/logdriver/issues"
       },
@@ -20,6 +20,7 @@
       },
       "devDependencies" : {
           "jscoverage" : "0.3.6",
+          "sinon-restore" : "1.0.0",
           "coveralls": "1.1.0",
           "mocha" : "1.8.1",
           "should" : "1.1.0"
diff --git a/test/index.js b/test/index.js
index 2293c62..100f150 100644
--- a/test/index.js
+++ b/test/index.js
@@ -1,13 +1,62 @@
 var should = require('should');
-var logger = require('../index');
+var logger = require('../lib/index');
+var defaultLogger = require('../lib/index').logger;
+var sinon = require('sinon-restore');
+var logged = "";
+
+/* NOTE: conole.log is mocked in these tests, so any uses of it will be
+ * appended to the variabled "logged".  This will include any calls you
+ * make to console.log() for the purpose of debugging in these tests.
+ */
+
 
 describe("logdriver", function(){
-  it ("creates a new instance of logger with default levels", function(){
-    should.exist(logger.error);
-    should.exist(logger.warn);
-    should.exist(logger.info);
-    should.exist(logger.debug);
-    should.exist(logger.trace);
-    logger.level.should.equal('trace');
+  beforeEach(function(){
+    logged = "";
+    sinon.stub(console, 'log', function(str){logged += str + "\n";});
+  });
+  afterEach(function(){
+    sinon.restoreAll();
+    logged = "";
+  });
+  it ("provides a default instance of logger with default levels", function(){
+    var mylogger = defaultLogger;
+    should.exist(mylogger.error);
+    should.exist(mylogger.warn);
+    should.exist(mylogger.info);
+    should.exist(mylogger.debug);
+    should.exist(mylogger.trace);
+    mylogger.level.should.equal('trace');
   });
+  it ("allows log level possibilities to be specified", function(){
+    var mylogger = logger({ levels : 
+                              ['superimportant', 
+                               'checkthisout', 
+                               'whocares']});
+    should.exist(mylogger.superimportant);
+  });
+  it ("allows log level to be specified, and doesn't log below that level", function(){
+    var mylogger = logger({ level : 'info'});
+    mylogger.info("info test"); 
+    mylogger.warn("warn test"); 
+    mylogger.error("error test"); 
+    mylogger.trace("trace test"); 
+    var lines = logged.split("\n");
+    lines[0].should.include('[info]');
+    lines[1].should.include('[warn]');
+    lines[2].should.include('[error]');
+    lines[0].should.include('info test');
+    lines[1].should.include('warn test');
+    lines[2].should.include('error test');
+  });
+  it ("allows you to override the default format", function(){
+    var mylogger = logger({
+      format : function(){
+        return JSON.stringify(arguments);
+      }
+    });
+    mylogger.error("here's an error");
+    JSON.parse(logged).should.eql({"0":"error","1":"here's an error"});
+  });
+
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-log-driver.git



More information about the Pkg-javascript-commits mailing list