log4j 记录日志
项目中有用到log4j 记录日志,在此记录下,废话不说,上代码。
log4j 与spring集成,在web.xml中配置如下:
<context-param> <param-name>webAppRootKey</param-name> <param-value>ahepc.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- log4j每隔60秒扫描 一下配置文件的变化 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener </listener-class> </listener>
配置文件 log4j.properties
# # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The five logging levels used by Log are (in order): # # 1. DEBUG (the least serious) # 2. INFO # 3. WARN # 4. ERROR # 5. FATAL (the most serious) # Set root logger level to WARN and append to stdout log4j.rootLogger=DEBUG,CONSOLE,A1 #log4j.rootLogger=DEBUG,CONSOLE #继承 log4j.addivity.org.apache=false #输出到控制台 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p [%c{1}] %m%n #log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG,CONSOLE log4j.logger.java.sql.ResultSet =DEBUG #按文件大小生成 log4j.appender.A1=org.apache.log4j.RollingFileAppender #log4j.appender.logfile=org.apache.log4j.FileAppender #log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender # 按日期生成配合DatePattern log4j.appender.A1.MaxFileSize=20MB log4j.appender.A1.MaxBackupIndex=20 log4j.appender.A1.File=${ahepc.root}/WEB-INF/logs/ahepc.log #log4j.appender.A1.DatePattern=.yyyy-MM-dd log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d %-5p [%c{1}] %m%n log4j.logger.com.sgcc.ahepc=INFO,DATABASE #数据库配置 #log4j.appender.DATABASE.Threshold=INFO log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender log4j.appender.DATABASE.BufferSize=1 #log4j.appender.DATABASE.URL=jdbc:oracle:thin:@localhost:1521:orcl log4j.appender.DATABASE.URL=jdbc:oracle:thin:@192.168.1.99:1521:orcl2 log4j.appender.DATABASE.driver=oracle.jdbc.driver.OracleDriver log4j.appender.DATABASE.user=PMP log4j.appender.DATABASE.password=PMP_123* log4j.appender.DATABASE.sql=insert into PMP_COMMON_LOGGING (log_id,user_id,remote_addr,access_url,cls,method,log_level,log_msg,created_dt) values (sys_guid(),'%X{user_id}','%X{remote_addr}','%X{access_url}','%C','%M','%p','%m',sysdate) log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
配置文件说明下:log4j可以生成多种格式的log日志,这里配置了最常用的三种,分别是控制台,文件和数据库,
log4j 有5中输出级别。
文件的输出,常有安日期时间每天生成,也可以安装文件大小生成,还可以定义按文件大小生成时,可以生成多少个备份文件,很少牛逼。
log4j.logger.com.sgcc.ahepc=INFO,DATABASE
这句指定 只有在com.sgcc.ahepc包目录下才存入数据库,并且级别是info,只有这个包结构下的类书写了info级别的信息能存入数据
private static Logger logger = Logger.getLogger(LoginController.class); MDC.put("user_id", user.getObj_id()); MDC.put("remote_addr", request.getRemoteHost()); MDC.put("access_url", request.getRequestURL()); logger.info("登录成功");
上面的代码不完全,就是在类的头部声明log对象,在需要写log的地方输出对应级别的信息。
需要说明的是MDC类,这个类是log4j提供的类,和map的应用基本一致。存入键值对。在sql语句中可以用‘%X{user_id}%’获得。这里就用这样的方式获得登陆人登陆的时间地点等一系列信息
相关推荐
chw0 2020-11-04
大唐帝国前营 2020-08-18
sdaq 2020-07-26
MrLiar 2020-07-07
sdaq 2020-06-16
CXC0 2020-06-14
丨Fanny丨Cri 2020-06-13
CXC0 2020-06-08
dongxurr 2020-06-07
sdaq 2020-06-06
MrLiar 2020-06-04
丨Fanny丨Cri 2020-06-03
MrLiar 2020-05-25
丨Fanny丨Cri 2020-05-17
MrLiar 2020-05-14
MrLiar 2020-05-12
sdaq 2020-05-11