SSM框架系列日志管理实现

网友投稿 757 2022-05-29

本博客介绍log4j的日志管理配置

log4j.peroperties:

在D盘创建一个logs的文件夹来存放日志

### set log levels ###

log4j.rootLogger = info , Console , D

#Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

log4j.logger.java.sql.ResultSet=INFO

log4j.logger.org.apache=INFO

log4j.logger.java.sql.Connection=INFO

log4j.logger.java.sql.Statement=INFO

log4j.logger.java.sql.PreparedStatement=INFO

#output2file

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = D\:/logs/log.log

log4j.appender.D.Append = true

log4j.appender.D.Threshold = INFO \#\# \u8F93\u51FAinfo\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7

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

写个日志管理类:

package com.appweb.core.logger;

public class Logger {

private org.apache.log4j.Logger logger;

/**

* 构造方法,初始化Log4j的日志对象

*/

private Logger(org.apache.log4j.Logger log4jLogger) {

logger = log4jLogger;

}

/**

SSM框架系列之日志管理实现

* 获取构造器,根据类初始化Logger对象

*

* @param Class

* Class对象

* @return Logger对象

*/

public static Logger getLogger(Class classObject) {

return new Logger(org.apache.log4j.Logger.getLogger(classObject));

}

/**

* 获取构造器,根据类名初始化Logger对象

*

* @param String

* 类名字符串

* @return Logger对象

*/

public static Logger getLogger(String loggerName) {

return new Logger(org.apache.log4j.Logger.getLogger(loggerName));

}

public void debug(Object object) {

logger.debug(object);

}

public void debug(Object object, Throwable e) {

logger.debug(object, e);

}

public void info(Object object) {

logger.info(object);

}

public void info(Object object, Throwable e) {

logger.info(object, e);

}

public void warn(Object object) {

logger.warn(object);

}

public void warn(Object object, Throwable e) {

logger.warn(object, e);

}

public void error(Object object) {

logger.error(object);

}

public void error(Object object, Throwable e) {

logger.error(object, e);

}

public void fatal(Object object) {

logger.fatal(object);

}

public String getName() {

return logger.getName();

}

public org.apache.log4j.Logger getLog4jLogger() {

return logger;

}

public boolean equals(Logger newLogger) {

return logger.equals(newLogger.getLog4jLogger());

}

}

可以写个BaseController的控制类

然后将log的管理类加进来,实现日志管理的重复使用

package com.appweb.core.base;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import org.springframework.web.servlet.ModelAndView;

import com.appweb.core.entity.Page;

import com.appweb.core.entity.PageData;

import com.appweb.core.logger.Logger;

import com.appweb.core.utils.UuidUtil;

public class BaseController {

protected Logger logger = Logger.getLogger(this.getClass());

private static final long serialVersionUID = 6357869213649815390L;

/**

* 得到分页列表的信息

*/

public static void logBefore(Logger logger, String interfaceName){

logger.info("");

logger.info("start");

logger.info(interfaceName);

}

public static void logAfter(Logger logger){

logger.info("end");

logger.info("");

}

}

然后每个控制类都extends这个BaseController,使用日志的时候叫调用logger日志管理类

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python 学习笔记6 读写文件
下一篇:5.9 Linux Vim批量注释和自定义注释
相关文章