Log4j未登录到文件或控制台

时间:2017-07-14 20:25:30

标签: java log4j2

我正在尝试学习log4j版本2.x. log4j属性使用xml完成​​。下面给出的代码是ecl​​ipse STS中的maven项目。它不会将任何日志记录消息打印到控制台或文件中。

我不知道如何调试它。谷歌搜索没有给我任何关于日食的答案。请建议我如何自己调试并修复它。除非我的大部分/全部代码都是错误的,否则我不需要马上完全回答。

代码:

package com.api.log4j;

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

public class LoggingExample {

    static Logger logger = LogManager.getLogger(LoggingExample.class);

    private void loggerLevel(String message) {
        if (logger.isDebugEnabled()) {
            logger.debug("This is set to debug: " + message);
        }

        if (logger.isInfoEnabled()) {
            logger.info("This is set to info: " + message);
        }

        logger.warn("This is set to warn: " + message);
        logger.error("This is set to error: " + message);
        logger.fatal("This is set to fatal: " + message);
    }

    public static void main(String[] args) {
        System.out.println("Running main...");
        LoggingExample loggingExample = new LoggingExample();
        loggingExample.loggerLevel("calling the loggerLevel method...");
    }

}

Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="System.out">
            <PatternLayout>
                [%-5level]<!-- Use 5 chars to show the level text. If level text < 5 
                    chars, then append spaces to make it 5 chars. -->
                %d{yyyy-MM-dd HH:mm:ss.SSS}<!-- The date. -->
                [%t]<!-- Which thread is running. -->
                %c{1}<!-- Class being logged. -->
                - %msg%n
            </PatternLayout>
        </Console>
        <File name="File" filename="/src/main/resources/log4j-file.txt">
            <PatternLayout>
                [%-5level]<!-- Use 5 chars to show the level text. If level text < 5 
                    chars, then append spaces to make it 5 chars. -->
                %d{yyyy-MM-dd HH:mm:ss.SSS}<!-- The date. -->
                [%t]<!-- Which thread is running. -->
                %c{1}<!-- Class being logged. -->
                - %msg%n
            </PatternLayout>
        </File>
    </Appenders>

    <Loggers>
        <Root>
            <AppenderRef ref="Console" /> <!-- Appends only to console appender -->
        </Root>
        <Logger name="com.api.log4j.LoggingExample" level="debug"
            additivity="false">
            <AppenderRef ref="File" /> <!-- Appends only to file appender -->
        </Logger>
    </Loggers>
</Configuration>

1 个答案:

答案 0 :(得分:0)

1.如果您使用MAVEN构建工具,请确保将log4j.jar添加到您的lib文件夹中。 2.在log4j.properties文件中写入配置参数,你的日志就可以了!!

    # Root logger option
    log4j.rootLogger=DEBUG,stdout, file
    # Redirect log messages to console
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p 
    %c{1}:%L - %m%n

   # Redirect log messages to a log file
   log4j.appender.file=org.apache.log4j.RollingFileAppender
   log4j.appender.file.File=your File path
   log4j.appender.file.MaxFileSize=5MB
   log4j.appender.file.MaxBackupIndex=10
   log4j.appender.file.layout=org.apache.log4j.PatternLayout