• Log4j的简单使用


    package com.chase.test;
    
    import org.apache.log4j.Logger;
    
    public class Log4jTest {
    
        /**
         * Log4j
         */
        protected final Logger logger = Logger.getLogger(getClass());
        
        public <T> void testLog(T t) {
            logger.info(t);
        }
        
        public void end() {
            logger.info("程序结束!!!");
            System.out.println("end+++++");
        }
        
        public static void main(String[] args) {
            Log4jTest log4jTest = new Log4jTest();
            log4jTest.testLog("程序启动");
            log4jTest.testLog(12580);
            log4jTest.testLog(12580l);
            log4jTest.end();
        }
    
    }

    输出:

    2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <程序启动>
    2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <12580>
    2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <12580>
    2013-10-14 17:44:40,921 INFO [com.chase.test.Log4jTest] - <程序结束!!!>
    end+++++

    log4j.properties配置:

    # For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
    # For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
    log4j.rootLogger=info, stdout, logfile
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
    
    log4j.appender.logfile=org.apache.log4j.RollingFileAppender
    ###日志存放位置,默认为程序根目录,具体到某个盘符 log4j.appender.logfile.File
    =/logs/chase.log log4j.appender.logfile.MaxFileSize=512KB # Keep three backup files. log4j.appender.logfile.MaxBackupIndex=3 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

    记得导入:log4j.jar包

  • 相关阅读:
    如何检测和删除通过pip安装的Python包?
    tensorflow使用keras
    ubuntu18.04安装tensorflow2.0
    python pip版本的安装与管理
    leetcode 377. 组合总和 Ⅳ
    little tips
    NYOJ 104-最大和
    NYOJ 44-子串和
    NYOJ 15-括号匹配(二)
    leetCode 32. Longest Valid Parentheses
  • 原文地址:https://www.cnblogs.com/chasewade/p/3368838.html
Copyright © 2020-2023  润新知