[Pkg-javascript-commits] [node-log-driver] 19/49: added the ability to set log-level to false to turn off logging entirely.

Bastien Roucariès rouca at moszumanska.debian.org
Thu Feb 22 12:57:02 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 e6acd66b2276975656ba2d110e39a854751bc3bd
Author: cainus <gregg at caines.ca>
Date:   Sat May 11 07:08:17 2013 -0700

    added the ability to set log-level to false to turn off logging entirely.
---
 .npmignore    |  4 ++++
 .travis.yml   |  3 +++
 README.md     |  7 +++++++
 lib/index.js  | 23 +++++++++++++++++------
 package.json  |  2 +-
 test/index.js | 11 +++++++++++
 6 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..748f2ed
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+test
+waltz.jpg
+logo.png
+
diff --git a/.travis.yml b/.travis.yml
index baa0031..26da641 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,3 +1,6 @@
 language: node_js
 node_js:
   - 0.8
+  - 0.9
+  - "0.10"
+
diff --git a/README.md b/README.md
index ae8f843..15beeca 100644
--- a/README.md
+++ b/README.md
@@ -3,6 +3,7 @@
 [![Build
 Status](https://travis-ci.org/cainus/logdriver.png?branch=master)](https://travis-ci.org/cainus/logdriver)
 [![Coverage Status](https://coveralls.io/repos/cainus/logdriver/badge.png?branch=master)](https://coveralls.io/r/cainus/logdriver)
+[![NPM version](https://badge.fury.io/js/logdriver.png)](http://badge.fury.io/js/logdriver)
 
 Logdriver is a node.js logger that only logs to stdout.
 
@@ -61,6 +62,12 @@ output:
 (notice the trace() call was omitted because it's less than the info
 level.
 
+Turning off all log output (sometimes nice for automated tests to keep
+output clean):
+```javascript
+var logger = require('log-driver')({ level : false });
+```
+
 Using the same logger everywhere:
 The last logger you created is always available this way:
 ```javascript
diff --git a/lib/index.js b/lib/index.js
index 7cd5459..a78776f 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -7,14 +7,18 @@ LogDriver = function(options){
     this.format = options.format;
   }
   this.levels = options.levels || ['error', 'warn', 'info', 'debug', 'trace'];
-  this.level = options.level || this.levels[this.levels.length - 1];
-  if (this.levels.indexOf(this.level) === -1){
-    throw new Error("Log level '" +
-                    this.level +
-                    "' does not exist in level list '" + JSON.stringify() + "'");
+  if (options.level === false){
+    this.level = false;  // don't log anything
+  } else {
+    this.level = options.level || this.levels[this.levels.length - 1];
+    if (this.levels.indexOf(this.level) === -1){
+      throw new Error("Log level '" +
+                      this.level +
+                      "' does not exist in level list '" + JSON.stringify() + "'");
+    }
   }
   this.levels.forEach(function(level){
-    if (logger.levels.indexOf(level) <= logger.levels.indexOf(logger.level)){
+    if (logLevelShouldOutput(level, logger.level, logger.levels)){
       logger[level] = function(){
         var args = Array.prototype.slice.call(arguments);
         args.unshift(level);  // log level is added as the first parameter
@@ -26,6 +30,13 @@ LogDriver = function(options){
   });
 };
 
+var logLevelShouldOutput = function(logLevel, configuredLevel, levels){
+  if (configuredLevel === false){
+    return false;
+  }
+  return (levels.indexOf(logLevel) <= levels.indexOf(configuredLevel));
+};
+
 LogDriver.prototype.format = function(){
   var args = Array.prototype.slice.call(arguments, [1]); // change arguments to an array, but
                                                          // drop the first item (log level)
diff --git a/package.json b/package.json
index 8ec1104..9a45fae 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.2.0",
+      "version": "1.2.1",
       "bugs": {
        "url": "https://github.com/cainus/logdriver/issues"
       },
diff --git a/test/index.js b/test/index.js
index 043fd5a..776b426 100644
--- a/test/index.js
+++ b/test/index.js
@@ -43,6 +43,14 @@ describe("logdriver", function(){
     should.exist(mylogger.superimportant);
   });
   it ("allows log level to be specified, and doesn't log below that level", function(){
+    var mylogger = logger({ level : false});
+    mylogger.info("info test"); 
+    mylogger.warn("warn test"); 
+    mylogger.error("error test"); 
+    mylogger.trace("trace test"); 
+    logged.should.equal('');
+  });
+  it ("allows you to specify a log level of false to suppress all output", function(){
     var mylogger = logger({ level : 'info'});
     mylogger.info("info test"); 
     mylogger.warn("warn test"); 
@@ -55,7 +63,10 @@ describe("logdriver", function(){
     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(){

-- 
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