I am debugging some jUnit tests that use the Camel TestSupport interface. This gives me access to a log4j log but I can't seem to figure out how to get a reconfigured version of that. I want to be able to change the log level since a lot of my logs are written in a trace level instead of just info or debug.
What is the best way to reconfigure this log's console output?
Thanks
You need to include a log4j.xml or log4j.properties on your classpath that you're using the test.
If you're using maven, this is easy, just put it in src/test/resources.
Related
My application is going to have a configuration file that configures its settings.
I want this to be the only configuration file the application uses,
The application will be run from a jar file, which means that log4j will be started as soon as the application is run.
Is there any way to delay initialization of log4j so i can use information from configuration file to configure log4j and then start log4j?
As Andreas writes, you (or a library you call) obviously use Log4J. But without a short code example, we can't tell you what's going on (Hint: by removing code to get a short example, you might find the culprit yourself).
According to the Documentation it should even be possible (starting from release 2.4) to change the configuration programmatically after Log4J started.
I have the same problem as this: How to set the log level to DEBUG during Junit tests? - except I'm working with java.util.logging, not Log4j.
Specifically, I'm converting an existing (Maven-built) application from Log4j to java.util.logging, in order to comply with a customer requirement (not negotiable). Everything uses SLF4J, so code changes are minimal. There are over a thousand test classes, spread across a few modules; we currently have a log4j.properties file in the src/test/resources of those modules to customize the log output of the tests.
I think it's important for log output to be easily customizable for tests: if a developer breaks a test and wants to read the log output, I'd prefer they locally tweak the log config than start messing with the (carefully chosen) log levels in the code, leading to "everything logged at info" syndrome. Note that I want the test log config to apply to "the test+application code when executed under test conditions".
I don't want to add log settings to each and every Test class (as suggested here: How to set the Loglevel for a JUnit Test), because of the large number of tests. It seems like the wrong solution. I also don't want to go editing the JVM's logging.properties file, as that would affect everything. This app isn't the only thing I do. I'd also have to get everyone else to make the same changes.
The existing system works because Log4j picks up /log4j.properties from the classpath. Is there an equivalent mechanism for java.util.logging?
Current thinking: add a logging.properties file to replace the existing config, and try and hack the Maven config to set the java.util.logging.config.file param to point at it. Is there a neater way?
A project-wide src/test/resources/logging.properties can be set specifically for unit testing via the java.util.logging.config.file System property using a single line in your #BeforeClass method:
System.setProperty("java.util.logging.config.file", ClassLoader.getSystemResource("logging.properties").getPath());
Is there a neater way?
That is probably the most reliable method to set the logging configuration.
Is there an equivalent mechanism for java.util.logging?
Configuration options are listed in the java.util.logging.LogManager documentation. You can:
Set the java.util.logging.config.file property and use a properties file.
Set the java.util.logging.config.class property and create a class file with a no argument constructor to execute any code you desire to manually configure the loggers.
Use LogManager.readConfiguration(InputStream) and ClassLoader.getResourceAsStream(String) to load a new configuration. This logic can be combined with java.util.logging.config.class property too.
Is there a way I can detect where jboss is writing the console log file?
I am using java.util.logger and log4j.
Check to see if there's a system property called jboss.server.log.dir. You should be able to read this value with System.getProperty("jboss.server.log.dir").
If it's AS 7, you can add the logging module as your module's dependency and reach the logging service, which will have this info.
Also, you might get that info through the Management API.
https://docs.jboss.org/author/display/AS71/Management+API+reference
Unfortunately what you need is still empty:
https://docs.jboss.org/author/display/AS71/The+native+management+API
I'm creating a java open source utility package and I would like to know if it is ok to include logging (like log4j) into that package.
The dilemma is if I include log4j into my package, where will i output the log file, I wouldn't want the log file to be at a wrong place for the user, but i would still want to create logs for debugging.
As well there is the issue of the user wanting to integrate my logs into the project itself.
and how will he be able to do that.
What would you recommend is the best way of doing this?
Thanks.
I think the normal way to do it, is not include the Log4j Jar in the package and let the library user to decide which version of Log4j to use.
Also you don't have to worry about where to output the log file, since the user will have its own log4j.properties or log4j.xml configuration.
The library user can also decide which level of logging to use from your library or whether to suppress it. For instance with Amazon AWS libraries, I can tell that I only want warning messages or the output will be too verbose. In that case I add to my log4j.properties:
log4j.logger.com.amazonaws=WARN
Said that, I'd also remind you that a modern alternative to log4j is SL4J.
I have some jar files that will be distributed to clients that are using log4j for logging. My question is should I include a log4j.xml configuration in the jar file or have the client provide one if they want logging?
My feeling is to leave the log4j.xml configuration file out of the client jars, since the apache jar files all come with log4j logging, but sans log4j.xml.
Yes, leave it out. It's an utter nuisance when your log4j configuration file is ignored because one of the 60 third-party libraries of your app contains its own.
The good thing about log4j in your case is that your jar really shouldn't have to worry about it. The basic use case of log4j is:
Obtain a logger object for the current class
Call one of the methods on that logger, such as debug("some message");
If the jars you are shipping are to be used by a larger application, then ideally your code will only do the two steps listed above. In this way, your code will simply obtain logger objects from the already-configured log4j instance in the client's application. Your production code is then decoupled from having to know how to configure log4j.
Any logging you need to see for your development of the jars can be accomplished by configuring a log4j instance in unit test setUp() methods or something similar that won't get bundled with the production code going to the client.
I would put a default log4j configuration that you expect will be useful to your clients in the documentation. This way interested people can see what logging options you have (usually certain classes have more interesting log messages, from the user's perspective). I find it annoying when I have a third-party lib using log4j and it has no documentation, and there are log messages filling my screen and I have to try to figure out how to enable or suppress certain log messages.
If you are using log4j in your application then you include it in your project. If you are not, then why would you put it in there? What if client A wants log4j version 1.2 and client B wants log4j version 1.3.
Let them decide what they need for their projects and worry about what you need for yours.
I would add the configuration xml and load it up with instruction for the user showing different configuration and options. This will make it easier for either them or support to enable addition logging.