這是目前找到較完整的網站
pom.xml
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
※語法
App.java
public class App { static Logger logger = Logger.getLogger("1234"); public static void main(String[] args) { String p = "c- %c %n" + "C- %C %n" + "d- %d %n" + "F- %F %n" + "l- %l %n" + "L- %L %n" + "m- %m %n" + "M- %M %n" + "p- %p %n" + "r- %r %n" + "t- %t %n" + "x- %x %n" + "X- %X %n" + "%%- %% %n" + "[%5c] %n" + "[%3c] %n" + "[%-5c] %n" + "[%-3c] %n" + "[%.3c] %n" + "[%5.3c] %n" + "[%5.4c] %n" + "[%-5.3c] %n" + "[%-5.4c] \n%n"; PatternLayout pl = new PatternLayout(p); ConsoleAppender ca = new ConsoleAppender(pl); logger.addAppender(ca); logger.setLevel(Level.FATAL); // ALL(6) < TRACE(6) < DEBUG(5) < INFO(4) < WARN(3) < ERROR(2) < FATAL(1) < OFF(0) logger.trace("TRACE(6)"); logger.debug("DEBUG(5)"); logger.info("INFO(4)"); logger.warn("WARN(3)"); logger.error("ERROR(2)"); logger.fatal("FATAL(1)"); } }
※結果:
c- 1234
C- SpringTest.log4j.App
d- 2016-02-02 15:45:44,219
F- App.java
l- SpringTest.log4j.App.main(App.java:53)
L- 53
m- FATAL(1)
M- main
p- FATAL
r- 0
t- main
x-
X- {}
%- %
[ 1234]
[1234]
[1234 ]
[1234]
[234]
[234]
[ 1234]
[234]
[1234 ]
※setLevel下面註解的圓括號的數字的意思是說,假設我設定WARN,那WARN層級以下(包括自己)會顯示,如最下面的六行,trace、debug、info雖然有寫,但不會顯示,如果setLevel不寫就等同OFF,不會顯示
※二個「%」等於一個%,如果少打會出類似「log4j:ERROR Unexpected char [ ] at position 107 in conversion patterrn.」的錯
※設定檔
要放在src下,但如果是maven,就要放在src/main/java下,有兩個檔案,二擇一即可log4j.properties
# Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n" /> </layout> </appender> <root> <priority value="debug" /> <appender-ref ref="console" /> </root> </log4j:configuration>
App.java
// PropertyConfigurator.configure("src/main/java/log4j.properties"); logger.trace("TRACE(6)"); logger.debug("DEBUG(5)"); logger.info("INFO(4)"); logger.warn("WARN(3)"); logger.error("ERROR(2)"); logger.fatal("FATAL(1)");
※如果設定檔不想放在預設地點,或者不想叫log4j,可以打開註解,但xml我試過沒用,還沒找到辦法
※輸出到指定地點
FileAppender fa = null; try { fa = new FileAppender(new SimpleLayout(), "log4j1.txt", false); } catch (IOException e) { e.printStackTrace(); } logger.addAppender(fa); logger.setLevel(Level.FATAL); logger.trace("TRACE(6)"); logger.debug("DEBUG(5)"); logger.info("INFO(4)"); logger.warn("WARN(3)"); logger.error("ERROR(2)"); logger.fatal("FATAL(1)");
※log4j1會放在workspace\專案名稱下,或者直接打D:也是可以的,最後的boolean是說,如果已經有檔案存在,要不要覆寫
沒有留言:
張貼留言