Giter Site home page Giter Site logo

Comments (7)

khirakawa avatar khirakawa commented on July 3, 2024

+1 I need this functionality as well.

from js-logger.

jonnyreeves avatar jonnyreeves commented on July 3, 2024

Hey Guys, could you please provide a little more detail on what feature you are after? Would pulling the default logHandler out into a member property be sufficient, ie:

Logger.defaultHandler = function (messages, context) { ... };

from js-logger.

khirakawa avatar khirakawa commented on July 3, 2024

My use case is to customize the message that gets printed out. For example, I always want the project name to be prepended to each log statement.

[MyProject] This is log statement
[MyProject] [ModuleA] This is a log statement from a named logger

@jonnyreeves For my case, I think it would be better if the message handling part of exposed instead:

// Prepend the logger's name to the log message for easy identification.
if (context.name) {
    messages[0] = "[" + context.name + "] " + messages[0];
}

from js-logger.

jonnyreeves avatar jonnyreeves commented on July 3, 2024

So my current train of thought is that useDefaults could be made a little more flexible when it comes to how the log messages are actually formatted - this would probably solve 80% of use-cases. To enable this, I am thinking of creating the following API:

Logger.useDefaults = function(options) {
        options = merge(options, {
            // Messages under this log level will not be written.
            defaultLevel: Logger.DEBUG,

            // Specify the format of log messages written to the console.  This string should be
            // made up using the following tokens:
            //   {{name}} The name of the logger.
            //   {{message}} The log message(s).
            //   {{timestamp}} timestamp when the log message is emitted
            //   {{level}} logLevel of the message as a string (ie: INFO).
            logFormat: "[{{name}}] {{message}}",

            // Will be invoked whenever a timestamp is required by the logFormat.
            formatTimestamp: function () { return new Date.toString() },
        });

So in @khirakawa's example, he could use:

Logger.useDefaults({
    logFormat: "[MyProject] [{{name}}] {{messages}}"
});

I would expose the logFormatter on Logger.utils.formatter so other, more bespoke log handlers, can also leverage it.

I'll try and knock some code together over the next few days and put it up for review, please let me know your thoughts if you have an opinion :)

from js-logger.

khirakawa avatar khirakawa commented on July 3, 2024

@jonnyreeves yes, that would work perfectly for my use case. Thanks!

from js-logger.

lucaspin avatar lucaspin commented on July 3, 2024

@jonnyreeves I'm also using js-logger to log on the client and server and I'd like to know if you're going to include the addition from @khirakawa

from js-logger.

jonnyreeves avatar jonnyreeves commented on July 3, 2024

Fixed in v1.2.0; you can now supplied a formatter function to useDefaults and mutate the messages Array as you see fit.

from js-logger.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.