Skip to content

using appropriate console function and displaying callee of logger #14

@gpicavet

Description

@gpicavet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions