0

Please instead of downvoting have a look at my problem and help me out of this.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class NewClass {
    private static final Logger logger = LogManager.getLogger(NewClass.class);

    public static void main(final String... args) {

        // Set up a simple configuration that logs on the console.
        logger.info("in info");
        logger.trace("Entering application.");
        Hello bar = new Hello();
        if (!bar.doIt()) {
             logger.error("Didn't do it.");
        }
        logger.trace("Exiting application.");
    }
}


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Hello {
    static final Logger logger = LogManager.getLogger(Hello.class.getName());

    public boolean doIt() {
        logger.entry();
        logger.error("Did it again!");
        return logger.exit(false);
    }
}

Configuration

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
    <Root level="info">
      <AppenderRef ref="Console"/>
    </Root>
    <Root level="trace">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

As in here I am able to print everything on the console but I'm not sure how to append the records in three different files in a folder for error, info and trace. I'm not sure how to code for it. Thank you for the help.

rgoers
  • 8,696
  • 1
  • 22
  • 24
Nutty Noru
  • 51
  • 1
  • 3
  • Possible duplicate of [How to configure log4j to log different log levels to different files for the same logger](http://stackoverflow.com/questions/1839647/how-to-configure-log4j-to-log-different-log-levels-to-different-files-for-the-sa) – Jens Jun 16 '16 at 13:02

1 Answers1

0

I have a full example here for a bit different case. You case is even easier: you should define a dedicated file appender for each desired log. Each appender should have a different name and threshold filter to filter relevant messages. For example, for error messages:

<RollingFile name="ErrorsLog" fileName="${errLogDir}/${errLogFile}.log"
        filePattern="${errLogDir}/${errLogFile}-%d{dd-MM-yyyy}.log">
        <Filters>
            <ThresholdFilter level="error"/>
        </Filters>
        <PatternLayout> 
            <pattern>
                %d{EEE, dd MMM yyyy HH':'mm':'ss } %m%n
            </pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy/>
        </Policies>
        <DefaultRolloverStrategy max="30"/>
</RollingFile>

etc.

asch
  • 1,923
  • 10
  • 21