CDR话单工具在变态的RIA多线程下无法实现日志记录,于是只能采用曲线救国日志工具来搞定,本来logback很好用,但是和log4j杂糅起来问题很大,于是干掉logback,采用log4j

历史原因:原来项目采用log4j,所以只好在log4j下搞定

目标,按照小时生成文件,同时按照需求写到不同文件

如此一下只是配置问题

首先查看log4j.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
log4j.rootLogger=INFO,R,stdout
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p [%F:%L] %c{8}.%M() - %m%n
 
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=f:/logs/ria.log
log4j.appender.R.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p [%F:%L] %c{8}.%M() - %m%n
 
log4j.logger.S = info,S
log4j.appender.S=org.apache.log4j.DailyRollingFileAppender
log4j.appender.S.File=f:/log/client_uservisit
log4j.appender.S.DatePattern='_'yyyyMMddHH'_0000.log'
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%m%n

同时在需要的class取用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.sxtemobi.cdr;
 
import org.slf4j.LoggerFactory;
 
public class Cdr {
	 static org.slf4j.Logger logger =LoggerFactory.getLogger("R");   
	 static org.slf4j.Logger logge1 =LoggerFactory.getLogger("S");
	public static void main(String[] args) {
		for(int i = 0;i<100;i++){
			logger.error("test log4j");
			logge1.error("==test log4j==");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

如此以来,轻松实现。