Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different level of logs in different log files

Tags:

log4j2

How can we write a simple log4j2.xml file with different levels of logs going into different files?

For example we have error logs any info logs I need to push all error log messages into one log file and all info log messages into another file.

I want that info message in InfoController.log file and error message in LoginController.log file

How can i do it?

like image 453
Mohammad Gouse Avatar asked Feb 26 '15 10:02

Mohammad Gouse


2 Answers

Finally got the answer by doing this i got the logs in different files.

<Loggers>
    <logger name="com.mvc.login" level="info" additivity="false">
        <AppenderRef ref="LoginController" level="error"/>
        <AppenderRef ref="InfoController" level="info"/>
    </logger>
  </Loggers>
like image 137
Mohammad Gouse Avatar answered Dec 13 '22 21:12

Mohammad Gouse


Loggers must have unique names. The second logger in your config will replace the first one, which explains why you don't see any output for the appender referenced by the first logger.

Normally, a configuration has a root logger that will receive all events. I would recommend that, so that part of the config becomes:

<Loggers>
    <logger name="com.mvc.login" level="error" additivity="false">
        <AppenderRef ref="LoginController"/>
    </logger>
   <root level="trace">
        <AppenderRef ref="InfoController"/>
    </root>
</Loggers>
like image 26
Remko Popma Avatar answered Dec 13 '22 22:12

Remko Popma