-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Why do I have to configure the log in every typescript file? #187
Comments
No idea I'm afraid! In general, you should only need to apply plugins once - the exported |
I think support for typescript needs to be improved. I think the issue I mentioned has something to do with https://stackoverflow.com/a/56984941/1830639 |
This is nothing to do with TypeScript. TypeScript works at compile time, and that question above mostly revolves around types. This is something that's happening in your runtime environment, where all the TypeScript has already been removed - everything is JavaScript at this stage. It might be due to some detail of the JavaScript that your TS is generating, although I'm not sure exactly how. You should be able to look at the JS output directly and see exactly what JavaScript is actually being generated & run. |
@JobaDiniz is your problem that:
I can’t reproduce (1), but (2) and (3) are probably caused by the issue of when you do the configuration. Here’s a simple reproduction (in JS, not TypeScript, but TypeScript should not make a difference here): https://gist.github.com/Mr0grog/0683970ae216da0dee76adba4be14fb7 The core issue here is that loglevel has to set up everything about log levels and formatting ahead of time, and you can’t change the level (which I would say there are two good ways to deal with this:
@pimterry Hi, I haven’t been here in years! I think there are changes that could be made here so that someone doesn’t have to do the above workarounds, if you think it’s worthwhile:
Side Q: are you still dedicated to ensuring |
Re: |
Yes, that was the issue! |
Nice work @Mr0grog! That makes a lot of sense, thanks for digging into this.
Yes, definitely. That's one of the main reasons this library exists - because that's the main footgun in just building a wrapper around console.log by yourself, and it can be very annoying when trying to quickly trace down browser logging. I understand that plugins break it, but I'm keen to provide a base where that works correctly out of the box. I'm broadly open to other improvements that don't break that constraint, but this library is quite small, focused & mature now (first release was more than 11 years ago!) and so maintainability & avoiding breakage for existing users is a higher priority than dramatic changes at this stage. Because of that, changing the default behaviour around all this is problematic. That said, adding a new method such as |
👍
Just for clarity, do you mean the default behaviour of keeping default functionality where Totally hear you on the former. For the latter, that makes sense — my feel is that this would be appropriate for a minor release, e.g. 1.9, but I can definitely imagine people un-knowingly relying on the current behavior and getting caught by surprise if previously instantiated child loggers got rebuilt, and you wanted to wait until 2.0 for that (seems like there are a lot of ideas there, but it ran out of steam in #119?). Anyway, I think I may have overcommitted myself on other projects for the rest of December, but I can try and take a cut at this by early January if you’d like:
|
I'm saying that automatically rebuilding child loggers is problematic. That is definitely a breaking change, since existing logging code would start behaving differently in potentially problematic ways. I'd strongly prefer to avoid doing breaking changes like that at this stage. Adding (We might want to bikeshed the name, but I think the concept is clear: apply the current level & plugins to this logger and all existing children) The trick is finding a way to provide a nice solution that provides a clean new API to avoid this problem, but with minimal impact to the rest of the existing code & users. This package is installed on npm 7.5 million times per week so the impact of breaking anything is absolutely huge, and this package really isn't my main focus nowadays so I'm quite keen to prioritise stability here. |
Oh, for sure. And I don't think that will be an issue. I was more trying to get at whether you felt automatically updating child loggers is more like fixing a longstanding bug (minor release, e.g. 1.9, since obviously issues like this come about because it’s how people expected it to work), or a disruptive change (major release). I think answer to that is now clear! (Too disruptive!) 👍 |
This implements a new `Logger.rebuild(includeChildren)` method that does the actual work of replacing logging methods on a logger and, optionally, on child loggers. The goal is to make adding plugins (or otherwise changing the method factory) easier by giving users a way to update any child loggers that have already been declared, solving the need for careful declaration ordering as described in pimterry#187. In the ideal case, plugin developers will call this method automatically when their plugin is attached to a logger so users don't have to worry about it at all.
Fixes pimterry#187.
Posted a first cut at solving this in #189. |
This implements a new `Logger.rebuild(includeChildren)` method that does the actual work of replacing logging methods on a logger and, optionally, on child loggers. The goal is to make adding plugins (or otherwise changing the method factory) easier by giving users a way to update any child loggers that have already been declared, solving the need for careful declaration ordering as described in pimterry#187. In the ideal case, plugin developers will call this method automatically when their plugin is attached to a logger so users don't have to worry about it at all.
Fixes pimterry#187.
This implements a new `Logger.rebuild(includeChildren)` method that does the actual work of replacing logging methods on a logger and, optionally, on child loggers. The goal is to make adding plugins (or otherwise changing the method factory) easier by giving users a way to update any child loggers that have already been declared, solving the need for careful declaration ordering as described in pimterry#187. In the ideal case, plugin developers will call this method automatically when their plugin is attached to a logger so users don't have to worry about it at all.
Fixes pimterry#187.
Circling back here for @JobaDiniz and anyone else reading this thread… In v1.9.0 (just released), there is a So you can now have code like: // file: module-a.js
import log from 'loglevel';
const logger = log.getLogger('ModuleA');
export function logFromA() {
logger.warn('Warn from A!');
logger.info('Info from A!');
} // file: module-b.js
import log from 'loglevel';
const logger = log.getLogger('ModuleB');
export function logFromB() {
logger.warn('Warn from B!');
logger.info('Info from B!');
} // file: index.js
import log from 'loglevel';
import logPrefix from 'loglevel-plugin-prefix';
import { logFromA } from './module-a.js';
import { logFromB } from './module-b.js';
// You can run the following config code anytime, even if the loggers in the
// other modules have already been set up.
logPrefix.reg(log);
logPrefix.apply(log, { template: '[%t] %l %n:' });
log.enableAll();
log.rebuild(); // ← This is new, it will make the following log calls work like you expect!
log.warn('Warn at root!');
log.info('Info at root!');
logFromA();
logFromB(); |
A bit of a noob question, but I have to do the following in every
.ts
file that I have. If one file does not dolog.enableAll()
no log is collected for that file.Why is that?
The text was updated successfully, but these errors were encountered: