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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
|
@@ -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() + ' ' : ''; | ||
|
||
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" ) { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
}; | ||
|
||
//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; | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.