Skip to content

Disable log_color programatically #65

Description

@xmedeko
  • ulog version: 2.0.0-beta.18

I cannot disable log_color programmatically for all loggers. I have tried

const ulog = require("ulog");
ulog.set("log_color", "off");
// or
ulog.set("log_color", false);

But if I create a logger afterwards const log = ulog("my"), it logs with colors. (Same with log_align).

Note: disabling or excluding whole colors mod would be also fine.

Activity

  1. Download commented on May 5, 2021

    @Download
    Owner

    The tutorial contains a section about enabling and disabling log colors.

    The command should be

    ulog.set('color', 'off')

    The reason for this is that if you change settings programmatically, you use the setting name (color in this case), but settings can have an alias for config and the alias for the color setting is log_color. The reason I chose to use aliases for config is that the two major configuration mechanisms, environment variables and localStorage, are shared/global between many apps/modules, so just assuming color would not collide with other apps/modules seemed naive. Most settings have a config alias starting with log_. There is also the level setting, which corresponds with the log config option.

    Also, if you want to get rid of colors all together, you could just remove the colors mod completely. It's there in the default config but you can just not include it. Check out ulog.js which contains the default set of mods. You can basically make your own version of this file and leave out colors and align mods.

  2. Download commented on May 5, 2021

    @Download
    Owner

    Also, when you set settings programmatically, you effectively override config. I recommend against it. Basically it's there for testing purposes. You can remove your override with ulog.set('color', undefined). I have doubted for a very long time whether to even include ulog.set at all in the API, but I decided that in some scenario's (mostly in unit tests) it would be beneficial to be able to override the config. But for 99.9 percent of normal logging code, you should not use it and instead let the guy installing / using your software decide on these settings through config and not hardcode them.

  3. xmedeko commented on May 6, 2021

    @xmedeko
    Author

    I am developing an app which is used by non-programmers. So, a user may rarely start the app with app --log=debug and that's all configuration they would like to set. So, all other log config, like log format, is hard coded.

  4. Download commented on May 6, 2021

    @Download
    Owner

    If you want to override the default config (as opposed to hardcode set the setting), I suggest making a small mod that only overrides the default value. The default value in the absence of config is determined by a property default on the setting declaration in the formats mod:

    module.exports = {
      use: [
        require('../channels'),
      ],
    
      settings: {
        format: {
          config: 'log_format',
          prop: {
            default: require('./default'),
          }
        },
      },

    https://github.com/Download/ulog/blob/master/mods/formats/index.js#L24

    (the value is stored in the file default.js, or default.browser.js, depending on platform)

    To override this default, you could make a mod like this:

    var ulog = require('ulog')
    var changeDefault = {
      use: require('ulog/mods/formats'),
      settings: {
        format: {
          prop: {
            default: 'My new default format!  :)',
          }
        }
      }
    }
    ulog.use(changeDefault)

    Do this before using ulog, so in your entry point somewhere. This gives you a new default format without breaking the option to use config to set it later.

  5. xmedeko commented on May 6, 2021

    @xmedeko
    Author

    Yes, that's what I have tried with color but failed, because I have missed the default property, because it's not explicitly defined in the color mod https://github.com/Download/ulog/blob/master/mods/colors/index.js#L19:

      settings: {
        colored: {
          config: 'log_color',
          prop: boolean(),
        },
      },

    It's working now, thanks.

  6. Download commented on May 6, 2021

    @Download
    Owner

    Glad you worked it out!

    because it's not explicitly defined in the color mod

    I guess you are referring to the boolean() hiding the fact that the default value for it is 'on'?

    module.exports = function(prop) {
      var result = {
        default: 'on',
        // ...

    https://github.com/Download/ulog/blob/master/mods/props/boolean.js#L5

    Could you maybe suggest a (documentation / code) change that would have helped you more easily understand?
    I am looking for feedback in this beta period to try to improve ulog before it finally hits 2.0 final.

    Would it have been better if the code would have looked like this:

    settings: {
        colored: {
          config: 'log_color',
          prop: boolean({
            default: 'on',
          }),
        },
      },

    I think it's also unnatural for booleans to default to 'on' (true) in stead of 'off' (false). This function boolean is basically the result of a refactor where I noticed it was a bit pesky when defining multiple boolean settings to have to repeat the fromStr and toStr functions everywhere. The choice for 'on' was maybe overly pragmatic; it was because that happened to be the intended default for all boolean settings, because I like colors and alignment to be enabled by default for ulog.

    Feedback?

  7. xmedeko commented on May 7, 2021

    @xmedeko
    Author

    Yes, it may help reading the code. I think general expectation is that the boolean() is default false, so it would help to see the default like you have proposed.

    Maybe also add similar case to https://github.com/Download/ulog#mods
    There is described the case "That it adds settings", so add a case "That changes default setting".

  8. Download commented on May 10, 2021

    @Download
    Owner

    Yes, good points. Let me keep this issue open for a bit longer so I can address these.
    Thank you for trying ulog and providing valuable feedback here!

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions