public class LogInfo {
/**
* 指定文件名后缀
*/
public String fileSuffix;
/**
* 日志消息
*/
public String message;
public static LogInfo creat(String fileSuffix,String message){
LogInfo logInfo=new LogInfo(fileSuffix,message);
return logInfo;
}
public LogInfo(){
}
public LogInfo(String fileSuffix, String message) {
super();
this.fileSuffix = fileSuffix;
this.message = message;
}
public String getFileSuffix() {
return fileSuffix;
}
public void setFileSuffix(String fileSuffix) {
this.fileSuffix = fileSuffix;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}
import org.apache.log4j.helpers.CountingQuietWriter;
import org.apache.log4j.spi.LoggingEvent;
/**
* 按文件大小切分日志
*
* 按指定的id作为后缀文件名创建文件
* 使用@TaskLogInfo 封装日志参数
* 其他使用方式和log4j提供的api一致
*/
public class RollingFileAppender extends org.apache.log4j.RollingFileAppender{
private long nextRollover = 0;
private String baseFilePattern;
@Override
protected void subAppend(LoggingEvent event) {
Object obj=event.getMessage();
String fileSuffix=null;
if(obj instanceof LogInfo){
LogInfo logInfo=(LogInfo)obj;
fileSuffix=logInfo.getFileSuffix();
}
String scheduled=(baseFilePattern).replace("%s", "");
if(fileSuffix!=null){
scheduled=String.format(baseFilePattern,fileSuffix);
}
setAppend(true);
setFile(scheduled);
activateOptions();
super.subAppend(event);
if(fileName != null && qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
if (size >= maxFileSize && size >= nextRollover) {
rollOver();
}
}
}
@Override
public void activateOptions() {
super.activateOptions();
if(baseFilePattern!=null){
return;
}
int index=fileName.lastIndexOf(".");
if(index>0){
baseFilePattern=fileName.substring(0,index)+"%s"+fileName.substring(index);
}else{
baseFilePattern=fileName+"%s";
}
}
}
public class Logger {
private org.apache.log4j.Logger log4jlogger;
/**
* 服务key=服务名+taskID
*/
private String strServiceKey;
public static Logger getLogger(Class clazz) {
return new Logger(clazz);
}
private Logger(Class clazz){
log4jlogger=org.apache.log4j.Logger.getLogger(clazz);
log4jlogger.setAdditivity(true);
}
private Logger(String name) {
log4jlogger=org.apache.log4j.Logger.getLogger(name);
}
public void info(String message){
log4jlogger.info(LogInfo.creat(strServiceKey,message));
}
public void error(String message) {
log4jlogger.error(LogInfo.creat(strServiceKey,message));
}
public void warn(String message) {
log4jlogger.warn(LogInfo.creat(strServiceKey,message));
}
public void error(String message,Throwable t) {
log4jlogger.error(LogInfo.creat(strServiceKey,message),t);
}
public void warn(String message,Throwable t) {
log4jlogger.warn(LogInfo.creat(strServiceKey,message),t);
}
public void info(String message,Throwable t) {
log4jlogger.info(LogInfo.creat(strServiceKey,message),t);
}
public void setServiceKey(String strServiceKey) {
this.strServiceKey=strServiceKey;
}
}
public class TestLog4j {
public static void main(String[] args) {
//不切分文件
org.apache.log4j.Logger logger2=org.apache.log4j.Logger.getLogger(TestLog4j.class);
for (int i = 0; i < 10; i++) {
logger2.info("info "+i);
logger2.error("error "+i);
logger2.warn("warn "+i);
}
//按id切分文件
Logger logger1=Logger.getLogger(TestLog4j.class);
for (int i = 0; i < 10; i++) {
logger1.setServiceKey(""+i);
logger1.info("info "+i);
logger1.error("error "+i);
logger1.warn("warn "+i);
}
}
}
配置信息
#显示大于INFO级别的日志
log4j.rootLogger=info,FILE
##所有的一般错误信息和INFO信息/logs/xxxxxxx.log 与task调度相关信息按task ID
#此处填写自定义的输出类型 RollingFileAppender
log4j.appender.FILE=com.xxxxxxx.RollingFileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.File=/logs/xxxxxxx.log
log4j.appender.FILE.Threshold=INFO
#最大备份文件个数
log4j.appender.FILE.maxBackupIndex=20
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss:SSS} [%p] - %c -%F(%L) -%m%n
log4j.appender.FILE.MaxFileSize=50MB