0

I am trying to set the log file name dynamically in my Maven project.

I tried many times following the blog posts but the log file is not generated.

After several attempts and debugging, I found that when I set the log file name using System.setProperty before the database call, then the log file is created.

 System.setProperty("logFileName", "E:\\FICO\\logs\\calculate");

But I want to set the filename with the value I get from the DB.

Ex:

String ccmValue = "E:\\FICO\\logs\\calculate_" + getCurrentCCMValue(); // DB Call to get the value
System.setProperty("logfilename", ccmValue ); // E:\\FICO\\logs\\calculate_ccmValue.log

If I set the System.setProperty after the DB call, then the file is not getting created.

Below is my log4j.xml file:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
    xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="console"
        class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="fileAppender"
        class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="${logFileName}.log" />
        <param name="maxFileSize" value="1MB" />
        <param name="maxBackupIndex" value="5" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <root>
        <priority value="info"></priority>
        <appender-ref ref="console"></appender-ref>
        <appender-ref ref="fileAppender"></appender-ref>
    </root>

</log4j:configuration>

Here is the console log:

log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [info].
log4j: root level set to INFO
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Setting property [target] to [System.out].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [file] to [.log].
log4j: Setting property [maxFileSize] to [1MB].
log4j: Setting property [maxBackupIndex] to [5].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: .log, true
log4j: setFile ended
log4j: Adding appender named [fileAppender] to category [root].
2019-07-10 22:58:16 INFO  Version:46 - HHH000412: Hibernate Core {5.4.3.Final}
2019-07-10 22:58:17 INFO  Version:49 - HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-07-10 22:58:18 INFO  ConnectionProviderInitiator:137 - HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
2019-07-10 22:58:18 INFO  C3P0ConnectionProvider:116 - HHH010002: C3P0 using driver: com.mysql.cj.jdbc.Driver at URL: jdbc:mysql://localhost:3306/gentexdba
2019-07-10 22:58:18 INFO  C3P0ConnectionProvider:117 - HHH10001001: Connection properties: {user=root, password=****}
2019-07-10 22:58:18 INFO  C3P0ConnectionProvider:120 - HHH10001003: Autocommit mode: false
2019-07-10 22:58:18 INFO  MLog:126 - MLog clients using log4j logging.

2019-07-10 22:58:18 INFO MLog:126 - MLog clients using log4j logging.

2019-07-10 22:58:18 INFO  C3P0Registry:248 - Initializing c3p0-0.9.5.3 [built 27-January-2019 00:11:37 -0800; debug? true; trace: 10]
2019-07-10 22:58:18 INFO  C3P0ConnectionProvider:200 - HHH10001007: JDBC isolation level: <unknown>
Abhilash
  • 43
  • 1
  • 8
  • duplicate https://stackoverflow.com/questions/1324053/configure-log4j-to-log-to-custom-file-at-runtime – Qingfei Yuan Jul 10 '19 at 17:31
  • @QingfeiYuan, are you referring to the accepted answer? – Abhilash Jul 10 '19 at 17:38
  • I mean it is impossible. As you need offer the log file name before application start. You need log by yourself if you really want to achieve such kind of flexibility. You cannot count on logging itself before itself logging configuration finish . One possible way is multiple threads/ processes. – Qingfei Yuan Jul 10 '19 at 19:05

0 Answers0