log4j log file not getting updated - java

I am using a apache's log4j for logging errors in my application. When I start the tomcat server, the log info information is written into the log file as expected (Also the log info is written into the log file when I stop the tomcat server). But when I start using the application, I log information I am expecting to be written through the app is not written into the log file. For e.g. I am giving inputs that will give exception, but the log.error(e,e) isn't getting written in the log file.
this is how i am using the Logger
static Logger log = Logger.getLogger(MyClass.class);
log.info("my message" );
log.error(e,e);
Please help
EDIT: Adding log4j.properties file contents
log4j.rootLogger =INFO, FILE, stdout
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${catalina.home}/logs/myapp.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ABSOLUTE} %5p\t\t%c{1} :%L - %m%n

It seems that you have not configured your logger. Please find log4j.xml or logging.properties typically located under TOMCAT_HOME/conf. Edit them to include your application's package.
This will help you to write your application level log messages to log file of your server (tomcat).
Better way is to manage your own logging configuration and files. Create log4j.xml file and put it under WEB-INF/classes into your application. You can find a lot of examples and tutorials that will help you to write your log4j.xml. For example this one: http://logging.apache.org/log4j/1.2/manual.html

Related

Limit log level from servlet that's written to Tomcat's catalina.out log level

I have a web app WAR file which I can't modify. The code in the WAR file uses Log4j, and also contains log4j config file logging.properties that's loaded explicitly. This file sets rootLogger level to DEBUG. The web app is such that almost 50G of DEBUG logs are written to catalina.out on startup.
I need to limit the log level of messages coming from the servlet into catalina.out to INFO. How can I achieve this?
Tomcat version is 5.5
I imagine just adding something like this to logging.properties should solve it.
log4j.logger.org.apache.catalina=INFO
Although the Apache Tomcat documentation suggests this.
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO
log4j.logger.org.apache.catalina.core=WARN
log4j.logger.org.apache.catalina.session=ERROR

How to restrict log4j console appender to log only on localhost

Working in both development and deployment environment,
I want my log4j.properties to have include console appender, so I could view log messages when I develop. However when I deploy the application, I prefer to disable the console appender.
Is there a filter I can add on the console appender to do this?
Deploy the application with a different log4j config file, that's why it's an external file.

Log is not working inside the java file. But works inside the servlet

I have issue in executing the logs. I have two projects: one is a servlet and the other one is a simple java file. private static Logger logger=Logger.getLogger(myServiceServlet.class); This was working fine. Its writing log wen i start running the Tomcat server. But the same thing is not achieve in my simple java file, it converted into WS and try to deploy in axis/Tomcat server
Servlet is directly running in Tomcat server.
But java file is converted into Webservice.aar inside the Axis2--->Tomcat server.
Log Properites
# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=C:/res backup/apache-tomcat-6.0.35/webapps/mylog/logs/servicelog.log
log4j.appender.R.MaxFileSize=2048KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=50
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n`
The .aar files are not running in the same context as the JARs in WEB-INF/lib.
They share some kind of context as you can load references to the WEB-INF/lib classes, however its not 100% consistent at this point.
I would recommend you build some kind of wrapper in your .aar files that do nothing else then forwarding the calls into methods of JAR files that are situated in WEB-INF/lib.
You just need to make sure you load the reference from the .aar to the methods in WEB-INF/lib context correctly.
To throw errors in the .aar classes I would recommend you catch them and then throw an "AxisFault".
throw new AxisFault(err.getMessage());
http://axis.apache.org/axis2/java/core/api/org/apache/axis2/AxisFault.html
That way we have have been able to solve almost all our issue with the context of .aar files.
Sebastian

How to specify Tomcat logs as home for log4j log file

My WAR is using a log4j FileAppender configured via a log4j.properties file under the classes/ directory inside the WAR.
I have configured my log4j appender as such:
# Set the root logger to DEBUG.
log4j.rootLogger=DEBUG
# MonitorLog - used to log messages in the Monitor.log file.
log4j.appender.MonitorAppender=org.apache.log4j.FileAppender
log4j.appender.MonitorAppender.File=MonitorLog.log
log4j.appender.MonitorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.MonitorAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# Use the MonitorAppender to log all messages.
log4j.logger.*=DEBUG,MonitorAppender
With this configuration I am trying to achieve the following:
All log messages DEBUG-level and higher get logged (so everything)
I want the MonitorLog.log file to be located under Tomcat's logs/ directory
Does this configuration achieve these items, and if not, what needs to change?
If you know this WAR will only be deployed to a tomcat, you can take advantage of the system property catalina.base, which represents the root of your tomcat base folder (there is also a cataline.home, but they are often the same unless you have multiple tomcats running on the same machine and are sharing the server libs, but i digress).
So update as follows:
log4j.appender.MonitorAppender.File=${catalina.base}/logs/MonitorLog.log

Reduce logs of log4j in catalina.out

I am using the log4j API for logging in my Java based Tomcat application and using version 1.2.14 (log4j-1.2.14.jar). The problem is that it creates more logs in the catalina.out log file, which are very micro level logs and not required for me. I am facing the problem of memory in my machine, and I want to reduce the logging of the log4j logger. Is there a way to reduce logs such that I can save my memory?
Setting a properties or a XML configuration file will allow you to control log4j output as stated in the log4j manual.
An example of the properties file would be like this
# Root logger option
log4j.rootLogger=**ERROR**, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\mylogging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Note the rootLogger parameter, as it controls how much information you get, whether you need only error messages or everything down to information messages.
In order to set it properly, look in Log4j Configuration Using Properties File for extra information as you can also use it in the XML version (see Multiple Appenders Using XML File). Tip: place it in src/ or in project root.
You will have to look at CATALINA_HOME/conf folder for log4j.properties and raise the logging level to say, warn or error depending upon your needs

Categories