Home > Blockchain >  Log4j2 does not write logs to console or file (properties)
Log4j2 does not write logs to console or file (properties)

Time:07-31

i tried to add log4j2 to my project, and i want it to write logs to the file and to the console, but no one works...

My log4j2.properties file

appenders = myfileappender,console
appender.myfileappender.type = file
appender.myfileappender.name = myfileappender
appender.myfileappender.fileName = logs/app.log
appender.myfileappender.layout.type = PatternLayout
appender.myfileappender.layout.pattern = %C{1.} {%-5level} %d{HH:mm:ss} - %msg%n
appender.console.type = console
appender.console.name = console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %C{1.} {%-5level} %d{HH:mm:ss} - %msg%n
rootLogger.appenderRefs = file,console
rootLogger.appenderRefs.file.ref = myfileappender
rootLogger.appenderRefs.console.ref = console
rootLogger.level = all

and my main class:

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

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

    public static void main(String[] args) {
        logger.info("Info msg");
        logger.warn("Warn msg");
        logger.debug("Debug msg");
        logger.error("Err msg");
        logger.fatal("Fat msg");
    }
}

my properties file is in src/main/resources, main class is in src/main/java. I want log4j to write log files to {my-project-root}/logs/app.log

CodePudding user response:

Use logback. Put this logback.xml file in your resources

<configuration>
  <appender name="FILE" >
    <file>logs/output.log</file>
    <append>false</append>
    <immediateFlush>true</immediateFlush>
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>
        
  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>

And add this line to your application.properties file

logging.config=example/path/logback.xml

CodePudding user response:

I finally resolved that problem, but not with log4j2. I used logback instead. Logback.xml file:

    <appender name="FILE" >
        <file>logs/output.log</file>
        <append>false</append>
        <immediateFlush>true</immediateFlush>
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="STDOUT" >
        <append>false</append>
        <immediateFlush>true</immediateFlush>
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

and added this code:

private static final Logger logger;

    static {
        try {
            logger = logbackConfigure();
        } catch (JoranException e) {
            throw new RuntimeException(e);
        }
    }

    public static Logger logbackConfigure() throws JoranException {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.reset();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        configurator.doConfigure("src/main/resources/logback.xml");

        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        root.setLevel(Level.INFO);
        return root;
    }

istead of:

private static final Logger logger = LogManager.getLogger();
  • Related