Log4j 簡單概念及樣本.

來源:互聯網
上載者:User
Loggers, Appenders and LayoutsLog4j has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.Appender: an output destination is called an appender.For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console.Here is a sample configuration file that results in identical output as the previous BasicConfigurator based example.------------------------------------------------# Set root logger level to DEBUG and its only appender to A1.log4j.rootLogger=DEBUG, A1# A1 is set to be a ConsoleAppender.log4j.appender.A1=org.apache.log4j.ConsoleAppender# A1 uses PatternLayout.log4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n Suppose we are no longer interested in seeing the output of any component belonging to the com.foo package. The following configuration file shows one possible way of achieving this.log4j.rootLogger=DEBUG, A1log4j.appender.A1=org.apache.log4j.ConsoleAppenderlog4j.appender.A1.layout=org.apache.log4j.PatternLayout# Print the date in ISO 8601 formatlog4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n# Print only messages of level WARN or above in the package com.foo.log4j.logger.com.foo=WARN -----------------------------log4j.rootLogger=debug, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the caller's file name and line number.log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppenderlog4j.appender.R.File=example.loglog4j.appender.R.MaxFileSize=100KB# Keep one backup filelog4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n Calling the enhanced MyApp with the this configuration file will output the following on the console. INFO [main] (MyApp2.java:12) - Entering application.DEBUG [main] (Bar.java:8) - Doing it again! INFO [main] (MyApp2.java:15) - Exiting application.-----------------------------------------------
#ERROR為等級,FILE為Appenderlog4j.rootLogger=ERROR,CONSOLE,FILE#定義一個新的logger,名稱為admin,可通過Logger.getLogger("admin")獲得,繼承了rootLogger的配置log4j.logger.admin=WARNlog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.conversionPattern=%p-[%d] -%M (%F\:%L) %m%n%nlog4j.appender.FILE=org.apache.log4j.RollingFileAppenderlog4j.appender.FILE.File=c\:/risen_app.loglog4j.appender.FILE.MaxFileSize=2000KBlog4j.appender.FILE.MaxBackupIndex=1log4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=%d %-5p %c - %m%n

//MyApp.javapackage com.test.log4j;import org.apache.log4j.BasicConfigurator;import org.apache.log4j.Logger;public class MyApp {// Define a static logger variable so that it references the// Logger instance named "MyApp".static Logger log=Logger.getLogger(MyApp.class);//按名稱取logger//static Logger log=Logger.getLogger("admin");public static void main(String[] args) { // Set up a simple configuration that logs on the console.     BasicConfigurator.configure();log.debug("MyApp 啟動");Foo f=new Foo();f.doIt();log.fatal("MyApp 中止");}}//Foo.javapackage com.test.log4j;import org.apache.log4j.Logger;public class Foo {static Logger log=Logger.getLogger(Foo.class);public void doIt(){log.info("Foo DoIt.");}}
log4j.properties
log4j.rootLogger=INFO,FILE#定義一個新的logger,名稱為admin,可通過Logger.getLogger("admin")獲得,繼承了rootLogger的配置log4j.logger.admin=DEBUGlog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.conversionPattern=%p-[%d] -%M (%F\:%L) %m%n%nlog4j.appender.FILE=org.apache.log4j.RollingFileAppenderlog4j.appender.FILE.File=c\:/wcg.loglog4j.appender.FILE.MaxFileSize=2000KBlog4j.appender.FILE.MaxBackupIndex=1log4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=%d %-5p %c - %m%n

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.