log4j Configuring the Logging system

Source: Internet
Author: User
Tags log log

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

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.