Repository navigation
Disable log_color programatically #65
Description
Activity
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 (
colorin this case), but settings can have an alias for config and the alias for thecolorsetting islog_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 assumingcolorwould not collide with other apps/modules seemed naive. Most settings have a config alias starting withlog_. There is also thelevelsetting, which corresponds with thelogconfig option.Also, if you want to get rid of colors all together, you could just remove the
colorsmod 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 outcolorsandalignmods.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 includeulog.setat 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.I am developing an app which is used by non-programmers. So, a user may rarely start the app with
app --log=debugand that's all configuration they would like to set. So, all other log config, like log format, is hard coded.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
defaulton 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.
Yes, that's what I have tried with color but failed, because I have missed the
defaultproperty, 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.
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 functionbooleanis basically the result of a refactor where I noticed it was a bit pesky when defining multiple boolean settings to have to repeat thefromStrandtoStrfunctions 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?
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".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!Reacted by Ondrej Medek
ulogversion: 2.0.0-beta.18I cannot disable
log_colorprogrammatically for all loggers. I have triedBut if I create a logger afterwards
const log = ulog("my"), it logs with colors. (Same withlog_align).Note: disabling or excluding whole
colorsmod would be also fine.