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

Export Logger and LoggingEvent for easy customization #222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dwelch2344
Copy link

TLDR: Export Logger and LoggingEvent so customization (like line numbers) can easily be implemented.

It's really useful to be able to see the originating file and line number when developing. I know this has performance impacts, but if the client code had access to the Logger and LoggingEvent objects, we could easily make that call on our own.

A simple example (abstracted from #73) shows how this could work in most smaller apps.

var log4js = require('log4js')    
    , logger = log4js.getLogger()    
    , sh = require("shelljs")
    , cwd = sh.pwd()
;

log4js.Logger.prototype.log = function() {

    var stack = new Error().stack.split('\n');
    var location = stack[3].match(/:([0-9]+):([0-9]+)\)$/);
    var file = stack[3].match(/.*\(([^:]+).*/)[1].replace(cwd + '/', "");
    var category = this.category;
    if (location) {
        category += ':' + file + ':' + location[1] + ':' + location[2];
    }

    var args = Array.prototype.slice.call(arguments)
        , logLevel = args.shift()
        , loggingEvent = new log4js.LoggingEvent(category, logLevel, args, this);
    this.emit("log", loggingEvent);
};

logger.info("Hello World!");

results in

[2014-07-26 22:41:30.227] [INFO] [default]:test.js:23:8 - Hello World!

It's a pretty trivial change (although it does expose internals) so I haven't written any tests, but I think any exposure to these two objects would suffice.

Thoughts?

Export these objects so their prototype can easily be overridden
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant