惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - 信息时代的生存哲学

搜索引擎谁主沉浮 一个有意思的搜索引擎Clusty(转) 是男人,一定要知道!(转) 猎狗与兔子 小狗的幸福 谨以此篇献给即将奔三十的人们 jakarta commons configuration 学习笔记 女孩对男孩说 等待是一种美丽 软件之战 我有话要说 老婆对老公说:我对你的要求不高吧!? [CI柳叶刀]谁诱奸了企业竞争情报?(转载) 毕业那天我们一起失恋 一些有待阅读的文章 推荐人力资源的文章 成功就是如此的简单 管理者执行力的素质要求 管理团队的“满汉全席”
jakarta commons log4j 学习笔记
信息时代的生存哲学 · 2004-11-18 · via 博客园 - 信息时代的生存哲学

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