可用的 log4j 配置, log4j.properties
log = /Users/grs/Documents/log4j log4j.rootLogger = DEBUG, FILEROLING, FILE, CONSOLE log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.file=${log}/log.out log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%d{ISO8601} - %-5p - %m%n log4j.appender.FILEROLING=org.apache.log4j.RollingFileAppender log4j.appender.FILEROLING.file=${log}/logRR.out log4j.appender.FILEROLING.MaxFileSize=1KB log4j.appender.FILEROLING.MaxBackupIndex=30 log4j.appender.FILEROLING.layout=org.apache.log4j.PatternLayout log4j.appender.FILEROLING.layout.conversionPattern=%d{ISO8601} - %-5p - %m%n log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} - %-5p - %m%n
日志生成的代码
public class App { static String path = "src/resources/log4j.properties"; static Logger log = Logger.getLogger(App.class.getName()); public static void main(String[] args) throws Exception { PropertyConfigurator.configure(path); App app = new App(); int cnt = 0; while (true) { for (int i = 0; i < 100; i++) { app.printLog(); } Random rd = new Random(); int rdint = rd.nextInt(3000); out.println(rdint + " - " + cnt); Thread.sleep(rdint); cnt++; if (cnt == 10) { break; } } } public void printLog() { log.debug("ddddd"); log.info("iiiiiii"); } }
日志效果如下
grs:log4j grs$ grs:log4j grs$ ll -trTh total 1128 drwx------+ 24 grs staff 816B Apr 29 00:35:30 2016 .. -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:11 2016 logRR.out.30 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:11 2016 logRR.out.29 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:11 2016 logRR.out.28 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:11 2016 logRR.out.27 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:11 2016 logRR.out.26 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:11 2016 logRR.out.25 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.24 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.23 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.22 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.21 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.20 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.19 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.18 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:12 2016 logRR.out.17 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.9 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.16 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.15 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.14 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.13 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.12 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.11 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:13 2016 logRR.out.10 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.8 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.7 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.6 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.5 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.4 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.3 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.2 -rw-r--r-- 1 grs staff 1.0K Apr 29 01:37:15 2016 logRR.out.1 -rw-r--r-- 1 grs staff 0B Apr 29 01:37:15 2016 logRR.out -rw-r--r-- 1 grs staff 440K Apr 29 01:37:15 2016 log.out drwxr-xr-x 34 grs staff 1.1K Apr 29 01:37:15 2016 . grs:log4j grs$
时间: 2024-10-30 14:12:59