I created a dynamic web project using IBM Rational Application Developer (RAD). I used java.util.logging as the logging framework. I put the logging.properties in WEB-INF/classes directly.
The problem which I am facing is, the application could not load the logging.properties even I put it in the WEB-INF/classes. I add the following generic JVM arguments in the WebSphere Application Server Administrator's Console
-Djava.util.logging.config.file="logging.properties"
I add the following code snippet in the servlet init method.
Properties prop = System.getProperties();
prop.setProperty("java.util.logging.config.file", "logging.properties");
System.out.println("Is file exists " + file.exists());
try {
LogManager.getLogManager().readConfiguration();
} catch (IOException ex) {
ex.printStackTrace();
}
I off the console level debug in logging.properties, so I should not get the logging in console. But currently I am getting the logs in console not in log files which I mentioned in logging.properits.
logging.properties
#------------------------------------------
# Handlers
#-----------------------------------------
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
# Default global logging level
.level=ALL
# ConsoleHandler
java.util.logging.ConsoleHandler.level=OFF
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# FileHandler
java.util.logging.FileHandler.level=FINE
# Naming style for the output file:
java.util.logging.FileHandler.pattern=${SERVER_LOG_ROOT}/nyllogs/loadData.log
# Name of the character set encoding to use
java.util.logging.FileHandler.encoding=UTF8
# Limiting size of output file in bytes:
java.util.logging.FileHandler.limit=25000000
# Number of output files to cycle through
java.util.logging.FileHandler.count=2
# Style of output (Simple or XML):
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
Please let me know why the application couldnt pick up the logging.properties file?
In a WebSphere server, the effect of what you are trying to do would be to change the logging configuration not only of the application, but the entire server. Since WebSphere itself uses java.util.logging, this would mean that everything that is logged internally by WebSphere goes to the same file as the application logs. That would be pointless because then you may as well use the standard WebSphere log files (SystemOut.log and trace.log).
In addition, since WebSphere installs its own LogHandler, it is likely that it will forbid usage of the readConfiguration() method.
Read the configuration from an inputstream using readConfiguration(is). Your code sets a property with relative path but the JVM cannot look into it.
Properties prop = System.getProperties();
prop.setProperty("java.util.logging.config.file", "logging.properties");
Calling the readConfiguration() method without arguments only reloads the properties, which may not be loaded since your path is relative.
public void readConfiguration()
throws IOException,SecurityException
Reinitialize the logging properties and reread the logging configuration.
Use an absolute path for the property or pass an Inputstream. Here's an example loading the properties from a file and using an InputStream.
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'm using slf4j and log4j2 combination as logging framework in my application. To redirect logs of java.util.logging into slf4j, I'm using jul-to-slf4j. I also have to execute :
static {
LogManager.getLogManager().reset();
SLF4JBridgeHandler.install();
}
the above to register the handler to make this work and it is working fine. But I would like to avoid this static block if it possible to do the same handler register by using log4j2.xml file configuration. I spent much time to find the xml configuration but could not found it.
If you are not running the code in a servlet container like Tomcat you can simply add the -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager to the launch command.
Otherwise should be able to use a logging.properties file with contents:
handlers=org.slf4j.bridge.SLF4JBridgeHandler
org.apache.logging.log4j.jul.Log4jBridgeHandler.propagateLevels = true
Then launch the application with -Djava.util.logging.config.file=logging.properties. You'll have to adjust the path to the logging.properties
I am required to write logs for jaxb-impl.jar. When I searched online (https://java.net/projects/jaxb/lists/users/archive/2007-01/message/5), I found that we need to enable logs in JDK i.e logging.properties (C:\Program Files (x86)\IBM\WMBT700\jdk\jre\lib). Based on my further search I found that we need to enable logs and trace in Java control panel, I even specified the jre inside the control panel.
Now to see the logs in the file, I have configured the file handler in logging.properties
java -Djava.util.logging.config.file=myfile.log
Handler used is specified as file handler
handlers= java.util.logging.FileHandler
and the process is specified as
com.sun.xml.bind = FINEST
I am still not getting the logs.
I checked the class files using decompiler. To my surprise I found no code as java.util.log..., Is that the reason why logs are not getting generated.
-Djava.util.logging.config.file=myfile
Here, myfile should be custom configuration file.
So try by either specifying your own configuration file or without specifying this system property in case if you have modified logging.properties
Also level could be changed to,
.level = ALL (default is INFO).
This may not be an answer but as i am new and i do not have that much reputation to comment so added it as answer if it may help :)
I need to get the path of catalina.out file which is configured in the logging.properties.
Is there a way to retrieve the property "1catalina.org.apache.juli.FileHandler.directory" via java without knowing the path to the properties file?
If I have understood you correctly, then you want to read an attribute of the Tomcat logging.properties file. As you can see here in the Tomcat FAQ for Logging, java.util.logging.config.file is used to define the path to the property file.
You can then retrieve this path via Java System Properties:
String pathLogProps = System.getProperty("java.util.logging.config.file");
Properties properties = new Properties();
try {
properties.load(new FileInputStream(pathLogProps));
System.out.println(prop.getProperty("database"));
} catch (IOException ex) {
ex.printStackTrace();
}
If this is not set, I would follow the hints from the Apache Tomcat 7 Logging Documentation:
JULI is enabled by default, and supports per classloader
configuration, in addition to the regular global java.util.logging
configuration. This means that logging can be configured at the
following layers:
Globally. That is usually done in the ${catalina.base}/conf/logging.properties file. The file is specified
by the java.util.logging.config.file System property which is set by
the startup scripts.
If it is not readable or is not configured, the default is to use the ${java.home}/lib/logging.properties file in the JRE. In the web
application. The file will be WEB-INF/classes/logging.properties
Per default the path to the logging.properties file should be available over the environment variable catalina.base, respectively CATALINA_BASE.
The file catalina.out is specified neither in any system property, nor is is specified in logging.properties (take a look, it isn't there).
Instead, catalina.out is created by the launching script's shell-based output redirection from within bin/catalina.sh (or bin/catalina.bat). That means that this file is only available if you have launched Tomcat using those scripts, or scripts that emulate this behavior. For example, if you use jsvc on a *NIX platform, or the Tomcat Windows Service, then the file logs/catalina.out does not get created (at least not by default).
If you want to take a peek to see if the file exists, you can bet on it being in $CATALINA_BASE/logs/catalina.out. Recent versions of Tomcat define the catalina.base system property so you can easily take a look like this:
File catalinaOut = new File(System.getProperty("catalina.base"), "logs/catalina.out");
if(catalinaOut.exists())
{
// Do whatever you want
}
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!