Import the log4j jar package,
Make the following configuration in Web. xml
<!--log4j Configuration - <Context-param> <Param-name>Webapprootkey</Param-name> <Param-value>Myapplication.root</Param-value> </Context-param> <Context-param> <Param-name>Log4jconfiglocation</Param-name> <Param-value>/web-inf/log4j.properties</Param-value> </Context-param> <Context-param> <Param-name>Log4jrefreshinterval</Param-name> <Param-value>60000</Param-value><!-- Reload log4j configuration Interval ms-- </Context-param>
Create a file under Web-inf log4j.properties
The contents are 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 according to different requirements.
On the Web page, make a selection of logging level page, select logging, incoming servlet,
Query logging level and upload to Web page
@RequestMapping (value= "/logging_view", method =requestmethod.get) PublicString Tologgingview (httpservletrequest request,model Model) {Properties props=NewProperties (); Try {
Read the contents of the Log4j.properties file String path= Systemcontroller.class. getClassLoader (). GetResource (""). Touri (). GetPath (); InputStream in=NewBufferedinputstream (NewFileInputStream (Path+".. /log4j.properties ") ;//This is the file path of the log4j.properties, which configures the props.load (in) itself according to the path of its servlet; String value= Props.getproperty ("Log4j.rootlogger"); Logger.debug (The value of the Log4j.rootlogger key is: "+value); Model.addattribute ("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); } returnSessionhandler.verifysession (Request, "Logging_view"); }//Get data from Web page, set logging level @RequestMapping (value= "/setlogginglevel", method =requestmethod.post) Publicstring Setlogginglevel (@RequestParam string Rootlogger, @RequestParam string loggerlevel, Httpservletreque ST request, model Model) {RequestContext RequestContext=NewRequestContext (Request); String[] Loggers= Rootlogger.split (","); loggers[0] =Loggerlevel; String Logging= ""; for(inti = 0; i < loggers.length; i++) {Logging+ = loggers[i]+ ","; } Logging= logging.substring (0, Logging.length ()-1); Properties Props=NewProperties (); Try {
Writes logging level to log4j.properties file String path= Systemcontroller.class. getClassLoader (). GetResource (""). Touri (). GetPath () + ". /log4j.properties "; InputStream in=NewBufferedinputstream (NewFileInputStream (path)); Props.load (in); OutputStream Fos=NewFileOutputStream (path); Props.setproperty ("Log4j.rootlogger", logging); Props.store (FOS,"Last Update"); String value= Props.getproperty ("Log4j.rootlogger"); Logger.debug (value); //Close FileIn.close (); Fos.close ();List<String> list =NewArraylist<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"; }
This completes, log4j the other configuration, just follow the above method to do the line.
Java web Dynamic configuration log4j