[Pkg-javascript-commits] [node-log-driver] 01/15: Add upstream

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:49:10 UTC 2017


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 3a631e9b033112a06e73f46796739ad5d43c37e2
Author: Bastien ROUCARIÈS <roucaries.bastien at gmail.com>
Date:   Thu Aug 24 21:37:21 2017 +0200

    Add upstream
---
 .gitignore    |   5 +++
 .npmignore    |   4 +++
 .travis.yml   |   5 +++
 LICENSE       |   6 ++++
 Makefile      |  20 ++++++++++++
 README.md     |  99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 index.js      |  60 +++++++++++++++++++++++++++++++++++
 logo.png      | Bin 0 -> 12954 bytes
 package.json  |  44 ++++++++++++++++++++++++++
 test/index.js |  79 ++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 322 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..63e30ca
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+node_modules
+npm-debug.log
+lib-cov
+coverage.html
+coverage
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..349612b
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+test
+waltz.jpg
+logo.png
+coverage
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..26313c0
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - "0.10"
+  - "0.11"
+
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..86b41a4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,6 @@
+Copyright (c) 2014, Gregg Caines, gregg at caines.ca
+
+Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..32264b1
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,20 @@
+REPORTER = spec
+test:
+	@NODE_ENV=test ./node_modules/.bin/mocha -b --reporter $(REPORTER) --check-leaks
+
+lint:
+	./node_modules/.bin/jshint ./test ./index.js
+
+test-cov:
+	$(MAKE) lint
+	@NODE_ENV=test ./node_modules/.bin/istanbul cover \
+	./node_modules/mocha/bin/_mocha -- -R spec --check-leaks
+
+test-codecov:
+	echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
+	$(MAKE) test
+	@NODE_ENV=test ./node_modules/.bin/istanbul cover \
+	./node_modules/mocha/bin/_mocha --check-leaks --report lcovonly -- -R spec && \
+	cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js
+
+.PHONY: test
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e9e9743
--- /dev/null
+++ b/README.md
@@ -0,0 +1,99 @@
+![Log Driver][logdriver-logo]
+=========
+[![Build Status][travis-image]][travis-url] [![NPM Version][npm-image]][npm-url] [![codecov.io](https://codecov.io/github/cainus/logdriver/coverage.svg?branch=master)](https://codecov.io/github/cainus/logdriver?branch=master)
+
+Logdriver is a node.js logger that only logs to stdout.
+
+####You're going to want to log the output of stdout and stderr anyway, so you might as well put all your logging through stdout.  Logging libraries that don't write to stdout or stderr are missing absolutely critical output like the stack trace if/when your app dies.  
+
+##There are some other nice advantages:
+* When working on your app locally, logs just show up in stdout just like if you'd used console.log().  That's a heck of a lot simpler than tailing a log file.
+* Logging transports can be externalized from your app entirely, and completely decoupled.  This means if you want to log to irc, you write an irc client script that reads from stdin, and you just pipe your app's output to that script.
+
+```console
+node yourapp.js 2>&1 | node ircloggerbot.js 
+```
+* You can still easily log to a file on a production server by piping your stdout and stderr to a file like so when you initialize your app:
+
+```console
+node yourapp.js 2>&1 >> somefile.log 
+```
+
+NB: If you're logging to a file, [Logrotate](http://linuxcommand.org/man_pages/logrotate8.html) is probably going to be your best friend.
+* You can still easily log to syslog by piping your stdout and stderr to the 'logger' command like so:
+
+```console
+node yourapp.js 2>&1 | logger
+```
+
+##Usage:
+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.
+
+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
+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)
+
+[logdriver-logo]: https://raw.github.com/cainus/logdriver/master/logo.png
+
+[travis-image]: https://travis-ci.org/cainus/logdriver.png?branch=master
+[travis-url]: https://travis-ci.org/cainus/logdriver
+
+[coveralls-image]: https://coveralls.io/repos/cainus/logdriver/badge.png?branch=master
+[coveralls-url]: https://coveralls.io/repos/cainus/logdriver
+
+[npm-image]: https://badge.fury.io/js/log-driver.png
+[npm-url]: https://badge.fury.io/js/log-driver
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..769e891
--- /dev/null
+++ b/index.js
@@ -0,0 +1,60 @@
+var util = require('util');
+
+var LogDriver = function(options){
+  options = options || {};
+  var logger = this;
+  if (options.format){
+    this.format = options.format;
+  }
+  this.levels = options.levels || ['error', 'warn', 'info', 'debug', 'trace'];
+  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 (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
+        console.log(logger.format.apply(logger, args));
+      };
+    } else {
+      logger[level] = function(){/* no-op, because this log level is ignored */};
+    }
+  });
+};
+
+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)
+  var out = "[" + arguments[0] + "] " + JSON.stringify(new Date()) + " ";
+  args.forEach(function(arg){
+    out += " " + util.inspect(arg);
+  });
+  return out;
+};
+
+var defaultLogger = null;
+
+var factory = function(options){
+  defaultLogger = new LogDriver(options);
+  factory.logger = defaultLogger;
+  return defaultLogger;
+};
+
+factory();
+
+module.exports = factory;
diff --git a/logo.png b/logo.png
new file mode 100644
index 0000000..ca24658
Binary files /dev/null and b/logo.png differ
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..905dedf
--- /dev/null
+++ b/package.json
@@ -0,0 +1,44 @@
+{
+  "name": "log-driver",
+  "description": "log-driver is a simple logging framework for logging to stdout",
+  "keywords": [
+    "logging",
+    "logger",
+    "log"
+  ],
+  "version": "1.2.6",
+  "bugs": {
+    "url": "https://github.com/cainus/logdriver/issues"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/cainus/logdriver.git"
+  },
+  "scripts": {
+    "test": "make test-codecov"
+  },
+  "maintainers": [
+    "Gregg Caines <gregg at caines.ca> (http://caines.ca)"
+  ],
+  "dependencies": {
+    "codecov.io": "0.0.1"
+  },
+  "devDependencies": {
+    "jshint": "2.4.4",
+    "istanbul": "0.2.6",
+    "coveralls": "2.10.0",
+    "sinon-restore": "1.0.1",
+    "mocha": "1.20.1",
+    "should": "1.1.0"
+  },
+  "engines": {
+    "node": ">=0.8.6"
+  },
+  "homepage": "https://github.com/cainus/logdriver",
+  "main": "index.js",
+  "directories": {
+    "test": "test"
+  },
+  "author": "Gregg Caines",
+  "license": "ISC"
+}
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..776b426
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,79 @@
+var should = require('should');
+var logger = require('../index');
+var defaultLogger = require('../index').logger;
+var sinon = require('sinon-restore');
+var logged = "";
+var oldConsoleLog = console.log;
+
+/* NOTE: console.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.
+ *
+ * use oldConsoleLog() instead if you need to console.log() for
+ * debugging;
+ */
+
+
+describe("logdriver", function(){
+  beforeEach(function(){
+    logged = "";
+    sinon.stub(console, 'log', function(str){
+      logged += str + "\n";
+    });
+  });
+  afterEach(function(){
+    sinon.restoreAll();
+    console.log = oldConsoleLog;
+    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 : 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"); 
+    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