Log4j file output

Source: Internet
Author: User

Write a class that dynamically generates Properties (the form in which log4j. properties is loaded into the memory). Specify the class to dynamically modify anything!
======================================
The directory cannot be created, but the log file is automatically created.
Log4j. appender. R = org. apache. log4j. RollingFileAppender
Log4j. appender. R. File = C: \ bea \ user_projects \ domain \ log

====================================

You can write the configuration information of log4j in a file, such as sort. properties.
Then, use servlet to initialize log4j so that it initializes log4j when the server starts.
To modify the log output destination at any time, you only need to modify log4j. appender. A2.File in the configuration file.

==========================================

The content of sort. properties is as follows:
#1 defines two output ends
Log4j. rootLogger = debug, A1, A2

#2 define A1 output to Controller
Log4j. appender. A1 = org. apache. log4j. leleappender
#3 define the layout mode of A1 as PatternLayout
Log4j. appender. A1.layout = org. apache. log4j. PatternLayout
#4 define A1 output format
Log4j. appender. A1.layout. ConversionPattern = %-4r [% t] %-5 p % c-% m % n

#5 define A2 output to file
Log4j. appender. A2 = org. apache. log4j. RollingFileAppender
#6 define the file to which A2 outputs
Log4j. appender. A2.File = D: \ hello. log
#7 define the maximum length of the A2 output file
Log4j. appender. A2.MaxFileSize = 1KB
#8 define the number of A2 backup files
Log4j. appender. A2.MaxBackupIndex = 3
#9 define the layout mode of A2 as PatternLayout
Log4j. appender. A2.layout = org. apache. log4j. PatternLayout
#10 define the A2 output format
Log4j. appender. A2.layout. ConversionPattern = % d {yyyy-MM-dd hh: mm: ss }:% p % t % c-% m % n

======================================

The servlet code for initializing log4j is as follows:
Package com. ctc. Init;

Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. io .*;
Import java. util .*;
Import org. apache. log4j .*;

Public class Log4jInit extends HttpServlet {
Private static final String CONTENT_TYPE = "text/html; charset = GBK ";
Public static Logger logger = Logger. getLogger (Log4jInit. class. getName ());

// Initialize global variables
Public void init () throws ServletException {
// Use web. xml to Dynamically Retrieve the configuration file
String prefix = getServletContext (). getRealPath ("/");
String file = getInitParameter ("log4j-init-file ");
// If the corresponding configuration file is not provided, Initialization is not performed.
If (file! = Null ){
PropertyConfigurator. configure (prefix + file );
}
}

Public void doGet (HttpServletRequest req, HttpServletResponse res ){
}

}

//////////////////////////////////////// ////////////////////////
The configuration in web. xml is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4">
<Display-name> systemapp </display-name>
<Servlet>
<Servlet-name> Log4jInit </servlet-name>
<Servlet-class> com. ctc. Init. Log4jInit </servlet-class>
<Init-param>
<Param-name> log4j-init-file </param-name>
<Param-value> sort. properties </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
</Web-app>

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.