Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

## SUMMARY

Removed the `sys.print` because it is obsolete
A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log()

## USAGE

A logger has 5 different levels of logging in a specific order:

'fatal', 'error', 'warn', 'info', 'debug'
Each of these log levels has its own method on the logging instance. You can set the maximum log level on a logger at runtime.

Each of these log levels has its own method on the logging instance. You can set the maximum log level on a logger at runtime.

By default, a logger writes to STDOUT, but given a writeable file path, it will log directly to a file.

### Instantiation:

// node/common.js style
// node/common.js style
var logger = require('./logger').createLogger(); // logs to STDOUT
var logger = require('./logger').createLogger('development.log'); // logs to a file

Expand All @@ -43,7 +44,7 @@ You can completely customize the look of the log by overriding the `format()` me
};
logger.debug('message');
//=> 1276365362167; message

## COMMENTS/ISSUES:

F-f-fork it, baby.
Expand Down
36 changes: 25 additions & 11 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ OTHER DEALINGS IN THE SOFTWARE.
*/

var path = require('path'),
sys = require('sys'),
sys = require('util'),
fs = require('fs');

var makeArray = function(nonarray) {
return Array.prototype.slice.call(nonarray);
var makeArray = function(nonarray) {
return Array.prototype.slice.call(nonarray);
};

// Create a new instance of Logger, logging to the file at `log_file_path`
Expand All @@ -42,23 +42,37 @@ var Logger = function(log_file_path) {
// default write is STDOUT
this.write = sys.print;
this.log_level_index = 3;

// if a path is given, try to write to it
if (log_file_path) {
// Write to a file
log_file_path = path.normalize(log_file_path);
this.stream = fs.createWriteStream(log_file_path, {flags: 'a', encoding: 'utf8', mode: 0666});
this.stream.write("\n");
this.write = function(text) { this.stream.write(text); };
this.useFile( log_file_path );
} else {
// write log into stdout
this.stream = process.stdout;
}

this.stream.write("\n");
this.write = function(text) { this.stream.write(text); };

return this;
};

Logger.levels = ['fatal', 'error', 'warn', 'info', 'debug'];


// This method make Logger uses a file
Logger.prototype.useFile = function(log_file_path) {
log_file_path = path.normalize(log_file_path);
this.stream = fs.createWriteStream(log_file_path, {flags: 'a', encoding: 'utf8', mode: 0666});
this.stream.write("\n");
return this;
};

// The default log formatting function. The default format looks something like:
//
// error [Sat Jun 12 2010 01:12:05 GMT-0400 (EDT)] message
//
//
Logger.prototype.format = function(level, date, message) {
return [level, ' [', date, '] ', message].join('');
};
Expand All @@ -79,8 +93,8 @@ Logger.prototype.log = function() {
message = '';

// if you're just default logging
if (log_index === -1) {
log_index = this.log_level_index;
if (log_index === -1) {
log_index = this.log_level_index;
} else {
// the first arguement actually was the log level
args.shift();
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "name" : "logger"
, "version" : "0.0.1"
, "description" : "A simple logging library that combines the simple APIs of Ruby's logger.rb and browser-js console.log()"
, "author" : "Aaron Quint <aaron@quirkey.com>"
{ "name" : "simple-logger"
, "version" : "1.0.0"
, "description" : "This is a simple fork of https://github.com/quirkey/node-logger"
, "author" : "Fatshotty <fat@fatshotty.net>"
, "main" : "logger"
, "engines" : { "node" : ">=0.1.90" }
}