I am using Play Framework 2.2.x/Java.
I want to create a module to sperate some of the logics from my main application and also I want to use the application.conf inside the module for its configurations instead of using the main application's config file!
But using the following snippet in the module, only reads values from the main application config file:
Play.application().configuration().getString("myVar");
Is there any other way to get the values from the application.conf file inside my module?
Play uses the typesafe-config library for reading configuration. This is actually a Java library, even though Typesafe is a Scala company.
The documentation for typesafe-config says "The idea is that libraries and frameworks should ship with a reference.conf in their jar."
So your module's config should be stored in a file called reference.conf - the format is exactly the same, it's just the name that is different.
The problem occurs because there is a conflict between the two config files because they are named the same, so it probably goes by classpath order or something. Don't use two application.conf files - this problem has bitten me in the past!
Save your config into ie. /conf/my-module.conf of the main app and then include it at the end of application.conf like:
include "my-module.conf"
I have a hibernate standard java application (not a webapp). Logging already works by default. I would like to see the parameters passed in the sql queries. I have researched that in order to that I need to enable logging. It seems as if hibernate uses slf4j. I have downloaded that jar and its accompanying slf4j-log4j jar. I have added these jars to the classpath. I have also added a log4j.properties to the root of the eclipse project.
I can't seem to have the project recognize that it needs to use slf4j and its properties file.
Do I need to add a reference in my hibernate.cfg.xml?
Make sure you have the property hibernate.show_sql set to true.
format_sql set to true will make the SQL be formatted a bit more pretty.
You may also need to log on TRACE level.
For more details have a look here or here.
I had the following working organization
src/main/resources/log4j.xml
src/main/resources/hibernate.properties
I wanted to reorganize my webapp as follow:
src/main/resources/log/log4j.xml
src/main/resources/orm/hibernate.properties
The Logger.info("foobar") still logs well (after having set the log4jConfigLocation context parameter), and the app still has a working database connection.
The problem is that Hibernate doesn't log anything anymore, even if hibernate.show_sql is set to true. Any idea why? Should specify the new path to the log4j.xml file to Hibernate? If yes, how?
You could run your server with this VM argument:-
-Dlog4j.configuration=/path/to/log4j.xml
I usually tend to place the log4j.xml at the recommended default location unless there's a need to do otherwise... "convention over configuration" is important especially if other peers may be working on the same project in the future.
Log4j first looks for its configuration by looking at the system property "log4j.configuration". If that system property is not set, it looks for a log4j.properties or a log4j.xml file on the classpath.
So, if you really want the log4j.xml at src/main/resources/log/log4j.xml, you will have to set the log4j configuration system property. This is basically what limc does by supplying it as a vm argument.
Also like limc says, you should probably just keep the log4j.xml at the default location.
I have created a code using jackrabbit-standalone-2.4.2.jar and i am getting jackrabbit.log_IS_UNDEFINED.log file in the project directory. This log file is very huge(close to 1 GB). I want to disable it but i dont know how. Does anybody have any idea to deal with situation?
To remove this file you have to (as stated in the file name) to define the logger that you want to use.
Since jackrabbit is relying on slf4j, you should configure properly a logger for it.
For instance you can add to you dependencies the lf4j-log4j12 implementation, plus a log4j.properties in your classpath.
You can also configure any other slf4j implementation.
I have solved this issue by removing logback.xml and slf4j.jar from jackrabbit-standalone.jar and adding slf4j.jar and slf4j-lo4j.jar in the classpath of the project. Now jackrabbit.log_IS_UNDEFINED.log is gone and i am getting desired logs :)
I'm currently working on a project that uses log4j.
I'm running a testcase (junit) and would like to set the log level to trace so that I can see if all the values are correct. Classes that use logging in the project contain a line like the following:
private static final Log LOG = LogFactory.getLog(MatchTaskTest.class);
and use a like like this to do the actual debugging
LOG.trace("value");
I have never used log4j before, does anybody know how I can change the log level just for the testcase, preferably simply by defining a parameter in eclipse's run configuration dialog.
Using another configuration file
Perhaps you could point to another configuration file.
java -Dlog4j.configuration=config file yourApp
Where:
config, you file of configuration, e.g. log4j.properties or log4j.xml.
file, the log file, e.g. myApp.log
yourApp, you app, e.g. MyAppGUI
Or you can use a class
java -Dlog4j.configurationClass=config class yourApp
Where:
config, you file of configuration, e.g. log4j.properties or log4j.xml.
class, any customized initialization class, like LogManager, should implement
the org.apache.log4j.spi.Configurator
yourApp, you app, e.g. MyAppGUI
You can see more in Apache log4j 1.2 - Short introduction to log4j on Default Initialization Procedure section.
Modifying the level programmatically
Moreover, you can also use the methods that offers the Logger class, like public void setLevel(Level level), e.g.:
Logger.getRootLogger().setLevel(Level.TRACE);
Since you want only for testing purposes, you could use them. But it is recommended not to use in client code because they overwrite the default configuration parameters in hard coded. The best way is to use an external configuration file.
In your junit class put:
Logger.getRootLogger().setLevel(Level.TRACE);
somewhere before the execution of the tested method. It will set the threshold level of the root logger to TRACE.
If you're using Maven, you can have two log4j configuration files:
one in src/main/resources, containing your production logging config
one in src/test/resources, containing your test-time logging config
Maven will automatically use the latter at test time, and bundle the former into your artifact (JAR, WAR, etc) so that it's used in production. You don't have to mess around with command line switches or anything.
I don't think this is possible.
The config file is going to let you configure what log messages actually surface in the log, not what level each message is going be logged at. This makes sense - the config should not affect the level of the message.
The javadoc has a method for each log level and a generic log method, which takes in a priority, so I'm not sure there's even a default to be set.
You can set a config file explictly on the command line via -Dlog4j.configuration=<FILE_PATH>, so you could set up a specific config for that test case.
I have no idea why some of the above didn't work for me. (I don't want to write config file). following works for me
Logger log1 = Deencapsulation.getField(Some.class,"logger");
log1.setLevel(Level.DEBUG);
NB that the log4j2.properties file may include the line
filter.threshold.level = debug
You can waste an entire afternoon trying to figure out why your LOG.trace() statements aren't outputting anything!
I actually put it in the #Before method. But I believe it could (and should) be placed in the #BeforeClass.
If you set it in the class body you'll get compiler errors.