pimterry / loglevel

:ledger: Minimal lightweight logging for JavaScript, adding reliable log level methods to wrap any available console.log methods
MIT License
2.62k stars 157 forks source link

Add `Logger.rebuild()` Method #189

Closed Mr0grog closed 10 months ago

Mr0grog commented 11 months ago

This resolves #187 by adding a new Logger.rebuild(includeChildren) method, which takes care of using the methodFactory to update all the logging methods according to the current level. If the includeChildren argument is set to true and the method is called on the root logger, it will recursively rebuild all the child loggers (it is false by default, but in some future v2, it should probably be changed to true).

The includeChildren argument is mainly useful for two cases:

  1. When applying a plugin or modifying the logger’s methodFactory, it can be used to update previously existing child loggers, so you can divorce logger configuration code from logger declaration code. Ideally, plugin developers will call this from their plugin’s apply/register/whatever code. But a user could call it directly if a plugin developer does not.

  2. When changing the level on the root logger and you want that new level to apply to already existing child loggers (that you haven’t called setLevel() on and that didn’t have persisted levels). This essentially lets you treat a logger’s “default” level as if it means “whatever my parent/root logger’s current level is.”

So in #187, the example problem code could be updated slightly to make everything work right:

// In `a-random-submodule.js`
import log from 'loglevel'

const logger = log.getLogger('DefaultUiAutomationWorker');
// In `index.js`
import aRandomSubmodule from 'a-random-submodule.js';
import log from 'loglevel';
import logPrefix from 'loglevel-plugin-prefix';

function main() {
    logPrefix.reg(log);
    logPrefix.apply(log, { template: '[%t] %l %n:' });
    log.enableAll();
    log.rebuild(true);  // Adding this line is the only change.

    // start rest of program here
}

main();

Other Notes

Mr0grog commented 11 months ago

Whoops, forgot to update docs and type definitions. I added two commits to do that.

I’m also noticing that it doesn’t look like Travis is in use any longer, or at least there are no CI checks showing up on this PR. Should we add a GitHub Actions workflow since Travis doesn’t really have a free-for-open-source plan anymore?

pimterry commented 10 months ago

Thanks for this! Definitely looks like the right direction. I think there's some tweaks to make but we're on the right road and I agree it'd be nice to fix this up :+1:

Mr0grog commented 10 months ago

I’m also noticing that it doesn’t look like Travis is in use any longer, or at least there are no CI checks showing up on this PR. Should we add a GitHub Actions workflow since Travis doesn’t really have a free-for-open-source plan anymore?

@pimterry Just a quick re-up on the above question. Since #190 landed, I think this should be pretty easy to do (and I am happy to do the work; just let me know).

pimterry commented 10 months ago

I’m also noticing that it doesn’t look like Travis is in use any longer, or at least there are no CI checks showing up on this PR. Should we add a GitHub Actions workflow since Travis doesn’t really have a free-for-open-source plan anymore?

@pimterry Just a quick re-up on the above question. Since https://github.com/pimterry/loglevel/pull/190 landed, I think this should be pretty easy to do (and I am happy to do the work; just let me know).

Sorry I missed that - yes, Travis is indeed dead, I'd be extremely happy to move to GitHub Actions if you want to set that up.

Mr0grog commented 10 months ago

Alright, this should be ready for another review (and maybe to land). See my replies to the previous review comments for an update on the architecture here: https://github.com/pimterry/loglevel/pull/189#discussion_r1462567157

I added more comments, which pumps up the un-minified size a bit:

File Before After Change
loglevel.js 9.41 KB 11.04 KB +17.3%
loglevel.min.js 3.08 KB 3.26 KB +5.9%
pimterry commented 10 months ago

Thanks, I agree that 3 variable model looks cleaner, and I'm happy this pulls together the rebuild logic without changing the existing code too significantly.

Thanks for all your hard work to make this and the other PRs happen. It's really tidied up the project and should be a great fix to help others who might be affected by #187 and similar confusion. Great stuff :+1:. I'll do some last testing & checks locally and then publish a new release with these changes included soon.

Mr0grog commented 10 months ago

Awesome! 🙌

I’ll finish up with a commit that updates the contributing instructions as mentioned in https://github.com/pimterry/loglevel/issues/191#issuecomment-1894653099.