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

Disable log_color programatically #65

Open
xmedeko opened this issue Apr 20, 2021 · 8 comments
Open

Disable log_color programatically #65

xmedeko opened this issue Apr 20, 2021 · 8 comments
Labels

Comments

@xmedeko
Copy link

xmedeko commented Apr 20, 2021

  • 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.

@Download
Copy link
Owner

Download commented May 5, 2021

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.

@Download
Copy link
Owner

Download commented May 5, 2021

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.

@xmedeko
Copy link
Author

xmedeko commented May 6, 2021

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.

@Download
Copy link
Owner

Download commented May 6, 2021

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.

@xmedeko
Copy link
Author

xmedeko commented May 6, 2021

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.

@xmedeko xmedeko closed this as completed May 6, 2021
@Download
Copy link
Owner

Download commented May 6, 2021

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?

@xmedeko
Copy link
Author

xmedeko commented May 7, 2021

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".

@Download Download reopened this May 10, 2021
@Download
Copy link
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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants