-
Notifications
You must be signed in to change notification settings - Fork 146
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
Feature request: Silent mode for all testing environments #1198
Comments
Hi @jamesmhaley thank you for opening this issue and for your support to the project! I understand that you'd like to mute/silence the logs in certain situations i.e. debugging and/or testing but to further the discussion I'd like to better understand a couple of things:
Also, could you share an example of the docs from Winston where this behavior is shown? I think it'd help me better frame this feature request since I'm not familiar with that logger. |
The My suggestion is to mimic what winston does, as it would limit/suppress logs coming from Powertools logger but wouldn't require any env vars, the mocking of anything and would still allow use of This is what the It's added as a param when you set up the logger, allow for a conditional to define it's state. Both solutions currently in place are not perfect in my view, for example:
I hope this makes sense, thanks again for all the hard work. |
Again, to clarify my use-case: My code uses Powertools logger to log out within my codebase. I log out a lot. I write some tests, when running these I don't want the noise of the Powertools logs, so I would like to suppress them. While writing my tests, to quickly check something like the data returned from a function call, I might add a Obviously I can attach a debugger in my IDE, but when writing a quick test, this can be cumbersome and (as we all know) sometimes slow things down a bit. I also don't like the idea of mocking |
Thank you for clarifying the use case, I now have a better understanding of what you're suggesting. To begin, I want to contextualize the current Logger behavior and its interaction with With the above in mind, Logger default behavior is to emit directly to In another feature request, customers have asked us to be able to disable logs when testing with As a stop-gap solution, if you want to emit logs out of band, you could use this solution that I have tested and that works without having to mock the console while also having full control over your logs and have all others silenced: Sample function that has logging in it ( import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger";
import middy from "@middy/core";
const logger = new Logger({});
export const handler = middy(async () => {
logger.info("Hello from the playground");
})
.use(injectLambdaContext(logger)); Test file using import { Console } from "console";
import { Context } from "aws-lambda";
import { handler } from "../lib/powertools-playground.function";
const console = new Console(process.stdout, process.stderr); // Create your own console object
describe("Sample Test", () => {
test("does stuff", async () => {
console.log('test'); // Use the newly created console object, this will always log regardless of Jest & Powertools flags/configs
await handler({ test: "ok " }, {} as unknown as Context);
});
}); Execute with Before deciding on whether to introduce a new flag, or a new non-standard log level (i.e. |
I like Andrea's workaround, but I also do like the idea of a "silent" option. I don't have a strong opinion, happy to hear other people's perspectives. |
Having an enabled constructor parameter would be great for my use case |
I also would like to be able to selectively disable all log emissions in scenarios including but wider than just in Jest tests. Use CaseMy current scenario is I'd like as much of our ecosystem to switch over to using this Logger and so possibly reference a global Then we could disable logging either at the root We could set the We could of course ask that module writer to not log an error when they receive one, and instead log a warning, and then set the Logger's Possible SolutionsTo that end, adding an That would also be fine for configuring the logger. It has a possible drawback though if a generic
|
Thank you for the comprehensive post, I really appreciate the added context and you flashing out the use case. I definitely see the value and acknowledge that we should seriously consider adding this feature. Before moving forward however I'd like to discuss a bit more the specifics. At the moment I'm leaning towards following Side notes
You can technically already do this, for example: const myDynamicLogType = resolveLogType(); // returns one of `info`, `error`, `warn`, `debug`
logger[myDynamicLogType]('I\'m a dynamic log entry');
Good point. If I recall correctly, this is the case because the log level can also come from the environment variables, which is always a string. With that said, I agree we could definitely be more strict about the accepted types and handle this internally. May I ask you to please open another feature request - referencing this message - so that we can address this? |
Writing here a recap of the discussion so far: Current situationCurrently Logger accepts a In some executions environments like CI, it's possible to suppress logging by patching one between the logger or the global console object (see here for more info). In Lambda however it's not possible to disable logs completely. This behavior is desirable for those customers who would like to have the ability to instrument their code with logs, but optionally have the chance to suppress them based on their requirements (see this comment for example). Expected behaviorLogger should accept a new const logger = new Logger({ logLevel: 'SILENT' });
// OR
process.env.LOG_LEVEL = 'SILENT';
const logger = new Logger(); Other notesThe following (but not limited to) files/methods will have be touched to implement the feature:
I'm also moving the issue to our backlog and marking it with |
@dreamorosi assign me, I read the discussion and I would like to help with this feature. |
Thank you for offering to work on this Sergei! As usual, happy to collaborate on the PR once is up 👍 |
Hey Andrea 👋 It seems like the log-level threshold scale is inverted, and following the current implementation, the Current scale is:
Because you proposed 0 for the silence mode and I think it is logical (zero = nothing), it makes sense reverting the scale from So new scale would be:
It would require one additional change here: private shouldPrint(logLevel: Uppercase<LogLevel>): boolean {
if (
- this.logLevelThresholds[logLevel] >=
+ this.logLevelThresholds[logLevel] <=
this.logLevelThresholds[this.getLogLevel()]
) {
return true;
}
return this.getLogsSampled();
} What do you think? |
Good observation, the proposed value wouldn't fit with the current scale. While I agree that intuitively inverting the scale sounds correct, I think we should keep the current scale and make the new value fit into it. My understanding (that I just got now after your comment) is that the scale conveys severity, so that the number that you set as With this in mind, I think the new level should probably be What do you think? |
Yes, that would be The scale depends on the convention. I followed this discussion where you mentioned log4j and assumed that we will follow that scale. But it's only my assumption. I did research on the history, I'm ok with both, let's make fewer changes, and as I mentioned before numbers are not important and are only in use internally. I can add a comment above the threshold levels to clarify the order of importance. |
Just to add to this discussion, (but I'm not sure what the best option is as I don't know the codebase well enough for that): The current scale does also seem inverted to me too, and is supported for example by how log4j2 defines their log levels as:
I'm not sure the impact on inverting the scale now whilst making the change is, but depending on that answer it may introduce less technical debt to do that at this moment. Or it may cause too much impact. I am hopeful that the use of the numeric value would be encapsulated enough to allow for such a refactoring if desired, but as I mentioned above, I don't know the code well enough to have an opinion on this. Side note: SILENT vs OFFBoth slf4j and log4j2 use Not a big deal either way, but thought being a bit more conventional is advantageous. I haven't checked other logging libraries outside the Java ecosystem however, so this may not be fully representative. |
A counter point to the It seems that a very popular JavaScript logging library loglevel (with over 9m weekly downloads) uses
I'm not advocating for any changes, I just wanted to add some data points before anything was implemented to validate the current intention or point to possible improvements. Update:
|
It's all good @danrivett, thank you for the inputs, I think they are valuable and further the discussion. Allow me to answer to both topics: Level scaleAs already touched upon in previous comments, the numeric values of the scale are not exposed to users. Users can only pass string values for When I initially brought the scale into the conversation (albeit with a wrong value) it was purely to aid a potential resolver. A note on Silent mode name/labelI don't have a strong preference either, however I'm more inclined towards
|
|
The new |
Use case
I'm a huge fan of the logger, thanks for all your hard work on it!! One side affect of it though is it can be super noisy when testing. It's useful to have the POWERTOOLS_DEV env variable, this is helpful. However, the problem with this is that there is no option of less noise, just silence.
I often debug with my IDE, but on occasion i'll add a
console.log
and find it useful to see that output. With my previous favourite logger (winston) you could silence within tests and that would stop the winston logger from outputting when explicitly silenced.Solution/User Experience
It would be great to be able to set the logger to silent when a certain condition is met, like in a test or dev environment. This could be using
NODE_ENV
or another conditional.A good solution would be to add an optional parameter of
silent
to the logger on invocation:This would allow for
console.log
to still make it's way through to the terminal, while reducing the noise from Logger.Alternative solutions
No response
Acknowledgment
The text was updated successfully, but these errors were encountered: