0

I have a log4j.properties file with 2 custom appenders and 2 custom loggers which I need to convert to log4j2.properties file. FileNamePatternRollingFileAppender is an extension of RollingFileAppender. Read the documentation and tried to use some conversion tools but it did not work. How the log4j2.properties file will look like in this case?

log4j.logger.ibuild.reports.XML.XMLLogger=DEBUG,StatisticXmlAppender
log4j.logger.ibuild.reports.CSV.CSVLog=DEBUG,CsvRolledFileAppender

log4j.appender.StatisticXmlAppender=ibuild.reports.FileNamePatternRollingFileAppender
log4j.appender.StatisticXmlAppender.File=IndexStat
log4j.appender.StatisticXmlAppender.suffix=.xml
log4j.appender.StatisticXmlAppender.simpleDateFormatpattern=MM.dd.yy_HH.mm.ss.SSS
log4j.appender.StatisticXmlAppender.MaxFileSize=100MB
log4j.appender.StatisticXmlAppender.MaxBackupIndex=10
log4j.appender.StatisticXmlAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.StatisticXmlAppender.layout.ConversionPattern=%m%n

log4j.appender.CsvRolledFileAppender=ibuild.reports.FileNamePatternRollingFileAppender
log4j.appender.CsvRolledFileAppender.File=CallProcInfo
log4j.appender.CsvRolledFileAppender.suffix=.csv
log4j.appender.CsvRolledFileAppender.simpleDateFormatpattern=MM.dd.yy_HH.mm.ss.SSS
log4j.appender.CsvRolledFileAppender.MaxFileSize=100MB
log4j.appender.CsvRolledFileAppender.MaxBackupIndex=10
log4j.appender.CsvRolledFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.CsvRolledFileAppender.layout.ConversionPattern=%m%n

Thanks

EDIT:

This is the log4j2.xml after using the recommended converter and with using the original class of RollingFileAppender.

<?xml version="1.0" ?><Configuration name="Log4j1">
<Appenders>
    <RollingFile 
        name="StatisticXmlAppender"
        fileName="IndexStat"
        filePattern="IndexStat.%i">
        <PatternLayout pattern="%m%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="100MB"/>
        </Policies>
        <DefaultRolloverStrategy max="10"/>
    </RollingFile>
    <RollingFile
        name="CsvRolledFileAppender"
        fileName="CallProcInfo"
        filePattern="CallProcInfo.%i">
        <PatternLayout pattern="%m%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="100MB"/>
        </Policies>
        <DefaultRolloverStrategy max="10"/>
    </RollingFile>
</Appenders>
<Loggers>
    <Logger
     name="ibuild.reports.CSV.CSVLog" level="DEBUG">
     <AppenderRef ref="CsvRolledFileAppender"/>
     </Logger>
     <Logger name="ibuild.reports.XML.XMLLogger" level="DEBUG">
     <AppenderRef ref="StatisticXmlAppender"/>
     </Logger>
</Loggers>
Or123
  • 1
  • 3
  • As you can guess by the name, the `ibuild.reports.FileNamePatternRollingFileAppender` is not a standard Log4j appender. Can you describe how your appender works? What are the names of the log files, how often it rotates them, etc. – Piotr P. Karwasz Dec 30 '21 at 18:48
  • Thanks for the reply. You right, I forgot to mention - this class extends the standard "RollingFileAppender" class. And the configuration worked as it mentioned here. I just want to know how to new properties file (log4j2) will look like. It is also enough for me to see just implantation of the loggers and 1 appender because the other appender is pretty much the same as you can see, apart of few attributes. thanks. – Or123 Dec 30 '21 at 19:03
  • The problem is that Log4j 2.x does not support Log4j 1.x appenders. Therefore you need to rewrite it for Log4j 2.x or describe precisely what should it do (chances are, the Log4j 2.x `RollingFile` can emulate the same behavior). – Piotr P. Karwasz Dec 30 '21 at 19:23
  • https://logging.apache.org/log4j/2.x/manual/migration.html From looking here I can see that the RollingFileAppender is under the "Supported Components". On "Configuration Compatibility" part. – Or123 Dec 30 '21 at 19:43
  • Yes, the `RollingFileAppender` can be converter automatically (see [this question](https://stackoverflow.com/q/70386274/11748454)), but classes derived from it can't. – Piotr P. Karwasz Dec 31 '21 at 05:55
  • thanks. I did not see where it is documented that classes derived from it can't be automatically converted. Is there a way (easy one) to make it work with the derived class? If not, can you please assume the standard RollingFileAppender is being used (instead the FileNamePatternRollingFileAppender) and let me know how the properties file will look like with log4j2? (giving the above configuration in log4j1) thanks. – Or123 Dec 31 '21 at 21:02
  • Regardless to your answer if it is possible or not to use the derived class, I am adding here the output of the log4j1 file after I used the converter tool you recommended in the other post you mentioned. Can you confirm that the output is correct based on the above input? suffix and simpleDateFormatpattern offcourse are missing because these are custom members of the derived class. I will add the code by editing my original question. – Or123 Dec 31 '21 at 22:50
  • The configuration converter and configuration factory included in `log4j-1.2-api` do not analyze the bytecode of your customized appenders (so it can not know you extended a standard appender). It just converts the configuration of those appenders it knows (cf. [source code](https://github.com/apache/logging-log4j2/blob/84ba3f260153d4674bf8c1db9cb491bd82dcd41f/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java#L163)). – Piotr P. Karwasz Jan 01 '22 at 14:51
  • thanks, so I understand we can not use the derived class in log4j2. Can you please confirm the log4j2.xml that I have added with the original class of the appender, if that looks correct? thanks. I used the converter tool you recommended in the other thread. – Or123 Jan 03 '22 at 16:00
  • Yes, custom Log4j 1.x components can not be used directly in Log4j 2.x. Your configuration looks Ok, you should post it as answer. – Piotr P. Karwasz Jan 03 '22 at 18:01

0 Answers0