Log4j log files are saved to the project release directory. log4j files are separated every day and named by date.

Source: Internet
Author: User

Set the directory by yourself, that is, set through system. setproperty at project startup to implement servletcontextlistener:


Public class log4jlistener implements servletcontextlistener {

Public static final string log4jdirkey = "log4jdir ";

Public void contextdestroyed (servletcontextevent ){

System. getproperties (). Remove (log4jdirkey );

}


Public void contextinitialized (servletcontextevent ){

String log4jdir = servletcontextevent. getservletcontext (). getrealpath ("/");

// System. Out. println ("log4jdir:" + log4jdir );

System. setproperty (log4jdirkey, log4jdir );

}

}


Web. xml configuration:

<Listener>

<Listener-class> com. Haier. Framework. util. log4jlistener </listener-class>

</Listener>


Log4j. prtperties Configuration:

# Set root logger level to warn and append to stdout

Log4j. rootlogger = info, console, file

Log4j. appender. Console = org. Apache. log4j. leleappender

Log4j. appender. Console. layout = org. Apache. log4j. patternlayout \ t

# Pattern to output the caller's file name and line number.

# Log4j. appender. Console. layout. conversionpattern = [% P] % l --- % m % N


Log4j. appender. File = org. Apache. log4j. dailyrollingfileappender

# Name the daily log file name by date,

Log4j. appender. file. File =$ {log4jdir}/logs/log. Log

Log4j. appender. file. datepattern = yyyymmdd '. Log'

Log4j. appender. file. layout = org. Apache. log4j. patternlayout

Log4j. appender. file. layout. conversionpattern = [%-5 p] % L-% d {yyyy-mm-dd hh: mm: SS, SSS} method: % L-% m % N


During the test, how can I seek the similar "log. log20140518.log, there is only one log file. log File, the log generated on the 7th day (today) is "log. logs are stored in the form of logs generated on the 7th day by the 8th day (tomorrow. save the log file to log. log201400707.log, and then create an 8-day log. log files, and so on.


Therefore, you must note that you must modify the system time to view the generated logs during the test.


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.