log4j basic configuration - log4j

I used log4j to log some steps in my application. To be quick and dirty, I used:
org.apache.log4j.BasicConfigurator.configure();
This output my logs in the Eclipse console.
I want to know if and how to set the level threshold higher than DEBUG? In other word, I do not want to display DEBUG level message, just ERR, WARN, INFO.
Thank you.
EDIT:
May I use this following?
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
[...]
Logger logger = Logger.getLogger(this.class);
logger.setLevel(Level.INFO);

I think the simplest way would be:
Logger.getRootLogger().setLevel(Level.INFO);

org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);

Assuming you are calling BasicConfigurator.configure() before any loggers are called:
You can use either of these config files to change it without recompiling:
log4j.properties
log4j.rootLogger=INFO
log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<root>
<priority value="INFO"/>
</root>
</log4j:configuration>
One of these must be on command line.

1) Find your appender, you sould have something like this in your log4j.xml configuration file.
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/logs/rmDebug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1500KB"/>
<param name="MaxBackupIndex" value="2"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="**FOOBAR** %d{dd.MM.yyyy HH:mm:ss} %c %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
You see levelMin and LevelMax value ? levelMin is where you begin to log, and levelMax, where you stop to log. ( with this specific appender ). You can have several appender.
Then for assign this appender to a class or package. You can do something like that :
<category name="com.foobar.automation.doremiResourceManager" additivity="true">
<appender-ref ref="DEBUG"/>
</category>

If you are not configuring inside the properties file, use this:
Logger root = Logger.getRootLogger();
root.setLevel(Level.INFO);
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));

What BasicConfigurator.configure() do is adding root logger to a ConsoleAppender and set the appender layout to a PatternLayout with the pattern "%r [%t] %-5p %c - %m%n". So you need to set the root logger's level. If you only set the level of the logger of this class, the level of root logger is unchanged, then all the other loggers(except this class's) may still use root logger's level, so you will still see the unwanted logs.
See http://logging.apache.org/log4j/1.2/manual.html.

Related

How to change the log4j log file name dynamically

I am sure that this question is answered multiple times. But somehow, it's not working for me.
My appender is something like this.
<appender name="myAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="File" value="D:\\out\\MyApp_${output}.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %5p %C{1}:%L - %m%n"/>
</layout>
</appender>
I am setting the system property before creating the logger.
System.setProperty("output", "abc");
Then, I am creating the logger like this.
logger = Logger.getLogger("myAppender");
But the output file is generated only like this.
MyApp_.log
Am I missing something? Thanks in advance.
Did you try -Doutput=abc during the startup?
Here is another thread with similar problem that may help you.
How to give environmental variable path for file appender in configuration file in log4j

log4j: Unrecognized element rollingPolicy

I have an app running on Tomcat 7.0.2 and using log4j for logging.
Shortly after tomcat start the following messages appear in catalina.out:
INFO: Initializing log4j from [file:///export0/home/tomcat/appconf/log4j.xml]
log4j:WARN Unrecognized element rollingPolicy
log4j:WARN Please set a rolling policy for the RollingFileAppender named 'PERFFILE'
log4j:WARN Unrecognized element rollingPolicy
log4j:WARN Please set a rolling policy for the RollingFileAppender named 'FILE'
and when my actual log messages should print, I see the following:
log4j:ERROR No output stream or file set for the appender named [FILE].
log4j:ERROR No output stream or file set for the appender named [PERFFILE].
These two appenders are defined like this:
<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="true" />
<param name="File" value="${catalina.base}/logs/server.log" />
<rollingPolicy class="com.myapp.logging.AgingTimeBasedRollingFilePolicy">
<param name="fileNamePattern" value="${catalina.base}/logs/archive/server.%d{yyyy-MM-dd}.log.gz" />
<param name="keepFilesForDays" value="30" />
</rollingPolicy>
<layout class="com.myapp.logging.jboss.WebappAwarePattern">
<param name="ConversionPattern" value="%d{ISO8601}{${server.localTimezone}} %p [%X{webapp}:%c{2}] %m%n" />
</layout>
</appender>
<appender name="PERFFILE" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="true" />
<param name="File" value="${catalina.base}/logs/performance.log" />
<rollingPolicy class="com.myapp.logging.AgingTimeBasedRollingFilePolicy">
<param name="fileNamePattern" value="${catalina.base}/logs/archive/performance.%d{yyyy-MM-dd}.log.gz" />
<param name="keepFilesForDays" value="30" />
</rollingPolicy>
<layout class="com.myapp.logging.jboss.WebappAwarePattern">
<param name="ConversionPattern" value="%d{ISO8601}{${server.localTimezone}} %p [%X{webapp}:%c{2}] %throwable{0} %m%n" />
</layout>
</appender>
I've been having a hard time trying to fugure out why this errors occur but got no result (probably my log4j.xml can not be parsed correctly)
So any ideas why this happens would be highly appreciated.
Thanks in advance!
Taken from the Appenders documentation page:
Parameter Name Type Description
policy TriggeringPolicy The policy to use to determine
if a rollover should occur.
I think you should replace rollingPolicy with policy. This probably depends on whether you are using 2.0 or 1.2; I dont see policy in the 1.2 version.
I think you set the wrong class or your custom class for the rollingPolicy class on your two appenders that maybe not exist or not recognize by log4j
<rollingPolicy class="com.myapp.logging.AgingTimeBasedRollingFilePolicy">
So change both appenders class with:
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
that have been provided by apache-log4j-extras jar

configuration log4j axis1 No appenders could be find for logger

I try to deploy web service using axis1 and use in my web service class log4j logger as
private static final Logger logger = Logger.getLogger(MobileService.class
.getName());
logger.debug("Count nodes" + nodes.getLength());
My class has default package. And I use next log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="HTML-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${catalina.home}/webapps/axis/WEB-INF/classes/log.html" />
<param name="DatePattern" value="'.'yyyy-MM-dd-HH'.html'" />
<layout class="org.apache.log4j.HTMLLayout" />
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ALL"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
</log4j:configuration>
Also I add in class path for deploying my app next
set CATALINA_HOME=C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0
set PATH_TO_LOG4J=C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\axis\WEB-INF\classes\log4j.xml
java -Dlog4j.info -Dlog4j.configuration="file:C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\axis\WEB-INF\classes\log4j.xml" -cp "%CATALINA_HOME%\webapps\axis\WEB-INF\lib\axis.jar;%CATALINA_HOME%\webapps\axis\WEB-INF\lib\jaxrpc.jar;%CATALINA_HOME%\webapps\axis\WEB-INF\lib\commons-logging-1.0.4.jar;%CATALINA_HOME%\webapps\axis\WEB-INF\lib\wsdl4j-1.5.1.jar;%CATALINA_HOME%\webapps\axis\WEB-INF\lib\commons-discovery-0.2.jar;%CATALINA_HOME%\webapps\axis\WEB-INF\lib\saaj.jar;%CATALINA_HOME%\common\lib\activation.jar;%CATALINA_HOME%\common\lib\mail.jar;%CATALINA_HOME%\webapps\axis\log4j-1.2.8.jar" org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService MobileService.wsdd
pause
But even -Dlog4j.info -Dlog4j.configuration= doesn't help me. I get error no appenders could be found for logger (org.apache.axis.i18n.projectresourcebundle).
Can you help me?
I think, you forgot to configure a logger. To configure the root logger, for example, place the following lines to the end of your log4j.xml file (before </log4j:configuration>):
<root>
<priority value ="debug" />
<appender-ref ref="HTML-APPENDER" />
</root>

log4J: Failure in post-close rollover action using TimeBasedRollingPolicy

I have setup TimeBasedRollingPolicy to rollout the file every minute (for test purpose) and the problem I am facing is a warning and no zip or gz file is being created. Warning is:
log4j:WARN Failure in post-close rollover action
I attached the source to figure-out the problem but have no success yet. Am I missing any configuration in my log4j.xml?
<appender name="errorAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="File" value="C:/error.log"/>
<param name="Append" value="true"/>
<param name="BufferedIO" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="C:/error.%d{ddMMMyyyy HH:mm:ss}.log.gz" />
<param name="ActiveFileName" value="C:/error.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C (line:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="error"/>
<param name="LevelMin" value="error"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
</appender>
I am using log4j-1.2.17 and apache-log4j-extras-1.1. Has anybody seen this problem or have any clue about it?
Problem with "log4j:WARN Failure in post-close rollover action" message is that in windows-based systems you can not create a file name with the ":" char, so the FileNamePattern specified should not contain any one of these: \, /, :, *, ?, ", <, >, |
Here it is a log4j.xml for my application that works fine using a rolling file appender. For testing purposes I made the rolling to create a new file every second:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consola" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="[%d{yyyyMMdd HH:mm:ss:mm,SSS}]%-5p [%t] [%c{1}-%M:%L] - %m%n"/>
</layout>
</appender>
<appender name="desarr" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="false"/>
<rollingPolicy name="desarr" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="fileNamePattern" value="C:/workspace/Probador/log/backups/importacion222.log_%d{mmss_mm}"/>
<param name="activeFileName" value="C:/workspace/Probador/log/importacion222.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="[%d{yyyyMMdd HH:mm:ss:mm,SSS}]%-5p [%t] [%c{1}-%M] - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="consola" />
<appender-ref ref="desarr"/>
</root>
</log4j:configuration>
Special attention to:
<param name="fileNamePattern" value="C:/workspace/Probador/log/backups/importacion222.log_%d{mmss_mm}"/>
Try this before attempting to zip the file.
I hit the same issue in log4j with WARN message - "log4j:WARN Failure in post-close rollover action" and the log file was not rolling over. It was root caused to insufficient permission issue on the directory into which log file was getting written. In this case, Java's File.renameTo() method was failing silently (just returns a boolean false). Took lot of time to figure out the issue :(
I am using log4j-1.2.17 and apache-log4j-extras-1.1. Has anybody seen
this problem and have any clue about it?
I have also observed this problem using log4j-1.2.16 and apache-log4j-extras-1.1. The exact same message.
I have tried various tweaks to no avail. The only time when rollingPolicy->FileNamePattern seems to be observed is when it is used without the appender->File parameter and rollingPolicy->ActiveFileName parameter. But even still I have not seen it rollover successfully nor gz or zip previous files.
I also get the same messages:
log4j: setFile called: somepath/somefile.log, true
log4j: setFile ended
log4j:WARN Failure in post-close rollover action
Very frustrating.
For me the solution was to create manually the directory for archived files.
I Also had the same problem,but in my case it was because of the fact that the 'fileNamePattern' path folder did not exist. Rectifying that worked for me and the rollover files were being created then.
If you are using the org.apache.log4j.rolling.TimeBasedRollingPolicy rollingPolicy, then the directory must exist prior to log4j being able to rotate.
For example, the following rollover will only work if /var/log/blah/archive/YYYY/MM directory exists; create it in a nightly cronjob should do the trick. And, as mentioned previously, this will also occur when there is not enough permission to create the log file.
<appender name="infoFile"
class="org.apache.log4j.rolling.RollingFileAppender">
<param name="threshold"
value="INFO"/>
<param name="append"
value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName"
value="/var/log/blah/file.log"/>
<!-- IMPORTANT the archive folder must already exist, or log4j cannot
put the rotated log there, and will keep using the old one -->
<param name="FileNamePattern"
value="/var/log/blah/archive/%d{yyyy}/%d{MM}/file.log.%d{yyyy-MM-dd}.gz"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%5p | %-40c{2} | %-4L | %d{yyyy-MM-dd}T%d{HH:mm:ss} | %m%n"/>
</layout>
</appender>

settings in jboss-log4j.xml not taking effect

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.

Resources