Search code examples
javalogginglog4j

migrating log4j.xml to log4j2.xml jdk8


Hello i am migrating to latest log4j version and migrated from 1.x to 2.x log4j. So here is my log4j2 config file. I am stuck with debug and error logs, maybe someone could see that if there should be something different.

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

<Configuration status="debug" packages="">
    <Appenders>
        <!-- System.out log -->
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
        </Console>
        <!-- debug log -->
        <RollingFile name="debug" fileName="/var/logs/debug.log"
                     filePattern="/var/logs/%d{ddMMyyyy}_debug.log.gz">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <LevelRangeFilter minLevel="debug" maxLevel="info" onMatch="accept"/>
        </RollingFile>
        <!-- error log -->
        <RollingFile name="error" fileName="/var/logs/error.log"
                     filePattern="/var/logs/%d{ddMMyyyy}_error.log.gz">>
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <LevelRangeFilter minLevel="warn" maxLevel="fatal" onMatch="accept"/>
        </RollingFile>
        <!-- info log -->
        <RollingFile name="info" fileName="/var/logs/info.log"
                     filePattern="/var/logs/%d{ddMMyyyy}_info.log.gz">>
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <LevelRangeFilter minLevel="info" maxLevel="info" onMatch="accept"/>
        </RollingFile>

        <!-- Special daily time log. Logib kui palju aega iga Request vytab -->
        <RollingFile name="timelog" fileName="/var/logs/input_timing.log"
                     filePattern="'.'yyyy-MM-dd'.log'">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
        <!-- accesslog log appender -->
        <RollingFile name="accesslog" fileName="/var/logs/access_log.log"
                     filePattern="'_'yy_ww'.txt'">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
        <RollingFile name="todolog" fileName="/var/logs/todo.log"
                     filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
        <!-- Special redirections servlet log. Logib online_report servlet'i teated -->
        <RollingFile name="redirectlog" fileName="/var/logs/redirect.log"
                     filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>
        </RollingFile>
        <!-- Asynchronous appender. Writes the log, when it has the time. -->
        <Async name="asyncRoot" bufferSize="10" includeLocation="true">
            <AppenderRef ref="info"/>
            <AppenderRef ref="error" level="error" />
            <AppenderRef ref="debug" level="debug" />
        </Async>
    </Appenders>
    <Loggers>


        <!-- additivity false - do not log to other appenders as defined here -->
        <Logger name="TIMING" additivity="false" level="info">
            <AppenderRef ref="timelog"/>
        </Logger>
        <!-- additivity false - do not log to other appenders as defined here -->
        <Logger name="TODO" additivity="false" level="info">
            <AppenderRef ref="todolog"/>
        </Logger>
        <!-- additivity false - do not log to other appenders as defined here -->
        <Logger name="ACCESS" additivity="false" level="info">
            <AppenderRef ref="accesslog"/>
        </Logger>
        <!-- additivity false - do not log to other appenders as defined here -->
        <Logger name="REDIRECT" additivity="false" level="info">
            <AppenderRef ref="redirectlog"/>
        </Logger>

        <!-- root logger -->
        <Root level="debug">
            <AppenderRef ref="asyncRoot"/>
        </Root>

    </Loggers>
</Configuration>

public class EffencyTests {

  private static final Logger log = LogManager.getLogger(EffencyTests.class);

  @Before
  public void cos() {
    log.info("hey");
    log.debug("czesc");
    log.error("smth");
  }

  @Test
  public void cosata() {
    log.info("hey");
    log.info("hey");
    log.info("hey");
    log.info("hey");
    log.info("hey");
    log.info("hey");
    log.debug("hey");
  }

  @Test
  public void cosatae() {
    int numberOne = 1;
    int numberTwo = 2;
    log.info("test");
    try {
      if(numberOne != numberTwo) {
        throw new IllegalArgumentException();
      }

    }catch (Exception e){
    log.error("error");
    }
  }

here is some output on info log :

enter image description here

error.log and debug.log are empty, i am really stuck for a long time can anyone help me with that ? I am struggling for over month and i feel like there is single letter in config is missing. Looking for any ideas and help thanks


Solution

  • if anyone would struggle it helped at my case :

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="debug">
        <Appenders>
            <!-- System.out log -->
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
            </Console>
    
            <!-- error log -->
            <RollingFile name="error" fileName="./logs/errors.log"
                         filePattern="./logs/${date:yyyy-MM}/errors-%d{yyyy-MM-dd}-%i.log.gz"
                         filePermissions="rw-rw-r--">
                <PatternLayout>
                    <Pattern>%d{ISO8601} [%t] %-5p %c{1} - %m%n</Pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="10 MB"/>
                </Policies>
            </RollingFile>
            <!-- info log -->
            <RollingFile name="info" fileName="./logs/info.log"
                         filePattern="./logs/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
                         filePermissions="rw-rw-r--">
            <PatternLayout>
                    <Pattern>%d{ISO8601} [%t] %-5p %c{1} - %m%n</Pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="10 MB"/>
                </Policies>
                <LevelRangeFilter minLevel="info" maxLevel="info" onMatch="accept"/>
            </RollingFile>
    
            <!-- debug log -->
            <RollingFile name="debug" fileName="./logs/debug.log"
                         filePattern="./logs/${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
                         filePermissions="rw-rw-r--">
            <PatternLayout>
                    <Pattern>%d{ISO8601} [%t] %-5p %c{1} - %m%n</Pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="10 MB"/>
                </Policies>
                <LevelRangeFilter minLevel="debug" maxLevel="debug" onMatch="accept"/>
            </RollingFile>
    
    
            <!-- Special daily time log. Logib kui palju aega iga Request vytab -->
            <RollingFile name="timelog" fileName="./logs/input_timing.log"
                         filePattern="'.'yyyy-MM-dd'.log'"
                         filePermissions="rw-rw-r--">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
                <Policies>
                    <SizeBasedTriggeringPolicy size="20 MB"/>
                </Policies>
            </RollingFile>
            <!-- accesslog log appender -->
            <RollingFile name="accesslog" fileName="./logs/access_log.log"
                         filePattern="'_'yy_ww'.txt'"
                         filePermissions="rw-rw-r--">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
                <Policies>
                    <SizeBasedTriggeringPolicy size="20 MB"/>
                </Policies>
            </RollingFile>
            <!-- Special todo jobs log. Logib kõik genereeritud todo sõnumid -->
            <RollingFile name="todolog" fileName="./logs/todo.log"
                         filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false"
                         filePermissions="rw-rw-r--">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
                <Policies>
                    <SizeBasedTriggeringPolicy size="20 MB"/>
                </Policies>
            </RollingFile>
            <!-- Special redirections servlet log. Logib online_report servlet'i teated -->
            <RollingFile name="redirectlog" fileName="./logs/redirect.log"
                         filePattern="'.'yyyy-MM-dd'.log'" ignoreExceptions="false"
                         filePermissions="rw-rw-r--">
            <PatternLayout pattern="%d{ISO8601} [%t] %-5p %c{1} - %m%n"/>
                <Policies>
                    <SizeBasedTriggeringPolicy size="20 MB"/>
                </Policies>
            </RollingFile>
    
            <!-- Asynchronous appender. Writes the log, when it has the time. -->
            <Async name="asyncRoot" bufferSize="10" includeLocation="true">
                <AppenderRef ref="info"/>
                <AppenderRef ref="error" level="error"/>
            </Async>
        </Appenders>
        <Loggers>
            <!-- additivity false - do not log to other appenders as defined here -->
            <Logger name="TIMING" additivity="false" level="info">
                <AppenderRef ref="timelog"/>
            </Logger>
            <!-- additivity false - do not log to other appenders as defined here -->
            <Logger name="TODO" additivity="false" level="info">
                <AppenderRef ref="todolog"/>
            </Logger>
            <!-- additivity false - do not log to other appenders as defined here -->
            <Logger name="ACCESS" additivity="false" level="info">
                <AppenderRef ref="accesslog"/>
            </Logger>
            <!-- additivity false - do not log to other appenders as defined here -->
            <Logger name="REDIRECT" additivity="false" level="info">
                <AppenderRef ref="redirectlog"/>
            </Logger>
    
            <!-- Level order (low to high): TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
            <Root level="DEBUG" includeLocation="true">
                <!--AppenderRef Medium ref Values must be defined earlier appenders-->
                <AppenderRef ref="asyncRoot"/>
    <!--            <AppenderRef ref="debug" level="debug"/>-->
            </Root>
        </Loggers>
    </Configuration>