Skip to content

Consider adding custom formatter that shows log line origin... #11

Description

@chadacious

One thing that I found difficult when using this package is not always easily knowing where my log messages were coming from. Perhaps you could provide in your readme a custom formatter that would show the source file and line number. Something like this:

        format(...parms) {
            const args = Array.prototype.slice.call(parms, [1]); // change arguments to an array, but drop the first item (log level)
            let out = `[${parms[0]}] ${JSON.stringify(new Date())} `;
            args.forEach((arg) => { out += ` ${util.inspect(arg)}`; });
            if ((process.env.LOG_ORIGIN || '').toLowerCase() === 'true') {
                try {
                    const err = new Error();
                    const callerLine = err.stack.split('\n')[3];
                    const index = callerLine.indexOf('at ');
                    const origin = callerLine.slice(index + 2, callerLine.length);
                    return `${out}\n\t${origin}`;
                } catch (e) { return `${out}`; }
            }
            return `${out}`;
        },

And a sample of the output with an env variable of LOG_ORIGIN=true would be:

[info] "2019-03-28T16:12:31.931Z"  'Prerequisite table LocalUser exists.'
         exports.checkPrerequisiteTable (C:/Projects/..../src/models/index.js:14:9)
[trace] "2019-03-28T16:13:02.159Z"  'the request user is' 'chad@....com'
         ApolloServer.context (C:/Projects/..../src/index.js:77:17)

Activity

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

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