信息时代的生存哲学

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

jakarta commons log4j 学习笔记

关键字:jakarta, commons, configuration

如何配置Log4j

  1. 用BasicConfigurator 对Log4j 进行配置,入门级的配置方式:
    import org.apache.log4j.BasicConfigurator;
    import org.apache.log4j.Logger;
    ......
    public class Log4jTest {
      private static Logger logger = Logger.getLogger(Log4jTest.class.getName());
    ......
      public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.info("log4j has been configurated with BasicConfigurator.configuration successfully!");
      }
    }
    运行后可以得到类似于下面的结果:
    0 [main] INFO Log4jTest - log4j has been configed with BasicConfigurator.configure() successfully!

  2. 用配置文件对Log4j 进行配置,非常灵活的一种方式:
    # For the general syntax of property based configuration files see the
    # documenation of org.apache.log4j.PropertyConfigurator.
    # The root category uses the appender called A1. Since no priority is
    # specified, the root category assumes the default priority for root
    # which is DEBUG in log4j. The root category is the only category that
    # has a default priority. All other categories need not be assigned a
    # priority in which case they inherit their priority from the
    # hierarchy.

    log4j.rootCategory=INFO, A1, A2

    # A1 is set to be a FileAppender which outputs to the file
    # "factor.log". Start the server NumberCruncherServer and two
    # NumberCruncherClients, and ask to factor two numbers
    # near-simultaneously. Notice that the log output from these two
    # requests are logged in the file factor.log. Nevertheless, the logs
    # of these requests can still be distinguished given their distinct
    # nested diagnostic contexts.

    log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.A1.File=WEB-INF/logs/portal.log
    log4j.appender.A1.DatePattern='.'yyyy-MM-dd
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout

    log4j.appender.A2=org.apache.log4j.ConsoleAppender
    log4j.appender.A2.layout=org.apache.log4j.PatternLayout

    # Note the %x conversion specifier for NDC printing.
    # %d date time
    # %-5p debug level
    # %m messages
    # %l class with method and line number (slowly! dubug only, on release use %c{2} in release version)
    # %n \n or \r\n

    #debug version
    log4j.appender.A1.layout.ConversionPattern=%d [%-5p] %l - %m%n
    log4j.appender.A2.layout.ConversionPattern=%d [%-5p] %l - %m%n

    #release version
    # log4j.appender.A1.layout.ConversionPattern=%d [%-5p] %c{2} - %m%n

    有了上面的配置文件后,可以通过以下任意一种方式对其进行装载:
    • 让Log4j自动装载配置文件
      将上面的文件命名为log4j.properties,并保存到WEB-INF/classes/ 目录下,这样当第一次调用Logger.getLogger(SomeClass.class.getName()); 时,Log4j 就会自动装载该配置文件对log 系统进行初始化。
    • 用系统变量指定配置文件的位置
      如果不想把配置文件放到WEB-INF/clases/ 目录下,或者不想把配置文件命名为log4j.properties 那么就可以通过系统变量log4j.configuration来对其进行自定义,例如:
      -Dlog4j.configuration=file:/D:/projects/someproject/WEB-INF/log4j.properties
      用指定位置的文件进行配置
      -Dlog4j.configuration=foo.txt
      用WEB-INF/classes/目录下的foo.txt 进行配置
    • 用PropertyConfigurator 读取配置文件
      import org.apache.log4j.PropertyConfigurator;
      import org.apache.log4j.Logger;
      ......
      public class Log4jTestServlet {
        private static Logger logger = Logger.getLogger(Log4jTestServlet.class.getName());
      ......
        public void init(ServletConfig servletConfig) throws ServletException {
          super.init(servletConfig);
          String file = getRealPath(getInitParameter("log4j-init-file"));
          PropertyConfigurator.configure(file);
          /*
           *PropertyConfigurator.configure(Properties);
           *PropertyConfigurator.configure(URL);
           *PropertyConfigurator.configureAndWatch(file, 30); //delay 30 second
           */
          logger.info("log4j has been configurated with PropertyConfigurator.configuration successfully!");
        }
      }

参考文档

Log4j short mannul
http://logging.apache.org/log4j/docs/manual.html
posted on 2004-11-18 11:17  信息时代的生存哲学  阅读(1522)  评论(0编辑  收藏  举报