1. lib Add 3 pack slf4j-api, SLF4J-LOG4J12, log4j
2. Create a log4j.properties under SRC
# # # Direct log messages to stdout # # #log4j. appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.target=system.outlog4j.appender.stdout.layout= Org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d{absolute}%5p%c:%L-%m%n### Direct messages to File Hibernate.log # # # #log4j. appender.file=org.apache.log4j.fileappender# log4j.appender.file.file=hibernate.log#log4j.appender.file.layout=org.apache.log4j.patternlayout# Log4j.appender.file.layout.conversionpattern=%d{absolute}%5p%c{1}:%l-%m%n### set log levels-for more verbose logging Change ' info ' to ' Debug ' # # #log4j. Rootlogger=warn, stdout#log4j.logger.org.hibernate=info# log4j.logger.org.hibernate=debuglog4j.logger.com.web.controller=debug### Log Schema Export/update # # # # Log4j.logger.org.hibernate.tool.hbm2ddl=debug
3. Add log information to the class that needs to use the journal
Package Com.web.controller;import Java.io.ioexception;import Java.io.printwriter;import java.util.List;import Javax.annotation.resource;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.context.webapplicationcontext;import Org.springframework.web.context.support.webapplicationcontextutils;import Org.springframework.web.servlet.support.requestcontextutils;import Com.sun.xml.internal.ws.client.requestcontext;import Com.web.controller.entity.page;import Com.web.controller.entity.user;import Com.web.controller.service.IUserService, @Controller @requestmapping ("/user ") public class Usercontroller {private log log = Logfactory.getlog (This.getclass ()); @Resourceprivate Iuserservice UserService; @RequestMapping ("/getalluser") public StrinG Getalluser (HttpServletRequest request) {list<user> users = Userservice.getalluser (); Request.setattribute (" Users, Log.fatal ("This is fatal info"); return "/usercontroller/usermanager";}}
In this way, the console can output information, can replace the System.out.print function
log4j Configuring the Logging system