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

Can't console.log the object in browser #214

Closed
totty90 opened this issue Jan 15, 2015 · 4 comments
Closed

Can't console.log the object in browser #214

totty90 opened this issue Jan 15, 2015 · 4 comments

Comments

@totty90
Copy link

totty90 commented Jan 15, 2015

You are using rec.msg = format.apply(log, msgArgs); like this which will convert objects to strings:

  str += ' ' + inspect(x);

But in browser I would like to do console.log(object), maybe allow me to get arguments by adding this:

msg.args = msgArgs
@bitplanets
Copy link

a

Is a hack, but I can't find any other way.

// Use the log like this:

var log       = utils.createLog({
    name   : 'client/stores/ChangeStore',
    // level  : 'fatal'
    level  : 'trace'
});
log.trace('trace : ', {tes:1})
log.debug('debug : ', {tes:1})
log.info('info   : ', {tes:1})
log.warn('warn   : ', {tes:1})
log.error('error : ', {tes:1})
log.fatal('fatal : ', {tes:1})

// BunyanBrowserStream.js

function BunyanBrowserStream() {}
BunyanBrowserStream.prototype.write = function (rec) {
    var nameFromLevel = {
        10 : 'TRACE',
        20 : 'DEBUG',
        30 : 'INFO',
        40 : 'WARN',
        50 : 'ERROR',
        60 : 'FATAL'
    };
    var colorFromLevel = {
        10 : '#B3B3B3',
        20 : '#3C3C3C',
        30 : '#000000',
        40 : '#FFB800',
        50 : '#FF5700',
        60 : '#FF0000'
    };
    var fnFromLevel = {
        10 : 'log',
        20 : 'log',
        30 : 'log',
        40 : 'warn',
        50 : 'error',
        60 : 'error'
    };
    var args = [
        '%c[' + nameFromLevel[rec.level]+ ']: ',
        'color:' + colorFromLevel[rec.level]
    ];
    args = args.concat(rec.args);
    console[fnFromLevel[rec.level]].apply(console, args);
}
module.exports = BunyanBrowserStream;

// utils.js

var bunyan              = require('bunyan');
var bformat             = require('bunyan-format')
var formatOut           = bformat({outputMode: 'short'})
var BunyanBrowserStream = require('./utils/BunyanBrowserStream');
utils.createLog = function(options){
    var log       = bunyan.createLogger({
        name: options.name,
        streams : [
            {
                stream : new BunyanBrowserStream(),
                type   : 'raw',
                level  : options.level,
            }
        ]
    });
    var wrapMethods = {
        'trace' : 10,
        'debug' : 20,
        'info'  : 30,
        'warn'  : 40,
        'error' : 50,
        'fatal' : 60,
    }
    _.forEach(wrapMethods, function(logLevel, wrapMethod){
        log[wrapMethod + '_orig'] = log[wrapMethod];
        log[wrapMethod] = function(){
            var maxLevel = this.level();
            if(maxLevel > logLevel) return;
            this._emit({
                args  : Array.prototype.slice.call(arguments),
                level : logLevel,
            });
        }
    })
    return log;
}

@trentm
Copy link
Owner

trentm commented Jan 16, 2015

Note that you can get objects through raw if you pass then in an object as the first arg, e.g.:

log.info({win: window, tes: 1}, "here is my message: ", 42)

However, I do kind of like the idea of that resulting in this call in the browser:

console.log({win: window, tes: 1}, "here is my message: ", 42)

I'm just not sure how to cleanly do that right now.

@bitplanets
Copy link

You just need to pass the original msgArgs to the rec object before emitting. That will not require any hacks.

Note that you can get objects through raw if you pass then in an object as the first arg, e.g.:

How you get the object later?

@trentm
Copy link
Owner

trentm commented Feb 12, 2016

Sorry for the crazy delay here. I fell of maint and am trying to get back (see #335 for details on that).

I'd like to clarify what is meant by "object" here. Given this naming:

log.info({one: 1, two: {three: "four"}}, "five: %s", {six: "seven"}, "eight");
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         `-- fields                      `-- msgArgs

In my comments above I had been stating that the "fields" object is passed through to as an object to "raw" streams (e.g. the default stream used for a Bunyan Logger in the browser).

However, I realize that @totty90 is referring to possible objects in the "msgArgs" array, e.g. {six: "seven"} in the example above. If so, then I don't want to change Bunyan's design to allow that. The intention is that msgArgs are all meant for a stringified <record>.msg. If one wants raw objects in a Bunyan record, then one must put those in the leading option "fields" object. I hope that works for you.

Please re-open if I've misunderstood you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants