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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

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

搜索引擎谁主沉浮 一个有意思的搜索引擎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