Dynamic configuration of log4j in Java Web and dynamic log4j in Java Web

Source: Internet
Author: User

Dynamic configuration of log4j in Java Web and dynamic log4j in Java Web

Import the jar package of log4j,

Configure the following in web. xml:

<! -- Log4j Configuration --> <context-param> <param-name> webAppRootKey </param-name> <param-value> myapplication. </param-value> </context-param> <param-name> log4jConfigLocation </param-name> <param-value>/WEB-INF/log4j. properties </param-value> </context-param> <param-name> log4jrefresh interval </param-name> <param-value> 60000 </param- value> <! -- Reload the interval MS for log4j configuration --> </context-param>

Create the file log4j. properties in the WEB-INF

The content is as follows:

log4j.rootLogger=INFO, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=${user.home}/myapplication.loglog4j.appender.logfile.MaxFileSize=512KBlog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

Different configurations can be made based on different requirements.

On the web page, select the logging level, select the logging level, and input the servlet,

  
// Query the logging level and upload it to the web page
@ RequestMapping (value = "/logging_view", method = RequestMethod. GET) public String toLoggingView (HttpServletRequest request, Model model) {Properties props = new Properties (); try {
// Read log4j. properties File Content String path = SystemController. class. getClassLoader (). getResource (""). toURI (). getPath (); InputStream in = new BufferedInputStream (new FileInputStream (path + ".. /log4j. properties "); // This is log4j. properties file path, configure props according to the servlet path. load (in); String value = props. getProperty ("log4j. rootLogger "); logger. debug ("log4j. the value of the rootLogger key is: "+ value); model. addattriger ("rootLogger", value); in. close ();} catch (URISyntaxException e) {logger. error ("SystemController: logging_view: URISyntaxException:" + e);} catch (FileNotFoundException e) {logger. error ("SystemController: logging_view: FileNotFoundException:" + e);} catch (IOException e) {logger. error ("SystemController: logging_view: IOException:" + e);} return SessionHandler. verifySession (request, "logging_view");} // obtain the data transmitted from the web page and set the logging level @ RequestMapping (value = "/setLoggingLevel", method = RequestMethod. POST) public String setLoggingLevel (@ RequestParam String rootLogger, @ RequestParam String loggerLevel, HttpServletRequest request, Model model) {RequestContext requestContext = new RequestContext (request); String [] loggers = rootLogger. split (","); loggers [0] = loggerLevel; String logging = ""; for (int I = 0; I <loggers. length; I ++) {logging + = loggers [I] + "," ;}logging = logging. substring (0, logging. length ()-1); Properties props = new Properties (); try {
// Write the logging level to log4j. properties file String path = SystemController. class. getClassLoader (). getResource (""). toURI (). getPath () + ".. /log4j. properties "; InputStream in = new BufferedInputStream (new FileInputStream (path); props. load (in); OutputStream fos = new FileOutputStream (path); props. setProperty ("log4j. rootLogger ", logging); props. store (fos, "last update"); String value = props. getProperty ("log4j. rootLogger "); logger. debug (value); // close the file in. close (); fos. close (); List <String> list = new ArrayList <String> (); list. add (requestContext. getMessage ("logging. level "); if (value. split (",") [0]. equals (loggerLevel) {model. addAttribute ("msg", requestContext. getMessage ("edit. success ", list);} else {model. addAttribute ("msg", requestContext. getMessage ("edit. failed ", list);} catch (URISyntaxException e) {logger. error ("SystemController: logging_view: URISyntaxException:" + e);} catch (FileNotFoundException e) {logger. error ("SystemController: logging_view: FileNotFoundException:" + e);} catch (IOException e) {logger. error ("SystemController: logging_view: IOException:" + e);} return "msg ";}

In this way, other log4j configurations will be completed just like the above method.


How does log4j be used and configured in java web projects?

It's okay if you just drop it to lib... This is a 3rd-party JAR package, and then you can use methods such as Log. XXX after importing in your code...
If you want to say where it is used... Generally, an error is written in catch, and the info class is written in the intermediate service processing class of your project. Similar to mvc c, the request IP address, start time, end time, and request method are recorded, specific Class Name, etc.

Let's talk about xml configuration ..
See Baidu Library...
Wenku.baidu.com/.. B .html

How to configure file output in java web log4j

Configure in the log4j. properties file.
You can use absolute or relative paths for paths.

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.