Home > Enterprise >  When using log4j, program creates mutliple files but fills only one, when should fill all
When using log4j, program creates mutliple files but fills only one, when should fill all

Time:12-22

At college we are now making a project, and as a part of it, we are trying to add a part that will use log4j to output all errors and exceptions to external files.

We want to have one file for errors, one for warnings and one for everything at once, it will also include messages that everything is correct. For thorough testing, we added some errors that SHOULD happen. Also we want to record all exceptions as warnings, and output them to separate file.

At the current stage, there is a problem: the program creates four files, desired three and also one that it adds on its own, which also includes debug messages. However, for some reason, the information about warnings and errors goes ONLY to INFO log, while it's also supposed to go to their own log files.

Logback file:

<appender name="FILE" >
    <file>${log.name}</file>
    <append>true</append>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
</appender>

<appender name="ERRORS-FILE" >
    <file>${log.name}.errors</file>
    <append>true</append>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{36} - %msg %n</pattern>
    </encoder>
</appender>

<appender name="WARNINGS-FILE" >
    <file>${log.name}.warnings</file>
    <append>true</append>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{36} - %msg %n</pattern>
    </encoder>
</appender>

    <appender name="INFO-FILE" >
    <file>${log.name}.info</file>
    <append>true</append>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{36} - %msg %n</pattern>
    </encoder>
</appender>

<!-- additivity=false ensures analytics data only goes to the analytics log -->

<logger name="errors" level="ERROR" additivity="true">
    <appender-ref ref="ERRORS-FILE"/>
</logger>

<logger name="warnings" level="WARN" additivity="true">
    <appender-ref ref="WARNINGS-FILE"/>
</logger>

<logger name="info" level="INFO" additivity="true">
    <appender-ref ref="INFO-FILE"/>
</logger>

<root>
    <appender-ref ref="FILE"/>
</root>

LoggerManager class I created to use it for outputting logs from other classes:

package com.evgenie_tomer_itay.CouponSystemSpringBoot.utilities;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

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

public class LoggerManager
{
    private final static Logger loggerErrors = LoggerFactory.getLogger("errors");
    private final static Logger loggerWarnings = LoggerFactory.getLogger("warnings");
    private final static Logger loggerInfo = LoggerFactory.getLogger("info");
  
    private static String getCurrentDateAsString()
    {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = new Date(System.currentTimeMillis());
            
        return formatter.format(date);
    }
    
    public static void addLogInfo(String log)
    {
        LoggerManager.loggerInfo.info(log);
    }
    
    public static void addLogWarnings(String log)
    {
        LoggerManager.loggerWarnings.info(log);
        LoggerManager.loggerInfo.info(log);
    }
    
    public static void addLogErrors(String log)
    {
        LoggerManager.loggerErrors.info(log);
        LoggerManager.loggerInfo.info(log);
    }
}

Dependencies:

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
    </dependency>

This part creates files at start of main:

public static void createLogFiles()
{
     String desktopPath = System.getProperty("user.home")   File.separator   "Desktop";
     String fileName = desktopPath "\\CouponSystemSpringBootLog";
     System.setProperty("log.name",fileName);
}

Example of how I create a log from any class:

LoggerManager.addLogErrors("ERROR");

Now, again, "addLogInfo" works, but not "addLogError" or "addLogWarning".

Please help understand what are doing wrong.

Thank you in advance!

CodePudding user response:

public static void addLogInfo(String log) {
        myLogger.info(log);
    }
    
    public static void addLogErrors(String log)
    {
        myLogger.info(log);
    }

You have something like these methods, to differentiate between info and error logs, but the name of the method doesn't have an impact on that.

You'll need to call the correct method of the logger:

public static void addLogInfo(String log) {
        myLogger.info(log);
    }

public static void addLogErrors(String log) {
    myLogger.error(log);
}
  • Related