Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to prefix date to messages #3

Merged
merged 1 commit into from Aug 23, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions logly.js
Expand Up @@ -5,7 +5,10 @@ exports.version =

var name = {};
var mode = {};
var colourText = false; //true if the output should be coloured
var options = {
'colourText': false, // true if the output should be coloured
'datePrefix': false // true if log messages should be prefixed with date
};
var colours = {}; //set up below colour functions


Expand All @@ -18,20 +21,23 @@ var logger = function( input, methodMode ) {
if ( typeof( input ) === "string" ) {
//colour output
var colour = noColour;
if(colourText) colour = ( colours[methodMode] );
if(options.colourText) colour = ( colours[methodMode] );
if(typeof colour === 'undefined') colour = noColour;

// add date as message prefix
var datePrefix = (options.datePrefix) ? (new Date()).toString() + ' ' : '';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about ISO8601via toISOString() ?

perhaps we can pass in a date format instead of just true? if the format is "ISO" or "ISO8601" it does it that way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of letting users just put a boolean value to get a default or a string to get their desired format.


switch(methodMode) {
case 'error':
case 'warn':
console.error( colour( name[ process.pid ] + '[' + methodMode + ']: ' + input ) );
console.error( datePrefix + colour( name[ process.pid ] + '[' + methodMode + ']: ' + input ) );
break;
case 'debug':
case 'verbose':
console.log( colour( name[ process.pid ] + '[' + methodMode + ']: ' + input ) );
console.log( datePrefix + colour( name[ process.pid ] + '[' + methodMode + ']: ' + input ) );
break;
default:
console.log( colour( name[ process.pid ] + ': ' + input ) );
console.log( datePrefix + colour( name[ process.pid ] + ': ' + input ) );
break;
}
} else if ( typeof( input ) === "function" ) {
Expand Down Expand Up @@ -134,11 +140,15 @@ exports.name = function( applicationName ) {
name[ process.pid ] = applicationName;
};

exports.colour = function( bColour ) {
colourText = bColour === true;
exports.options = function( opts ) {
options.colourText = (('color' in opts) ? (opts.color === true) : (('colour' in opts) ? (opts.colour === true) : options.colourText));
options.datePrefix = (('date' in opts) ? (opts.date == true) : options.datePrefix);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is opts.date on purpose? or is it supposed to be opts.datePrefix? or both? (also, how about ISO8601 format?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'date' is how the option is exposed to code setting that option: logly.options({date: true}) but within the module I used a longer datePrefix. If that feels dirty, you can call it 'date' internally as well instead of 'datePrefix'.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking about it more, it makes sense, we already have a convention to support two different color spellings, so a datePrefix being different seems consistent to me

};

//I even included this spelling because I'm nice
// this is for compatibility with initial way to set output coloring
exports.colour = function( bColour ) {
options.colourText = bColour === true;
};
exports.color = exports.colour;

exports.debug = debug;
Expand Down