log4js icon indicating copy to clipboard operation
log4js copied to clipboard

using appropriate console function and displaying callee of logger

Open gpicavet opened this issue 10 years ago • 3 comments

Hello, thank you for this cool library !

Just 2 little things : -It would be nice to use the appropriate console function(error, info, log) according to the log level. -Displaying the line where the logger has been called (using the stack)

As an exemple, here is an extension i did myself (sorry i'm not a javascript expert!) :

Log4JS.SafariJSConsoleAppender.prototype.doAppend= function (loggingEvent) {
        var style;
        var func = null;
        if (loggingEvent.level.toString().search(/ERROR/) != -1) {
            style = 'color:red';
            func = "error";
        } else if (loggingEvent.level.toString().search(/FATAL/) != -1) {
            style = 'color:magenta';
            func = "error";
        } else if (loggingEvent.level.toString().search(/WARN/) != -1) {
            style = 'color:yellow';
            func = "warn";
        } else if (loggingEvent.level.toString().search(/DEBUG/) != -1) {
            style = 'color:cyan';
            func = "log";
        } else if (loggingEvent.level.toString().search(/INFO/) != -1) {
            style = 'color:green';
            func = "info";
        } else if (loggingEvent.level.toString().search(/TRACE/) != -1) {
            style = 'color:blue';
            func = "log";
        } else {
            style = 'color:grey';
            func = "log";
        }

        if(!console[func]) {
            func = "log";
        }

        var realLine = "";
        var stack = new Error().stack;
        var stackStr = stack.split("\n");
        for(var i in stackStr) {
            var call = stackStr[i];
            if(call.indexOf("Log4js")==-1 && call.indexOf("Logger")==-1) {
                realLine = call;
                break;
            }
        }

        console[func].apply(console, ["%c "+this.layout.format(loggingEvent) + " "+realLine, style]);
    };

Kind regards

gpicavet avatar Jun 17 '15 17:06 gpicavet

I would also like for there to be default context based colours for log entries that appear in web console(not to be confused with the browser console or shell) but that should be its own(Issue) appender or plugin for an appender. As that would output _raw_ in a shell(terminal).

I know at least in FF you can log directly to the web console without using console.* methods.

I thought I'd mention that the log methods should be preset(binded?) not set inside doAppend.

js-logger has the right idea on this. See here for an example.

ghost avatar Jun 18 '15 20:06 ghost

Bump. This is clearly a bug. Your library doesn't use console.* methods! If you mean to compete with lets say log4javascript or even js-logger then this issue should definitely be fixed ASAP. :+1:

ghost avatar Jun 21 '15 10:06 ghost

Many thanks for the improvements. I will have a look on them, but also any pull requests are welcome

stritti avatar Jun 30 '15 21:06 stritti