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
configurations in log4j2.xml not being applied after installing logcaptor #8
Comments
Hey, To help you out and to understand the root cause of the issue I need a bit more information. Did you added the dependency as a test scoped dependency? Could you share pom or other project configuration? |
Hi mate @mhammadkassem any news from your side, looking forward to see your project configuration |
Hello, I also have this issue with both slf4j and log4j2, and when I apply the exclusions in the maven config. I'll try to make a sample Github project for you to understand. And one more point, SLF4J say the following about librairies (maybe it can help) : |
Hello again. You can find a well known code of yours here : https://github.com/Edhilion/java-tutorials 😉 You will find two new "tutorials" :
How to see the issue ?You will see that each log4j2.xml contains a "with-config" or "with-config-without-logcaptor" in the appended pattern layout.
Happy bug hunting. 🙂 |
Amazing @Edhilion thank you for the detailed description and the examples of the issues on the forked repo. I have now a clear understanding what the issue is you are facing, this is helpful. I will ping you when I have any updates from my side. |
Hi @Edhilion and @mhammadkassem I have analysed the issue and came to a conclusion that sadly this option is not possible with LogCaptor. Let me first try to explain how I came to this conclusion... The log configuration xml file contains a list of appenders, which can be configured to have a different kind of formatting or other configuration such as log level. While LogCaptor uses a custom appender, to be very specific the ListAppender of Logback which is an in-memory list for collecting log events. So when LogCaptor is being used (during the tests) it will use a different appender than configured in your So would it be possible to use the ListAppender of Logback, which LogCapture is using, and also use the What will happen to my production logs when using LogCaptor? What is the alternative? You also mentioned the following:
LogCaptor forwards all type of logs (Log4j, Log4j2, Java Util Logging etc) to SLF4J and afterwords it uses Logback as SLF4J implementation to capture those logs. So to have a functional LogCaptor I need to ship it with a SLF4J binding or else it won't be able to capture the logs and I think it is pretty essential as the sole purpose of the library is to capture logs... But I agree with your statement. I have another library, see here: GitHub - SSLContext Kickstart which only depends on I hope this makes sense. If you have any other questions or concerns please drop it here. And if you think it is possible to use LogCaptor while also applying the Log4j2 configuration during the test phase please share it 😊 or feel free to contribute to the project if you can add this capability to the library. |
Hi @Hakky54. I'll try to test it on my side and, if I can, I'll be proud to contribute to your project. |
Hi again, If I want to make it work with both Logback and Log4j2, do you mind I propose two implementations of your LogCaptor, one for each framework, with a switch based on |
Never mind. After some deep analyzis in SLF4J code, it seems this is really not possible. When I say "it is not possible", I mean "I can write production code that will work, but I can't test it inside the very same project", so it is a no go for me. So I guess I'll make my own version based on log4j2 instead of logback. |
Ah thats sad to hear, but I can understand it. I was still hoping this was somehow possible and it would be nice to add it to this library. Having a logcaptor for two different logging api's in this case 1 for SLF4J and 1 for Log4J2 would be more powerful and more customisable, but I choose to redirect all non SLF4J based logs to SLF4J and just use Logback to capture it and for the basic assertions this is sufficient. (for the most developers i think...)
Sounds as a great idea, please let me know if you start on it. I would love to contribute! I will close this issue as it is not-fixable (for now...). But I will reopen it when I find a solution, or feel free to reopen when you or someone else has a solution. |
Hey @Edhilion and @mhammadkassem! I have some update regarding this issue/limitation. You can view it here: GitHub - ConsoleCaptor I would love to hear your opinion. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Adding the LogCaptor dependency caused the logging level to be DEBUG instead of INFO, even though it is configured to be INFO in log4j2.xml. This is a known issue (Hakky54/log-captor#8) because LogCaptor uses a different appender than the one configured in log4j2.xml. More details in this comment: Hakky54/log-captor#8 (comment). The solution is to remove the dependency and create a custom appender which is used to test the logs.
In my log4j2.xml file I configured log messages to be printed without any log info, only the message itself () amd it was working fine. Once I added logcaptor dependency in my POM file, the log messages appear as if there is a default configuration, so my configuration won't be applied.
The text was updated successfully, but these errors were encountered: