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.
Related
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
I successfully configured log4j configuration by placing log4j.properties file inside in src/main/webapp/WEB-INF/classes folder. The logs for the application was working fine but my JUnit test cases logging was not working.
Searched over the internet and found out that I have to place my log4j.properties file inside src/test/resources for my JUnit test cases logging. Did so and logging for JUnit test cases also started working.
Deployed my application in my server after that and the log4j is now reading log4j.properties file from src/test/resources instead of one from WEB-INF/classes directory (the JUnit log4j.properties file creates a different log file for logging, and the logs are being written in that log file).
The application (maven project and not the war file) is being deployed in my weblogic server inside eclipse.
Please let me know how can i resolve this log4j configuration issue, so that my server uses the correct log4j configuration file.
log4j import:
import org.apache.log4j.Logger;
Logger instantiation:
private static Logger logger = Logger.getLogger(TestClass.class);
This is probably your IDE environments' issue. I think your IDE has test packages marked as source directories and when your IDE deploys web app, weblogic has your test packages on classpath.
Log4j seeks for configuration on classpath, so it likely sees test/log4j.properties first.
My 2 suggestions:
try to mark test/src as not exported to weblogic (or exluded from deploy - this is IDE specific)
force log4j configuration file by setting -Dlog4j.configuration=path/to/log4j.properties inside your weblogic startup configuration
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
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
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