I am using log4j for loggin purpose in my application. Since now to configure the logging i was using the following code :
LogManager.resetConfiguration();
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties");
Properties props= new Properties();
props.load(stream);
PropertyConfigurator.configure(props);
But the problem with this was , that whenever i wanted to change the logging level during the process, i had to restart the server. So i changed the code to :-
LogManager.resetConfiguration();
PropertyConfigurator.configureAndWatch(("log4j.properties", 900000L);
this code ideally should help to re-load the log4j.properties file after the time specified, which i have mentioned as 15 minutes. But still the code is not working
Am i missing somthing during the code?
Regards.
configureAndWatch() watches files. Not resources in the classpath.
I tried the solution and works fine! The point is that your must provide the path like a file not like a resource.
//Resource
DOMConfigurator.configureAndWatch("/log4j.xml", 2000L);
//File
DOMConfigurator.configureAndWatch("./src/log4j.xml", 2000L);
Try the second option and modify the log4j.xml and test it!
Related
I have a dynamic web-app. I'm working in a spring environment, with maven and mybatis. I'm deploying a war file on tomcat. I'm using log4j for logging the data. I want this data to be accessible real time to everyone. For example if the home page of the web-app is localhost:8080 then I want something like localhost:8080/logs to display the logs real time. I know I have to Spring request mapping for this. But I don't know how to make it work, when my file keeps on updating itself.
Any help is appreciated
If it's a spring mvc.
In your controller, you need to read the file and write it into the response.
It's actually fair simple.
Code snippet for you as reference: The following is SUDO code. Just give you the direction
#RequestMapping("/log")
public class Controller {
public void readLog(HttpRequest req, HttpResponse resp) {
File file = new File('YOUR_FILE_LOCATION')
FileReader fr = new FileReader(file);
// you can read every line for the log file and write into resposne
while(str = nextLine()) {
resp.println(str)
}
fr.close()
}
}
You can use HTML Layout feature of log4j as following:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${catalina.base}/webapps/yourappname/log.html
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout
log4j.appender.FILE.layout.Title=HTML logs
log4j.appender.FILE.layout.LocationInfo=true
This file should be accessible on http://localhost:8080/yourappname/log.html. Tomcat will provide catalina.base/catalina.home as system property or you can provide full file path.
So, here's the deal.
I'm using Spring Framework to develop a appointment app.
Everything's going fine in my localhost, even the email send part.
But when i pass the project to my weblogic, the resource.loader.path property appears not to load.
Here's the important part of my code:
Properties prop = new Properties();
prop.setProperty("resource.loader", "class");
prop.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
prop.setProperty("class.resource.loader.path", "../jsp/email-templates");
VelocityEngine.init(prop);
Template template = VelocityEngine.getTemplate("user-response.jsp");
As i said, just the important part of my code. Basically is configured like this with a Properties Object and the VelocityEngine.getTemplate() loading the user-response.jsp file that are inside the folder mentioned above.
As i said, in my localhost, he just works fine but in weblogic appears that way:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource '..nulluser-response.jsp'
For some reason, the class.resource.loader.path property do not load in weblogic.
So... someone has already been through this problem? Any ideias of what's wrong?
Obs.: The two projects (localhost and weblogic) are the same, using a version control software (bazaar)
I'd recommend making it relative to the CLASSPATH. If you put the /email-templates folder under WEB-INF/classes and make the load path "email-templates" it should work.
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.