log4j配置日志系统

1. lib里加入3个包 slf4j-api, slf4j-log4j12, log4j

2. 在src下 创建log4j.properties

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.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=debug
log4j.logger.com.web.controller=debug ### log schema export/update ###
#log4j.logger.org.hibernate.tool.hbm2ddl=debug

3. 在需要使用日志的类里添加log信息

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());
@Resource
private IUserService userService; @RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request){
List<User> users = userService.getAllUser();
request.setAttribute("users", users);
log.fatal("this is fatal info");
return "/userController/userManager";
} }

这样, 控制台就可以输出信息了, 可以代替system.out.print功能

上一篇:【贪心】[hdu1052]Tian Ji -- The Horse Racing(田忌赛马)[c++]


下一篇:js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时