詳細可以參考這裡(原文):http://www.journaldev.com/7128/apache-log4j-2-tutorial-configuration-levels-appenders-lookup-layouts-and-filters-example


程式碼:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;

public class Main {

    public static void main(String[] args) throws IOException {
        ConfigurationFactory factory = XmlConfigurationFactory.getInstance();
        ConfigurationSource configurationSource = new ConfigurationSource(new FileInputStream(new File("C:/log4j2.xml")));
        Configuration configuration = factory.getConfiguration(configurationSource);
        // 輸出到主控台
        ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());

        configuration.addAppender(appender);
        LoggerConfig loggerConfig = new LoggerConfig("com", Level.FATAL, false);
        loggerConfig.addAppender(appender, null, null);
        configuration.addLogger("com", loggerConfig);

        LoggerContext context = new LoggerContext("JournalDevLoggerContext");
        context.start(configuration);

        Logger logger = context.getLogger(Main.class.getName());
        // Logger logger = context.getLogger("自訂 Log 名稱");
        logger.log(Level.FATAL, logger.getName() + " : 嚴重訊息");
        logger.log(Level.ERROR, logger.getName() + " : 錯誤訊息");
        logger.getParent().log(Level.ERROR, "錯誤訊息 2");
    }

}

然後在 C:/ 建立 log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="ERROR">
            <AppenderRef ref="Console"/>
        </Root>
        <logger name="com" level="TRACE">
            <AppenderRef ref="Console"/>
        </logger>
        <logger name="com.journaldev" level="TRACE">
            <AppenderRef ref="Console"/>
        </logger>
        <logger name="net" level="ERROR">
            <AppenderRef ref="Console"/>
        </logger>
        <logger name="net.journaldev" level="ERROR">
            <AppenderRef ref="Console"/>
        </logger>
    </Loggers>
</Configuration>

輸出:

2015-07-15 13:03:32.308 [main] FATAL test.Main - test.Main : 嚴重訊息
2015-07-15 13:03:32.308 [main] ERROR test.Main - test.Main : 錯誤訊息
2015-07-15 13:03:32.308 [main] ERROR - 錯誤訊息 2







創作者介紹
創作者 黃彥霖 的頭像
黃彥霖

彥霖 實驗筆記

黃彥霖 發表在 痞客邦 留言(0) 人氣()