Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Option to prefix date to messages #3

Merged
merged 1 commit into from

2 participants

@brunomorency

Hi Tristan -- Here's a commit that adds an option to prefix date+time to all log messages. It's implemented through a logly.options() method that integrates both this 'date' option as well as the 'color' option added recently. To keep it backwards compatible, the logly.color() calls are still there but are just the same as logly.options({color: true}).

@tristanls tristanls commented on the diff
((6 lines not shown))
if(typeof colour === 'undefined') colour = noColour;
+
+ // add date as message prefix
+ var datePrefix = (options.datePrefix) ? (new Date()).toString() + ' ' : '';
@tristanls Owner

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?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tristanls tristanls commented on the diff
@@ -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);
@tristanls Owner

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

'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'.

@tristanls Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tristanls
Owner

Thank you @brunomorency !

I'm happy to add it. I've added some inline comments for your consideration.

@brunomorency

Great, do you want me to make those changes or do you prefer putting it to your own tastes?

@tristanls
Owner

I'll merge your stuff in and add what we agreed to. (I have to add to documentation, so might as well do it together).

@tristanls tristanls merged commit 4e8e587 into tristanls:master
@tristanls
Owner

@brunomorency all updated, committed, and published. If you have a chance, please take a look if I messed something up. Cheers!

@brunomorency

Changes made post-merge seem good to me, thanks for quickly accepting that pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 23, 2012
  1. Added option to prefix date to log messages

    Bruno Morency authored
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 8 deletions.
  1. +18 −8 logly.js
View
26 logly.js
@@ -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() + ' ' : '';
@tristanls Owner

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?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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);
@tristanls Owner

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

'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'.

@tristanls Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
};
-//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;
Something went wrong with that request. Please try again.