Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding Level Decorators and an option to Send Debug to StdOut #127

Closed
wants to merge 3 commits into from

2 participants

@kenperkins

This pull request has two key changes:

  • Adding Level Decorators
  • Adding a flag to send Debug level messages to StdOut

I'm not expecting the Pull Request as is to be accepted, but rather, to start the dialog about these proposals and to shape how they go from here.

LevelDecorators are an option on creating your transport that allow you to set the start and end characters for the decoration. For example:

transports.push(new winston.transports.Papertrail({
    levelDecorators: ['[', ']'],
    level: options.level,
    host: options.host,
    port: options.port,
    program: options.program
}));

Similarly, for the Console transport, I've proposed a debug level to stdout as it was biting us in the ass to have non-linear levels going to different pipes, i.e. debug, verbose, info, warn, error as your levels in order of severity.

transports.push(new winston.transports.Console({
    levelDecorators: ['[', ']'],
    level: options.level,
    colorize: true,
    timestamp: true,
    debugToStdOut: true
}));

Thoughts?

@kenperkins kenperkins commented on the diff
lib/winston/logger.js
((8 lines not shown))
: true;
-
+
+ //
+ // For now this is a hack as I'm still figuring out what I want
+ // the decorators to look like
+ //
+ if (options.levelDecorators && options.levelDecorators.length == 2) {

I'd love some suggestions on a better data structure for the decorators.

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

I agree that dealing with output formatting is a huge pain point in the winston console transport, but I don't know that this is the right answer.

I'm going to close this one, but this is definitely something that could use some thought, and I appreciate the PR.

@jfhbrook jfhbrook closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 17, 2012
  1. @kenperkins
Commits on Apr 11, 2012
  1. @kenperkins

    Merge from original

    kenperkins authored
  2. @kenperkins
This page is out of date. Refresh to see the latest.
View
3  .gitignore
@@ -3,4 +3,5 @@ test/fixtures/*.json
test/fixtures/logs/*.log
node_modules/
node_modules/*
-npm-debug.log
+npm-debug.log
+.idea/*
View
3  lib/winston/common.js
@@ -109,6 +109,7 @@ exports.log = function (options) {
var timestampFn = typeof options.timestamp === 'function' ? options.timestamp : exports.timestamp,
timestamp = options.timestamp ? timestampFn() : null,
meta = options.meta ? exports.clone(options.meta) : null,
+ decoratedLevel = options.levelDecorators[0] + options.level + options.levelDecorators[1],
output;
//
@@ -144,7 +145,7 @@ exports.log = function (options) {
}
output = timestamp ? timestamp + ' - ' : '';
- output += options.colorize ? config.colorize(options.level) : options.level;
+ output += options.colorize ? config.colorize(options.level, options.levelDecorators) : decoratedLevel;
output += (': ' + options.message);
if (meta) {
View
5 lib/winston/config.js
@@ -15,8 +15,9 @@ config.addColors = function (colors) {
mixin(allColors, colors);
};
-config.colorize = function (level) {
- return level[allColors[level]];
+config.colorize = function (level, levelDecorators) {
+ var formattedLevel = levelDecorators[0] + level + levelDecorators[1];
+ return formattedLevel[allColors[level]];
};
//
View
19 lib/winston/logger.js
@@ -46,10 +46,23 @@ var Logger = exports.Logger = function (options) {
this.level = options.level || 'info';
this.emitErrs = options.emitErrs || false;
this.stripColors = options.stripColors || false;
- this.exitOnError = typeof options.exitOnError !== 'undefined'
- ? options.exitOnError
+ this.exitOnError = typeof options.exitOnError !== 'undefined'
+ ? options.exitOnError
: true;
-
+
+ //
+ // For now this is a hack as I'm still figuring out what I want
+ // the decorators to look like
+ //
+ if (options.levelDecorators && options.levelDecorators.length == 2) {

I'd love some suggestions on a better data structure for the decorators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ this.levelDecorators = options.levelDecorators;
+ }
+ else {
+ this.levelDecorators = ['',''];
+ }
+
+ this.debugToStdErr = options.debugToStdOut || false;
+
//
// Setup other intelligent default settings.
//
View
7 lib/winston/transports/console.js
@@ -26,7 +26,9 @@ var Console = exports.Console = function (options) {
this.json = options.json || false;
this.colorize = options.colorize || false;
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;
-
+ this.levelDecorators = options.levelDecorators;
+ this.debugToStdOut = options.debugToStdOut || false;
+
if (this.json) {
this.stringify = options.stringify || function (obj) {
return JSON.stringify(obj, null, 2);
@@ -68,10 +70,11 @@ Console.prototype.log = function (level, msg, meta, callback) {
meta: meta,
stringify: this.stringify,
timestamp: this.timestamp,
+ levelDecorators: this.levelDecorators,
raw: this.raw
});
- if (level === 'error' || level === 'debug') {
+ if (level === 'error' || (level === 'debug' && !self.debugToStdOut)) {
console.error(output);
} else {
console.log(output);
Something went wrong with that request. Please try again.