36701

Log4j - file extension (.log) disappears after using DatePattern

Question:

I'm having a problem with my Log4j.xml file. When I save a log file like that:

<appender name="file" class="org.apache.log4j.DailyRollingFileAppender"> <param name="append" value="true" /> <param name="datePattern" value="_dd.MM.yyyy_HH.mm.ss" /> <param name="file" value="/logs/dailyschedule.log" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} class:[%c] method:[%t] [%-5p] : %m%n"/> </layout> </appender>

the file is ok, but has no file extension. Is it a bug or I'm doing something wrong?

Edit: Before I saw Paul's answer, I was temporarily solved my problem by using a simple class for initializing my logger. It wasn't the best decision, but it can be usefull to someone with similar problem:

public class DomainLogger { private Logger logger = null; public DomainLogger(String directoryName, String fileName) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("_dd.MM.yyyy"); String timeNow = sdf.format(new Date()); String fullFilePath = System.getenv("DOMAIN_HOME") + directoryName + "\\" + fileName + timeNow + ".log"; System.out.println("full filepath is " + fullFilePath); logger = Logger.getLogger(timeNow); //warning! different time will create a new file !!! PatternLayout patternLayout = new PatternLayout("%m%n"); FileAppender fileAppender = new FileAppender(patternLayout, fullFilePath); logger.addAppender(fileAppender); } public void writeLog(String logMessage) { logger.info(logMessage); }

}

Answer1:

If you're using log4j 1.x, we strongly recommend that you use <a href="https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html" rel="nofollow">org.apache.log4j.rolling.RollingFileAppender</a><sup><b>1</b></sup> instead of <a href="https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html" rel="nofollow">org.apache.log4j.DailyRollingFileAppender</a> (may lose messages, <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=43374" rel="nofollow">Bug 43374</a>).

So the configuration of you appender can be:

<pre class="lang-xml prettyprint-override"><appender name="file" class="org.apache.log4j.rolling.RollingFileAppender"> <param name="encoding" value="UTF-8" /> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="activeFileName" value="/logs/dailyschedule.log" /> <param name="fileNamePattern" value="/logs/dailyschedule%d{_dd.MM.yyyy_HH.mm.ss}.log" /> </rollingPolicy> <layout class="org.apache.log4j.PatternLayout"> <param name="conversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} class:[%c] method:[%t] [%-5p] : %m%n" /> </layout> </appender>

Using this configuration, you will get finenames like the next:<sup><b>2</b></sup>

<pre class="lang-none prettyprint-override">dailyschedule.log dailyschedule_28.08.2014_14.57.45.log dailyschedule_28.08.2014_14.57.46.log dailyschedule_28.08.2014_14.57.47.log dailyschedule_28.08.2014_14.57.48.log dailyschedule_28.08.2014_14.57.49.log <hr />

<em>Notes</em>

<ol><li>In that case, you need to add the respective jar (<a href="http://search.maven.org/remotecontent?filepath=log4j/apache-log4j-extras/1.2.17/apache-log4j-extras-1.2.17.jar" rel="nofollow">apache-log4j-extras-1.2.17.jar</a>).</li> <li>If you don't want the first filename (dailyschedule.log), drop the param activeFileName in the rolling policy.</li> </ol>

Recommend

  • Traversing a multi-dimensional array
  • Hoisting and variable scope
  • how to make my program to wait for a particular thread and also not affecting my GUI's interact
  • how C++ Implicitly convert c style string to a string object?
  • Firebase Save Notification to DB not working when app is not running
  • how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsi
  • Formatting a TimeSpan to look like a time zone offset
  • Find 4 minimal values in 4 __m256d registers
  • Convert date string value from U.S. to native Date with current locale U.S.?
  • logging static methods in a parent class
  • Year, Month, and Day parameters describe an un-representable DateTime in Persian calender
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Init child with Parent instance
  • reducers get Function not Object in redux, what's wrong with it?
  • A simple datepicker in VueJS
  • How to parsing NSDate to RFC 822 always use in English?
  • Gradle - How to detect --debug flag from command line?
  • IIS 7.5 404 Error for .PDF files
  • Unexpected Error Google Apps Script Fetch
  • CakePHP 3: change order in dateWidget
  • Installing SSL on AWS EC2 Bitnami Mean Stack
  • Typecasting `this` of a base class template to its derived class
  • stop execution in Custom validator if it false
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Most efficient way to move table rows from one table to another
  • Unable to install Git-core+svn by MacPorts
  • Inline R code in YAML for rmarkdown doesn't run
  • Django simple Captcha “No module named fields” error
  • Could not find rake using whenever rails
  • Highlight one bar in a series in highcharts?
  • Trying to switch camera back to front but getting exception
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • WPF Applying a trigger on binding failure
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Programmatically clearing map cache