1
votes

i am aritinga small application in Linux based system where i am using slf4j libraries for logger

import org.slf4j.Logger; import org.slf4j.LoggerFactory;

i could see the logs are visible in console but not appending to log files.

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="INFO" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{ABSOLUTE}][%-5p][%-10t]%m%n" />
    </layout>
</appender>






<!-- EXAMPLE logging setup NOTES: 1. files are written to "./logs/<filename>". 
    You must setup the environment so that ./logs is a symlink to the correct 
    location according to the EXAMPLE log standard. For example, "/opt/logs/". 
    If that's not possible, change the File setting in each appender appropriately. -->

<appender name="EXAMPLE_AUDIT" class="org.apache.log4j.RollingFileAppender">
    <param name="threshold" value="INFO" />
    <param name="File" value="./logs/audit.log" />
    <param name="MaxFileSize" value="128MB" />
    <param name="MaxBackupIndex" value="20" />
    <layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
        <param name="ConversionPattern" value="EXAMPLE_AUDIT" />
    </layout>
</appender>

<appender name="EXAMPLE_METRIC" class="org.apache.log4j.RollingFileAppender">
    <param name="threshold" value="DEBUG" />
    <param name="File" value="./logs/metric.log" />
    <param name="MaxFileSize" value="128MB" />
    <param name="MaxBackupIndex" value="10" />
    <layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
        <param name="ConversionPattern" value="EXAMPLE_METRIC" />
    </layout>
</appender>

<appender name="EXAMPLE_ERROR" class="org.apache.log4j.RollingFileAppender">
    <param name="threshold" value="ERROR" />    <!-- only WARN and ERROR are allowed in this log -->
    <param name="File" value="./logs/error.log" />
    <param name="MaxFileSize" value="128MB" />
    <param name="MaxBackupIndex" value="10" />
    <layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
        <param name="ConversionPattern" value="EXAMPLE_ERROR" />
    </layout>
</appender>

<appender name="EXAMPLE_DEBUG" class="org.apache.log4j.RollingFileAppender">
    <param name="threshold" value="DEBUG" />
    <param name="File" value="./logs/debug.log" />
    <param name="MaxFileSize" value="128MB" />
    <param name="MaxBackupIndex" value="20" />
    <layout class="com.rsg.ova.logging.log4j.EXAMPLELayout">
        <param name="ConversionPattern" value="EXAMPLE_DEBUG" />
    </layout>
</appender>


<logger name="com.rsg.EXAMPLE.audit" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_AUDIT" />
</logger>

<logger name="com.rsg.EXAMPLE.metrics" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_METRIC" />
</logger>

<logger name="com.rsg.EXAMPLE.error" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_ERROR" />
</logger>

<logger name="com.rsg.EXAMPLE.debug" additivity="false">
    <level value="info" />
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="EXAMPLE_DEBUG" />
</logger>


<root>
    <level value="DEBUG" />
    <appender-ref ref="EXAMPLE_DEBUG" />
    <appender-ref ref="CONSOLE" />

</root>

<logger name="org.openEXAMPLE.agcp.commonFunction.input"
    additivity="false">
    <level value="INFO" />
    <appender-ref ref="CONSOLE" />
</logger>

<logger name="org.openEXAMPLE.agcp.commonFunction.output"
    additivity="false">
    <level value="INFO" />
    <appender-ref ref="CONSOLE" />

</logger>

<logger name="org.openEXAMPLE.agcp.commonFunction.error"
    additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="EXAMPLE_ERROR" />
</logger>

<!-- The EXAMPLE logging standard has four specific classes of logging that 
    are unrelated to subsystem logger names. If you want them activated, uncomment 
    this block. -->
<logger name="com.rsg.EXAMPLE.audit" additivity="false">
    <level value="info" />
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="EXAMPLE_AUDIT" />
</logger>

<logger name="com.rsg.EXAMPLE.metrics" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_METRIC" />
</logger>

<logger name="com.rsg.EXAMPLE.error" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_ERROR" />
</logger>

<logger name="com.rsg.EXAMPLE.debug" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_DEBUG" />
</logger>


<logger name="com.rsg.ova.apiClient.http.HttpClient" additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_ERROR" />
    <appender-ref ref="CONSOLE" />
</logger>

<logger name="com.rsg.ova.roman.client.impl.romanSimplerBatchPublisher"
    additivity="false">
    <level value="info" />
    <appender-ref ref="EXAMPLE_ERROR" />
    <appender-ref ref="CONSOLE" />
</logger>

4

4 Answers

0
votes

I have checked your logback.xml file configuration. It seems that the root tag("configuration") is missing. Can you add and check it?.

Refer https://logback.qos.ch/manual/configuration.html

0
votes

It seems you missed root tag in your xml file. Please find the below code to write the logs in a file.

<appender name="file" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="false" />
    <param name="maxFileSize" value="128MB" />
    <param name="maxBackupIndex" value="20" />
    <param name="file" value="./logs/audit.log" />
    <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>
    <level value="INFO" />
    <appender-ref ref="file" />
</root>
0
votes

The configuration shown in the question is in the log4j 1.2 format.

The format for log4j2.xml is different. It is plugin based so you don't need to specify the class name of the appenders any more.

Please see the Log4j2 manual. It has many examples configurations.

0
votes

it has been identified that i have to include sl4j-log4j bridge jar to resolve above issue, i was including log4j and sl4j individual jars , due to this i can see no compilation errors but the bridge is not fulfilling.

after removing individual jars and adding bridge jar the issue has been solved.