Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of working module

  • Loading branch information...
commit 3f337aa54015ec6fb3c01e3156b293b6aa3d6bbd 0 parents
@tristanls authored
Showing with 168 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +55 −0 README.md
  3. +71 −0 index.js
  4. +20 −0 package.json
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Tristan Slominski <tristan.slominski@gmail.com>
+
+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.
55 README.md
@@ -0,0 +1,55 @@
+stdjson
+====
+
+`stdjson` is a minimal `nodejs` module to log JSON to stdout and stderr.
+
+## Installation
+
+ npm install stdjson
+
+## Usage
+
+```javascript
+var log = require( 'stdjson' );
+
+// initialize, logLevel is the only option
+log = log( { logLevel : "info" } ); // default "debug"
+
+log.debug( "this will be ignored" );
+// no output
+
+log.info( "just message" );
+// stdout:
+// {"message":"just message","level":"info","timestamp":"2012-10-13T21:02:42.113Z"}
+
+log.info( "message", "data" );
+// stdout:
+// {"message":"message","data":"data","level":"info","timestamp":"2012-10-13T20:32:08.625Z"}
+
+log.info( "message", { my : "data object" } );
+// stdout:
+// {"message":"message","data":{"my":"data object"},"level":"info","timestamp":"2012-10-13T21:03:48.764Z"}
+
+log.warn( { just : "data" } );
+// stdout:
+// {"data":{"just":"data"},"level":"warn","timestamp":"2012-10-13T21:01:01.817Z"}
+
+log.error( new Error( "oh no" ) );
+// stderr (includes stack trace):
+// {"message":"Error: oh no","data":"Error: oh no\n at repl:1:11\n at REPLServer.self.eval (repl.js:111:21)\n at rli.on.self.bufferedCmd (repl.js:260:20)\n at REPLServer.self.eval (repl.js:118:5)\n at Interface.<anonymous> (repl.js:250:12)\n at Interface.EventEmitter.emit (events.js:93:17)\n at Interface._onLine (readline.js:199:10)\n at Interface._line (readline.js:517:8)\n at Interface._ttyWrite (readline.js:735:14)\n at ReadStream.onkeypress (readline.js:98:10)","level":"error","timestamp":"2012-10-13T20:58:18.275Z"}
+
+// if you are really curious
+
+log.info( "message", {} );
+// stdout:
+// {"message":"message","data":{},"level":"info","timestamp":"2012-10-13T21:04:20.688Z"}
+
+log.info( "message", null );
+// stdout:
+// {"message":"message","data":null,"level":"info","timestamp":"2012-10-13T21:04:30.806Z"}
+
+log.info( "message", undefined );
+// stdout:
+// {"message":"message","level":"info","timestamp":"2012-10-13T21:04:41.371Z"}
+
+```
71 index.js
@@ -0,0 +1,71 @@
+/*
+ * index.js
+ *
+ * (C) 2012 Tristan Slominski
+ */
+
+var log = function log ( options ) {
+
+ options = options || {};
+
+ var logLevel = options.logLevel || "debug";
+
+ var logLevels = {
+ "debug" : 0,
+ "info" : 1,
+ "warn" : 2,
+ "error" : 3
+ };
+
+ var _do = function _do ( stream, level, message, data ) {
+
+ // don't log if wrong level
+ if ( logLevels[ level ] < logLevels[ logLevel ] ) {
+ return;
+ }
+
+ if ( typeof( message ) == 'string' ) {
+ data = { message : message, data : data };
+ } else if ( message instanceof Error ) {
+ data = { message : message.toString(), data : message.stack };
+ } else if ( typeof( message ) == 'object' && Object.keys( message ).length == 0 ) {
+ data = { message : message.toString(), data : message.stack };
+ } else {
+ data = { data : message };
+ }
+
+ data.level = level;
+ data.timestamp = data.timestamp || ( new Date() ).toISOString();
+
+ stream.write( JSON.stringify( data ) + '\n' );
+
+ }; // _do
+
+ var debug = function debug ( message, data ) {
+ _do( process.stdout, "debug", message, data );
+ };
+
+ var info = function info ( message, data ) {
+ _do( process.stdout, "info", message, data );
+ };
+
+ var warn = function warn ( message, data ) {
+ _do( process.stdout, "warn", message, data );
+ };
+
+ var error = function error ( message, data ) {
+ _do( process.stderr, "error", message, data );
+ };
+
+ return {
+ debug : debug,
+ error : error,
+ info : info,
+ level : logLevel,
+ log : info,
+ warn : warn
+ };
+
+}; // log
+
+module.exports = log;
20 package.json
@@ -0,0 +1,20 @@
+{
+ "name": "stdjson",
+ "version": "1.0.0",
+ "description": "Module for minimally logging JSON to stdout and stderr",
+ "main": "index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/tristanls/stdjson.git"
+ },
+ "keywords": [
+ "log",
+ "logger",
+ "json",
+ "stdout",
+ "stderr",
+ "minimal"
+ ],
+ "author": "Tristan Slominski <tristan.slominski@gmail.com>",
+ "license": "MIT"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.