I have java application which is running on play framework 1.2.5.
I want to do logging in such a way that every module will have its own log file and respective module logging will go in its own file.
Is that possible using play logging? or is there any other way to do it?
Any help will be greatly appreciated.
Yes of course, it's possible. You can use advanced logger setting using apache log4j. By default, Play!Framework use apache log4j for logging purpose, see this documentation.
You must enabled this advanced setting on application.conf file using the entry like :
# More logging configuration - config file located at the same level on this file
application.log.path=/log4j.properties
application.log.system.out=off
Suppose you have two modules that located on com.mymodule and com.othermodule package. so if you want to make these modules logging on different file, your log4j.properties file should be like this :
# Define logging file appender for mymodule package
log4j.appender.mymodule=org.apache.log4j.FileAppender
log4j.appender.mymodule.File=mymodule.log
log4j.appender.mymodule.layout=org.apache.log4j.PatternLayout
# Define logging file appender for othermodule package
log4j.appender.othermodule=org.apache.log4j.FileAppender
log4j.appender.othermodule.File=othermodule.log
log4j.appender.othermodule.layout=org.apache.log4j.PatternLayout
log4j.logger.com.mymodule=INFO, package1
log4j.logger.com.othermodule=INFO, package2
For more reference, try to learn from following links:
How to create different log files for different packages using same log4j logger?
Apache Log4j learning resource
Related
the log file is generated when I run the code within IDE (Intellij IDEA).
as soon as I create runnable jar of the code and then try to run the jar then the logs are not generating.
I have made sure the log4j2.xml file is a part of classpath.
is there anything extra I have to do while creating jar in the Intellij IDEA?
Taken from the FAQ: How do I debug my configuration?
First, make sure you have the right jar files on your classpath. You need at least log4j-api and log4j-core.
Next, check the name of your configuration file. By default, log4j2 will look for a configuration file named log4j2.xml on the classpath. Note the “2” in the file name! (See the configuration manual page for more details.)
From log4j-2.9 onward
From log4j-2.9 onward, log4j2 will print all internal logging to the console if system property log4j2.debug is either defined empty or its value equals to true (ignoring case).
Prior to log4j-2.9
Prior to log4j-2.9, there are two places where internal logging can be controlled:
If the configuration file is found correctly, log4j2 internal status logging can be controlled by setting in the configuration file. This will display detailed log4j2-internal log statements on the console about what happens during the configuration process. This may be useful to trouble-shoot configuration issues. By default the status logger level is WARN, so you only see notifications when there is a problem.
If the configuration file is not found correctly, you can still enable log4j2 internal status logging by setting system property -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE.
I am new to SLF4j and I don't know if the logback.xml file has loaded properly or not. The logback.xml file is in PROJECTNAME/src/main/java where all my packages are found.
My questions are:
How can I know if the configuration file has properly loaded or not
?
How can restrict the logging only from an explicit set of class,
only to avoid logging from libraries
You can add the debug="true" attribute to the <configuration> element to enable debug of the logback configuration. It will print the configuration to the console. See https://logback.qos.ch/manual/configuration.html#dumpingStatusData.
Simple answer, if the configuration file is loaded properly, you will see results in log file or console, depending on your configuration.
By default, logback searches file in src/main/resources instead of src/main/java if I remember correctly.
In the configuration file, you can define log lever on a specific logger. Normally you'll still want to see logs of the libraries, but maybe only WARN or ERROR, so you could set the root level to WARN/ERROR, and add a logger of your root package with DEBUG/INFO level.
Also, use a logback-test file (under src/test/resources) for your own dev environment.
I am using log4j for logger purpose. At the same time I am also using JXL to read/write Excel file.
But instead of writing log into log4j logger file, it is writing into jxl.log file.
What can be issue?
Looks like you have been using jxl-2.6.3.jar or similar version.
Log4j picks up the first configuration file with default file name ( i.e. log4j.xml or log4j.properties ) in your classpath if you haven't specified a specific name via JVM parameters. As jxl-2.6.3.jar contains a log4j.xml you ended up printing everything to jxl.log as defined in the log4j.xml
The best way to deal with these kind of problems is to run your application with -Dlog4j.debug JVM parameter. This would print a few line snippet when the log4j is initialized.
log4j: Using URL [jar:file:/C:/YourApp/WEB-INF/lib/jxl-2.6.3.jar!/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
...{Blah Blah Blah}
There are many ways in which you can solve this problem.
Use the newer versions of jxl which doesn't contain log4j.xml.
Make sure your log4j.properties file is on top of classpath.
Remove the log4j.xml from the jxl-2.6.3.jar (Dirty solution).
Pass the configuration file name in VM parameter as -Dlog4j.configuration=log4j.properties. This would atleast make sure log4j.xml inside jxl-2.6.3.jar will not be used. (But what if another jar with same name as log4j.properties?).
Rename your log4j.properties file to log4j-yourApp.properties and add VM parameter -Dlog4j.configuration=log4j-yourApp.properties This would definitely help and this is how it should be done to avoid these kind of situations.
More details on Log4j here
I've done my best to setup Eclipse and my Java application to use a log4j.properties file. However, it does not seem to be using the properties file and I'm not sure why.
Libraries: slf4j-api-1.6.1, slf4j-jdk14-1.6.1
Within the application the logging works fine. I am able to print info, warnings, and errors into the Eclipse console.
What I would like to be able to do is change the log level to debug and print all logging messages to both the console and a log file.
I have created a log4j.properties file that looks like this:
log4j.rootLogger=DEBUG,console,file
log4j.rootCategory=DEBUG, R, O
# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender
# File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=5
log4j.appender.file.File=checkLog.log
log4j.appender.file.threshold=DEBUG
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
My directory structure looks like this:
My Project
--src/
----MYProject/
------*.java
--bin/
----MYProject/
------*.class
--log4j/
----log4j.properties
In Eclipse I this this:
Run Configurations -> Classpath (tab) ->, right clicked on User Entries -> Added "log4j" as a new folder, and saved.
Then in my code I call the logger like this (sample code to demonstrate my approach so it may have syntax errors):
package MYProject;
import org.slf4j.LoggerFactory;
public class MyClass{
final org.slf4j.Logger test_logger = LoggerFactory.getLogger(MyClass.class);
public MyClass(){}
public someMethod(){
test_logger.debug("Some Debug");
test_logger.info("Some Info");
test_logger.warn("Some Warning");
test_logger.error("An Error");
}
}
I then call someMethod and it prints INFO, WARN, ERROR to the Eclipse console. It won't print DEBUG and won't print to a file.
I'd appreciate any suggestions on what I may be doing wrong.
There may be another log4j.properties or log4j.xml file in the classpath ahead of your log4j.properties. Open the run configuration for your project, and add -Dlog4j.debug=true as a VM Argument for your project. This will instruct log4j to print a lot of additional information on the console, including the config file that it is using.
If you are using the logging façade slf4j, then you need to specify exactly one logging backend by including the corresponding jar file for that backend. In your case, you have installed slf4j-jdk14-x.x.x.jar on your classpath, which is just a generic logger backend.
In order to use the log4j backend, you need to remove slf4j-jdk14-x.x.x.jar and replace it with slf4j-log4j12-x.x.x.jar. If you don't remove it, slf4j must choose only one backend jar, and probably not the one you want.
Of course you will also need the actual log4j-x.x.x.jar file on your classpath too.
Once these jars are properly in place, then the VM parameter of -Dlog4j.debug will actually work and be useful in debugging where your logging configs are coming from.
You need to tell your code to use the properties file. Before any logging is done please put
PropertyConfigurator.configure("log4j/log4j.properties");
You might have an old version in your target directory.
Clean the project and try again.
Apart from that you should make sure that you refresh the eclipse project if you didn't add the log4j.properties via eclipse.
delete jre(JRE System Library) of project in path project properties/library tab and set jre again!
I have a project which is in turn used by several other projects. I want log4j to log only my logs to a file that I have specified in the properties file. Other project use their own logging mechanisms and I have no control over them. My log4j files should not affect other project's logging. How should i configure my log4j property file?
So far what I'm doing is setting log4j.rootLogger = ERROR and for my module log4j.logger.com.xyz.myproject = INFO, FILE. Will this work without affecting other project's loggers? Or possibly limit logging to only my jar?
Thanks
It depends on the package structure of the other projects. Supposing that
loggers from other projects are created by Logger.getLogger(ClassA.class) AND
some of them rely on root logger configuration (have no specific log4j.category.loggerName settings AND
these projects contain subpackages of the package used by your project (i.e. your project's package is com.abc.def and other projects have packages deeper in the hierarchy com.abc.def.ghi THEN
changing com.abc.def logging level would affect other projects - they'll start logging on the level defined by com.abc.def.
Verify that it's not the case and you should be safe.
I suppose your jar is entirely contained in your own package (ex com.foo.mypackage). In this case, is just enough to add to your log4j configuration something like:
# Print only messages of priority WARN or above in the package com.foo
log4j.category.com.foo=WARN
# Print only messages of priority DEBUG or above in the package com.foo.mypackage
log4j.category.com.foo.mypackage=DEBUG
Regards,
M.