I have a war file that I deployed to JBOSS_HOME/server/default/deploy.
I add the following to the JBOSS_HOME/server/default/conf/jboss-log4j.xml
<appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Append" value="false"/>
<param name="Threshold" value="INFO"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
I restarted the server,
But the logs are not writing under Server.log,
Logs are generating under boot.log (including my war deployment logs also) in the path JBOSS_HOME/server/default/logs/.
(Im running Jboss with JDK7 on linux.)
See root section, add <appender-ref ref="FILE"/>:
<!-- ======================= -->
<!-- Setup the Root category -->
<!-- ======================= -->
<root>
<!--
Set the root logger priority via a system property. Note this is parsed by log4j,
so the full JBoss system property format is not supported; e.g.
setting a default via ${jboss.server.log.threshold:WARN} will not work.
-->
<priority value="${jboss.server.log.threshold}"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
Defining an appender is not enough in itself. You need to associate that appender with one or more loggers in the logger hierarchy. If you look at the other parts of the file, you'll see loggers defined which use the <appender-ref> element. This sends output from that logger to that appender.
If all you want to do is to send all logging to your new appender, then add <appender-ref> to the <root> element, which defines the root logger, e.g.
<root>
<!-- existing config in <root> -->
<appender-ref ref="FILE"/> <!-- reference to my new appender -->
</root>
See this tutorial at JavaLobby for more details.
Related
Our app runs on Tomcat 8 and Linux. We have log4j.xml shipped with the war that controls the level of logging for our applications logging. The log4j also defines logging levels for SQL.
In hibernate.cfg.xml, the "hibernate.show_sql" is set to true.
<property name="hibernate.show_sql">true</property>
In $CATALINA_BASE/conf/logging.properties
All references to ConsoleAppender:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Are removed, so nothing is sent to std out except for the SQL statements.
The: "hibernate.show_sql">true, sends all SQL statements being processed to catalina.out – please note that catalina.out is a file created only on Linux/Unix machines.
I read that "hibernate.show_sql">true, logs the SQL statements at DEBUG level. The problem is that I want to log those statements at ERROR level but don’t know how to control the level?
I read that that level for SQL logging can be controlled by log4j.xml, but changing it in log4j.xml has no effect on how the catalina.out is being logged. Does anyone know how can I control the SQL logging in catalina.out to be only at ERROR level?
Below is log4j.xml.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mylog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${catalina.base}/logs/mylog.log" />
<param name="Threshold" value="TRACE" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="300MB" />
<param name="MaxBackupIndex" value="10" />
<layout class="it.openutils.log4j.FilteredPatternLayout">
<param name="ConversionPattern"
value="%d{ISO8601} %5p [%t] Executor:%X{Executor} Type:%X{Type} A:%X{Account} C:%X{Campaign} %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="org.hibernate.SQL" additivity="false">
<level value="ERROR" />
<appender-ref ref="mylog" />
</logger>
<logger name="org.hibernate">
<level value="ERROR"/>
</logger>
<logger name="org.hibernate.jdbc.JDBCContext">
<level value="ERROR"/>
</logger>
<root>
<level value="info" />
<appender-ref ref="mylog" />
</root>
Since I was not able to find anyway to set the level to ERROR for Catalina.out, I decided to turn off the SQL logging all together. So I changed all levels in log4j.xml back to INFO and set this property to false in hibernate.cfg.xml.
<property name="hibernate.show_sql">false</property>
After a while logging, I noticed that there is nothing being logged in Catalina.out unless an error happens, so in this case, the errors and exceptions are being logged into Catalina.out and not the regular SQL statements, which is (almost) something I was trying to achieve at the first place. I do not know why that’s the case thou and where/how those logging levels are defined. But hope that helps somebody.
I'm new to log4j and trying to use it in a project. For some reason the info won't get displayed on the console. It only works when i change it to logger.error(). This is only happening in the userServiceImpl class the other classes like Controllers are fine.
This is the log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false"
xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- This default ConsoleAppender is used to log all NON perf4j messages
to System.out -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n" />
</layout>
</appender>
<!-- The root logger sends all log statements EXCEPT those sent to the perf4j
logger to System.out. -->
<root>
<level value="ERROR" />
<appender-ref ref="console" />
</root>
<logger name="com.click.heal.controller" additivity="false" >
<level value="INFO" />
<appender-ref ref="console"/>
</logger>
<logger name="com.click.heal.service" additivity="true" >
<level value="INFO" />
<appender-ref ref="console"/>
</logger>
</log4j:configuration>
Your root category is at ERROR level. Anything that doesn't fall under one of your other two loggers will fall under root, and will only log at ERROR level. If you change your root level to INFO, I bet it logs, right? Not saying that's what you want, but it would be a clue that your UserServiceImpl class is logging under root right now.
EDIT
Try turning additivity to false on your logger for the service package. additivity means the messages propogate to the parent, and since the parent is root and root logs to the same console appender, it seems likely that this is causing the problem.
I am trying to separate the Tomcat and application logs. I want the Tomcat logs written to catalina.out, and the application logs written to an app.log file. However, my application logs are being written in both files catalina.out and app.log.
How can I instruct Tomcat to write the logs in separate files?
You can specify the package of your application in log4j.xml or log4j.properties file to write the log messages to a separate file . You can see a sample log4j.xml that write log messages to a file appender based on the package com.urpackage . You can replace this with your application specific package .
You can learn more about this here
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log_file.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %-5p %-15c{1}: %m%n" />
</layout>
</appender>
<logger name="com.urpackage" additivity="false">
<appender-ref ref="FileAppender" />
</logger>
<root>
<level value="debug" />
</root>
I want to set up log4j so that all log meessages produced from classes under package com.foo.bar go to bar.log, and all the log meessages produced from classes under package com.bar.blatz go to blatz.log.
Questions
How do I do this using log4j.xml?
I know its possible using property files, but how do I do it using the XML configuration?
This is based on my answer to a similar question:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="BarLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="bar.log" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<!-- additional fooSystem logging -->
<appender name="BlatzLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="blatz.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<logger name="com.foo.bar">
<appender-ref ref="BarLogFile"/>
</logger>
<logger name="com.bar.blatz">
<appender-ref ref="BlatzLogFile"/>
</logger>
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
</log4j:configuration>
If you add an appender-ref to the root element, it will also receive com.foo.bar etc messages. You can stop that by specifying 'additivity="false"' on the loggers.
<root>
<level value="INFO"/>
<!-- no appender, output will be swallowed (I think) -->
</root>
We can add appenders here. It will work if the application is using root logger. for example quartz Scheduler API.
So log4j comes with two existing log rollers: RollingFileAppender, and DailyRollingFileAppender. Has anyone heard of an appender that does both of what the former do?
I need an appender that will roll log files based on filesize, but also append the current date to it.
I've been thinking about creating my own appender, but if there is already one that has been created, why not save the time and use that one?
Looks like you want a mix of the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html and the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/RollingFileAppender.html.
You'll have to code by yourself. The good news is: you'll just have "merge" those classes functionality, no "low level" new code required.
I know this post is a bit late but you could try out the TimeAndSizeRollingAppender. It's freely available under the Apache 2.0 license, download it from www.simonsite.org.uk.
The Log4J Extras from the "companions" project provide an array of policies for rolling, including what you're looking for.
APIDoc: http://logging.apache.org/log4j/extras/apidocs/index.html
Homepage: http://logging.apache.org/log4j/extras/
Using Log4j
As #JavaJigs said, Log4j's extras can be used.
First of all, if you're using Maven, add it as dependency keeping in mind to use the same log4j version, just to avoid any kind of conflict.
<!-- you should already have something like this -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- add this one, please note the version is the same than log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.2.17</version>
</dependency>
Then, set up an appender to use both kinds of rollings. This is a quick and dirty example that rolls every minute and / or when the size of the log files exceeds 1000 bytes.
<appender name="rollout5" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="log4j-current.log" />
<param name="FileNamePattern" value="log4j-%d{HH-mm}.%i.log.gz" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="1000" />
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n" />
</layout>
</appender>
Add the appender to the logger as you usually do.
If you run the application you should obtain something like that...
log4j-current.log
log4j-18-13.1480266729211.log.gz
log4j-18-12.1480266729095.log.gz
log4j-18-12.1480266729123.log.gz
Obviously the numbers of files and their names depend on how your application logs.
As you can see, the %i placeholder gets replaced with a sort of random yet ever increasing number. I was not able to find a way to have it replaced with number starting from 0 though. Nevertheless, such files listed in alphabetic order should match their historical order.
Other ideas
I know you mentioned explicitly log4j. But, if you can, why not to evaluate moving to log4j2? In this log4j2 piece of doc there are a couple of examples that seem to suit your needs.
Too late to reply. But hopefully this will help someone.
Add log4j dependency to your pom.xml file
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Grab a copy of this FileAppender implementation. Thanks to the author of this file.
http://haobangshou.googlecode.com/svn/trunk/hbs/APPLICATION/server/common/src/com/hbs/common/appender/TimeSizeRollingFileAppender.java
Place this log4j.xml file in your resources folder
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%p] %c{1} %m%n"/>
</layout>
</appender>
<appender name="debug" class="com.myproject.log.TimeSizeRollingFileAppender">
<param name="File" value="log/debug.log"/>
<param name="MaxBackupIndex" value="300"/>
<param name="Encoding" value="GB2312"/>
<!--CHANGE THIS TO A LARGER SIZE EG : 20MB. USE 1MB TO TEST IF THE SETTING WORKS.-->
<param name="MaxFileSize" value="1MB"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %p [%c] - [%m]%n"/>
</layout>
</appender>
<!--CHANGE name TO ROOT PACKAGE NAME OF YOUR PROJECT THAT YOU WANT TO LOG.-->
<logger name="com.myproject" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</logger>
<logger name="org.springframework" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</logger>
<logger name="org.hibernate" additivity="false">
<level value="debug"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</logger>
<root>
<priority value="INFO"></priority>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="debug"/>
</root>
To do a quick test:
import org.apache.log4j.Logger;
public class Main {
static public void main(String[] args) {
Logger log = Logger.getLogger(Main.class);
for(int i = 0; i < 10000; i ++)
log.info("Testing log");
}
}
Done!!
We use the class you see here. It works as you described and extends FileAppender.