1. After the basic framework has been set up, add the log function. You only need to modify the original web. xml and add some configuration information: <! -- Notifies the location of the spring log4j configuration file --> <context-param> <param-Name> log4jconfiglocation </param-Name> <param-value>/WEB-INF/log4j. properties </param-value> </context-param> <! -- Spring refreshes the log4j configuration file by default, the Unit is millisecond --> <context-param> <param-Name> log4jrefreshinterval </param-Name> <param-value> 60000 </param-value> </context-param> <! -- When the application is started, spring loads log4j --> <listener-class> Org. springframework. web. util. log4jconfiglistener </listener-class> </listener> 2. next we will define log4j. properties # global logging configurationlog4j. rootlogger = debug, console, file ## appenders ### define an appender named console, which is set to be a consoleappenderlog4j. appender. console = org. apache. log4j. consoleappender # define an appender named file, wh Ich is set to be a rollingfileappenderlog4j. appender. file = org. apache. log4j. rollingfileappender # Replace <catalina_home> with your true pathlog4j. appender. file. file = D:/Eclipse/workspace/login/log.txt # layouts ### assign a simplelayout to console appenderlog4j. appender. console. layout = org. apache. log4j. simplelayout # assign a patternlayout to file appenderlog4j. appender. file. layout = org. apache. log4j. Patternlayoutlog4j. appender. file. layout. conversionpattern = % T % P-% m % N in log4j. in properties, we define that the log level is debug, and the log output destination is console and file ). 3. in this way, we can perform log operations in the program code: package test. register. action; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. log4j. logmanager; import Org. apache. log4j. logger; import Org. apache. struts. action. action; import Org. apache. struts. action. actionform; import Org. apache. struts. action. actionforward; import Org. apache. struts. action. actionmapping; import Org. apache. struts. action. dynaactionform; import test. register. entity. register; import test. register. service. registerservice; public class registeraction extends action {
Private Static final logger log = logmanager. getlogger (registeraction. Class );Private registerservice; Public registerservice getregisterservice () {return registerservice;} public void setregisterservice (registerservice) {This. registerservice = registerservice;} public actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {
Log.info ("Save register ...");Register r = (Register) (dynaactionform) form ). get ("register"); getregisterservice (). saveregister (r); Return Mapping. findforward ("success") ;}} 4. in the above log4j. in properties, we specify that the absolute path is used for the log input file, which is inconvenient in many cases. For this reason, we make the following modifications. add a context parameter <context-param> <param-Name> webapprootkey </param-Name> <param-value> weblogin. root </param-value> </context-param> adds a system variable named webapprootkey. The variable value can be used to specify the directory where the application is deployed. properties:
Log4j. appender. file. File =$ {weblogin. Root}/log.txt