Eclipse and log4j configuration (detailed ~)

Source: Internet
Author: User

[1] starting from scratch

 

A). Create a Java project> Create a package> Create a Java class;

B). Import jar package (One is enough.Here I am using a log4j-1.2.14.jar,

C). Create log4j. properties and place it inProject root directory;

Log4j. rootlogger = info, serverdailyrollingfile, stdout
Log4j. appender. serverdailyrollingfile = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. serverdailyrollingfile. datepattern = '. 'yyyy-mm-dd
Log4j. appender. serverdailyrollingfile. File = C: // logs/notify-subscription.log
Log4j. appender. serverdailyrollingfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. serverdailyrollingfile. layout. conversionpattern = % d-% m % N
Log4j. appender. serverdailyrollingfile. append = true

Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS} % P [% C] % m % N

 

D). Load log4j in main:

Propertyconfigurator. Configure ("log4j. properties ");

 

E). Write a small program and test it. Well, let's take a look at the effect:

[Shenma] cannot find the file exception?

1). It is not recommended to use absolute paths. It is not elegant;

2) Place the log4j file in the bin/directory:

A). In the code, propertyconfigurator. Configure ("Bin/Log4j. properties ");

B) in the code, propertyconfigurator. Configure (Classloader. getsystemresource("Log4j. properties "));

C). Note that the directory must be located in the direct bin directory and not in the deeper bin directory. But is it true that this is for God's horse?

See: http://blog.sina.com.cn/s/blog_3f4755c70100jco1.html

3) Mandatory killer:

Private Static void initlog4j (){
Properties prop = new properties ();

Prop. setproperty ("log4j. rootlogger", "debug, console ");
Prop. setproperty ("log4j. appender. Console", "org. Apache. log4j. leleappender ");
Prop. setproperty ("log4j. appender. Console. layout", "org. Apache. log4j. patternlayout ");
Prop. setproperty ("log4j. appender. console. layout. conversionpattern "," % d {hh: mm: SS, SSS} [% T] %-5 p % c {1}: % m % N ");

Propertyconfigurator. Configure (PROP );
}

 

 

 

[2] log4j format details

 

Log4j. rootlogger = Log Level, appender1, appender2 ,....

  • Log Level:All <debug <info <warn <error <fatal <off, Case insensitive
  • Note: You must enter one of the appender in the consoleStdoutYou can.
  • Note: rootloggerBy defaultEffective
  • Note that ifOnly wantOperation, then: log4j. logger.Com. Hutu= Info, stdout, indicates that the log takes effect for package COM.
  • Note: in this way, you can differentiate DEV/online, or reduce the performance impact:If (log. isdebugenabled ()){Log. debug ();}

Log4j. appender. appender1 = org. Apache. log4j. Where is the log output?

  • Consoleappender(Console)
  • Fileappender(File)
  • Dailyrollingfileappender(One log file is generated every day)
  • Rollingfileappender(A new file is generated when the file size reaches the specified size)
  • Writeappender(Send log information to any specified place in stream format)
  • Jdbcappender(Save the log information to the database)

Log4j. appender. appender1.file = file directory and file

$ {User. Home}/logs /...

Log4j. appender. appender1.maxfilesize = Maximum File Size

Log4j. appender. appender1.maxbackupindex = number of backup files

  • Appender1 is defined in the first line;
  • File directory and file, for example,/home/admin/logs/huudan. Log
  • Maximum file size, for example, KB
  • Number of backup files, for example, 1

Log4j. appender. serverdailyrollingfile. datepattern = Log suffix format

  • For example, '.' yyyy-mm-dd

Log4j. appender. appender1.layout = org. Apache. log4j. Log layout format

  • Htmllayout (HTML table layout)
  • Simplelayout (Level and information string containing log information)
  • Ttcclayout (including the log generation time, execution thread, category, and other information)
  • Patternlayout (you can flexibly specify the layout format,Common)

Log4j. appender. appender1.layout. conversionpattern = log output format

  1. For example, % d-% m % N or % d {yyyy-mm-dd hh: mm: SS} % P [% C] % m % N
  2. % CFull name of the class to which the output log information belongs
  3. % DThe date or time of the log output time point. The default format is iso8601. You can also specify the format later, for example, % d {YYY-m-dd hh: mm: SS }, output:-22:10:28
  4. % FClass Name of the class to which the output log information belongs
  5. % LThe location where the log event is generated, that is, the number of lines in the class where the statement that outputs the log information is located
  6. % MOutput the specified information in the Code, such as the message in log (Message ).
  7. % NReturns a carriage return line break. The windows platform is "RN" and the UNIX platform is "N"
  8. % PThe output priority, namely debug, info, warn, error, and fatal. If it is output by calling debug (), it is debug, and so on.
  9. % RIt takes several milliseconds to output the log information from application startup to application startup.
  10. % TName of the thread that generates the log event
  11. See: http://blog.sina.com.cn/s/blog_4e4dd5570100qowy.html

Log4j. appender. serverdailyrollingfile. append = true

  • For example, if it is not explained, it is

Summary:

  • Logger: completes logging and sets the log information level.
  • Appender class: determines the log destination, terminal, DB, Hard Disk
  • Layout class: determines the log output style, such as the current thread, row number, and time.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.