0

I have a web app with jee6 and log4j 2. I want to have a custom log for my Batch process, I set a custom level to fill with the logs of the Batch.

But I can't log nothing, on the startup of the Server I can create the log file server.log but is empty, and the error_batch.log has all the logs: .info and my custom level, I have not errors in the server, what is the correct config for my case??

private static final Logger logger = LogManager.getLogger(test.class);

-

logger.info("Starting on the Server");
logger.log(Level.forName("ERROR_BATCH", 450), "Example Error in Batch");

-

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="WARN">

    <CustomLevels>
        <CustomLevel name="ERROR_BATCH" intLevel="450" />
    </CustomLevels>

    <Appenders>

        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <RollingFile name="logBatch" fileName="C:\\Workarea\\Error\\error_batch.log"
                     filePattern="C:\\Workarea\\Error\\error_batch-%d{dd-MM-yyyy}-%i.log"
                     append="true">
            <PatternLayout>
                <Pattern>%d %p %c [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>

        <RollingFile name="logServer" fileName="C:\\Workarea\\Error\\server.log"
                     filePattern="C:\\Workarea\\Error\\server-%d{dd-MM-yyyy}-%i.log"
                     append="true">
            <PatternLayout>
                <Pattern>%d %p %c [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>

    </Appenders>

    <Loggers>
        <Root level="error">
            <AppenderRef ref="Console" />
            <AppenderRef ref="logServer" />
        </Root>
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="logServer" />
        </Root>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="logServer" />
        </Root>
        <Root level="trace">
            <AppenderRef ref="logBatch" level="ERROR_BATCH" />
        </Root>
    </Loggers>

</Configuration>
reizintolo
  • 429
  • 2
  • 7
  • 20

2 Answers2

1

This looks to be a duplicate of How to log in different file? log4j2, which in turn is a duplicate of a previously asked question.

Your basic problem is that you can only configure the root logger once, not 4 times as you are doing. I'm guessing the one with level="error" is "winning", but I'm not really sure.

As for how to get events to specific log files please refer to the answer given to you in the question you asked previously.

Community
  • 1
  • 1
rgoers
  • 8,696
  • 1
  • 22
  • 24
0
logger.log(Level.forName("ERROR_BATCH", 450), "Example Error in Batch");

and

<CustomLevels>
    <CustomLevel name="ERROR_BATCH" intLevel="450" />
</CustomLevels>

are duplicates. You define your custom level twice and the second (in the code) must override the first (in config file).

Remove the one in your code. It should work.

terrasson marc
  • 97
  • 1
  • 10