概述
log4j —log for java java日志框架
是一个用户收集日志信息的框架,可以直接在项目中引入使用
log4j的引入
导入log4j的jar包
导入log4j的配置文件(log4j.properties)
log4j.properties
log4j.rootLogger = debug,stdout,D,E
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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =D://logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
注解
EasyMall改写(添加日志框架)
把监听信息存放在日志框架中
@WebListener()
public class ScListener1 implements ServletContextListener{
//创建Logger对象(指定当前类的字节码对象来构建Logger类的对象)
Logger logger=Logger.getLogger(ScListener1.class);
//ServletContext创建对象(web应用被加载)触发下面的重写
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//System.out.println("EasyMall项目启动...");
//指定输出级别
logger.info("EasyMall项目启动...");
}
//ServletContext对象销毁(web应用被移除)触发下面的重写
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
//System.out.println("EasyMall项目关闭...");
//指定输出级别
logger.info("EasyMall项目关闭...");
}
}
@WebListener()
public class RequestListener implements ServletRequestListener {
//
Logger logger=Logger.getLogger(RequestListener.class);
//当请求结束时触发下面的重写方法
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
//获取request对象
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
//获取用户访问的url路径
String url = request.getRequestURL().toString();
//获取用户的ip
String ip = request.getRemoteAddr();
//访问资源的用户名---初始是游客
String username="游客";
//判断用户是否登录
if(request.getSession(false)!=null&&request.getSession().getAttribute("user")!=null){
//用户已登陆
//获取用户名
User user= (User) request.getSession().getAttribute("user");
//把用户名覆盖之前初始的用户名
username= user.getUsername();
}
//System.out.println("用户【"+username+"】ip【"+ip+"】访问url【"+url+"】请求结束");
logger.debug("用户【"+username+"】ip【"+ip+"】访问url【"+url+"】请求结束");
}
//当请求开始时触发下面的重写方法
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
//获取request对象
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
//获取用户访问的url路径
String url = request.getRequestURL().toString();
//获取用户的ip
String ip = request.getRemoteAddr();
//访问资源的用户名---初始是游客
String username="游客";
//判断用户是否登录
if(request.getSession(false)!=null&&request.getSession().getAttribute("user")!=null){
//用户已登陆
//获取用户名
User user= (User) request.getSession().getAttribute("user");
//把用户名覆盖之前初始的用户名
username= user.getUsername();
}
//System.out.println("用户【"+username+"】ip【"+ip+"】访问url【"+url+"】请求开始");
logger.debug("用户【"+username+"】ip【"+ip+"】访问url【"+url+"】请求开始");
}
}
//javabaen可以封装数据以及提供方法
public class User implements HttpSessionBindingListener {
//
Logger logger=Logger.getLogger(User.class);
//javaben属性要和表的字段对应
private int id;
private String username;
private String password;
private String nickname;
private String email;
//构造方法
//无参构造
public User() {
}
//有参构造
public User(int id, String username, String password, String nickname, String email) {
this.id = id;
this.username = username;
this.password = password;
this.nickname = nickname;
this.email = email;
}
//提供get、set方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
//重写toString方法
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
'}';
}
//当javabean对象存放在session对象中触发下面重写方法
@Override
public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
// System.out.println("用户【"+username+"】登陆");
logger.info("用户【"+username+"】登陆");
}
//当javabean对象移除在session对象中触发下面重写方法
@Override
public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
// System.out.println("用户【"+username+"】注销");
logger.info("用户【"+username+"】注销");
}
}