-
Notifications
You must be signed in to change notification settings - Fork 39
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
Rework LogFilter.level
#38
Comments
Hi, it is currently not intended to set the level of a filter manually, you should use the constructor parameter of the logger to set your initial level! However, if you have ideas for restructuring the relationship between Logger and its Filter, feel free to propose them! |
So how would I change the log level after the code has begun? Eg, I have a function that usually logs a lot, but I want to suppress it once, so I set the log level to
Perhaps the default class MyLogger {
static Level level = Level.info;
}
class MyFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) => event.level.index >= MyLogger.level.index;
} From what I can tell in the code, |
You should be able to change the level of your filter just fine after the logger has been created.
This only works because you statically referenced your single logger instance, however, this can't be applied to other filters, as already mentioned, filters don't know their loggers, and it should be possible to use multiple loggers without sharing the same level.
True, but you can save your filter reference and use that to modify it after the creation. |
LogFilter.level
in the Logger
constructorLogFilter.level
But due to this bug, I can't. I used to have a global variable Lines 88 to 105 in 84c1668
Deleting
Fully agree here, my use of |
The logger doesn't overwrite anything on your filter while logging, this is only set when creating the logger, hence, the constructor parameter. Are you sure your And as I said, I can't just remove the assignment, as this would currently break a lot of code. |
It's a final global variable, which also has the lazy behavior, unfortunately. I definitely agree that it would be bad to break current code. I was thinking removing that line wouldn't if you made another change Currently, |
https://dart.dev/language/variables#default-value
You are right, I didn't know that a global final field would be lazily initialized, bummer.
This would work for this filter, but how about all the custom filters that people are using in their code and are currently relying on the logger constructor to set their level to a non-null value, the same way Production and DevelopmentFilter are relying on it? This would still break those filters. I know you want an easy fix especially because it looks easy to fix, but in this case I can't easily change something without risking people's code breaking in a way that isn't obvious (for example, in opposite to a method signature change). Currently, there is an issue that plans to rework the whole relationships, and we can probably use it to implement a better solution for this. (#5) |
However, we could probably change the field to a getter and in this getter check for |
@Levi-Lesches Sorry for the long delay, I finally put my idea into code, can you please check if this works for you? dependency_overrides:
logger:
git:
url: https://github.com/SourceHorizon/logger |
Nice! import "package:logger/logger.dart";
final logger = Logger(filter: ProductionFilter(), printer: SimplePrinter());
void main() {
Logger.level = Level.warning;
logger.i("This should not show");
Logger.level = Level.info;
logger.i("This will show");
} Output:
|
Great, thanks! Additionally, I would like to keep this issue open for the greater rework that I am planning, as the current state is still not great. 👍 |
@Levi-Lesches 2.0.2 has been successfully published! |
I'd say level shuold be part of Logger(filter: LogFilter(level: Level.warning)); |
This is the cause of a pretty weird bug:
Took me a while to isolate the problem, but it is because
logger
is lazily evaluated:Turns out, when
logger
is accessed for the first time, its constructor setsfilter.level = Logger.level
, overriding any previous assignment. This should be removed so we can configure the log level of a global loggerThe text was updated successfully, but these errors were encountered: