• [log4j] 可用案例


    可用的 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

    日志生成的代码

    package log4j;
    
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    
    public class Log4jTest {
    
        static String path = "src/resources/log4j.properties";
        static Logger log = Logger.getLogger(Log4jTest.class.getName());
        
        public static void run() throws InterruptedException{
            
            PropertyConfigurator.configure(path);
            
            int i = 0;
            for (; i < 10000; i++) {
                log.debug("ddddd");
                log.info("iiiiiii");
            }
            System.out.println("loop times :" + i);
        }
    }

    控制台日志如下

    ......
    2016-10-28 22:33:16,068 - INFO  - iiiiiii
    2016-10-28 22:33:16,068 - DEBUG - ddddd
    2016-10-28 22:33:16,068 - INFO  - iiiiiii
    2016-10-28 22:33:16,068 - DEBUG - ddddd
    2016-10-28 22:33:16,068 - INFO  - iiiiiii
    2016-10-28 22:33:16,068 - DEBUG - ddddd
    2016-10-28 22:33:16,068 - INFO  - iiiiiii
    2016-10-28 22:33:16,068 - DEBUG - ddddd
    2016-10-28 22:33:16,068 - INFO  - iiiiiii
    loop times : 10000

    日志文件目录如下

    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$ 

     参加资料:

    log4j, tutorialspoint

    change location of log4j.properties, stackoverflow

  • 相关阅读:
    Java集合类框架的基本接口有哪些?
    JSR303校验 —— hibernate-validator实现
    JSON和对象或集合间的转换
    Servlet 单例、多线程
    session.invalidate()
    request获取各种路径
    动态加载类并实例化对象 —— newInstance
    js 事件冒泡和事件捕获
    js事件绑定
    css 选择器和优先级
  • 原文地址:https://www.cnblogs.com/TonyYPZhang/p/5444950.html
Copyright © 2020-2023  润新知