任务日志取出slf4j依赖

v1.7
xueli.xue 8 years ago
parent b4fd8bf93e
commit 7102a9f86d

@ -40,7 +40,7 @@
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>

@ -31,6 +31,7 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
private int port = 9999;
private String appName;
private RegistHelper registHelper;
public static String logPath;
public void setIp(String ip) {
this.ip = ip;
@ -44,6 +45,9 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
public void setRegistHelper(RegistHelper registHelper) {
this.registHelper = registHelper;
}
public void setLogPath(String logPath) {
this.logPath = logPath;
}
// ---------------------------------- job server ------------------------------------
private NetComServerFactory serverFactory = new NetComServerFactory();

@ -1,12 +1,27 @@
package com.xxl.job.core.handler;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobFileAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* remote job handler
* @author xuxueli 2015-12-19 19:06:38
*/
public abstract class IJobHandler {
private static Logger logger = LoggerFactory.getLogger(IJobHandler.class);
/**
* append log
*
* @param appendLog
*/
public void log(String appendLog) {
String logFileName = XxlJobFileAppender.contextHolder.get();
XxlJobFileAppender.appendLog(logFileName, appendLog);
logger.info("xxl-job log [{}]: {}", logFileName, appendLog);
}
/**
* job handler

@ -1,6 +1,7 @@
package com.xxl.job.core.handler.impl;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.executor.XxlJobExecutor;
import com.xxl.job.core.glue.GlueTypeEnum;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.log.XxlJobFileAppender;
@ -35,17 +36,17 @@ public class ScriptJobHandler extends IJobHandler {
String scriptFileName = null;
if (GlueTypeEnum.GLUE_SHELL == glueType) {
cmd = "bash";
scriptFileName = XxlJobFileAppender.filePath.concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".sh");
scriptFileName = XxlJobExecutor.logPath.concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".sh");
} else if (GlueTypeEnum.GLUE_PYTHON == glueType) {
cmd = "python";
scriptFileName = XxlJobFileAppender.filePath.concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".py");
scriptFileName = XxlJobExecutor.logPath.concat("gluesource/").concat(String.valueOf(jobId)).concat("_").concat(String.valueOf(glueUpdatetime)).concat(".py");
}
// make script file
ScriptUtil.markScriptFile(scriptFileName, gluesource);
// log file
String logFileName = XxlJobFileAppender.filePath.concat(XxlJobFileAppender.contextHolder.get());
String logFileName = XxlJobExecutor.logPath.concat(XxlJobFileAppender.contextHolder.get());
// invoke
int exitValue = ScriptUtil.execToFile(cmd, scriptFileName, logFileName, params);

@ -1,9 +1,9 @@
package com.xxl.job.core.log;
import com.xxl.job.core.biz.model.LogResult;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.LoggingEvent;
import com.xxl.job.core.executor.XxlJobExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.text.SimpleDateFormat;
@ -13,18 +13,13 @@ import java.util.Date;
* store trigger log in each log-file
* @author xuxueli 2016-3-12 19:25:12
*/
public class XxlJobFileAppender extends AppenderSkeleton {
public class XxlJobFileAppender {
private static Logger logger = LoggerFactory.getLogger(XxlJobFileAppender.class);
// for JobThread
public static ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// trogger log file path
public static volatile String filePath;
public void setFilePath(String filePath) {
XxlJobFileAppender.filePath = filePath;
}
/**
* log filename: yyyy-MM-dd/9999.log
*
@ -35,7 +30,7 @@ public class XxlJobFileAppender extends AppenderSkeleton {
public static String makeLogFileName(Date triggerDate, int logId) {
// filePath/
File filePathDir = new File(filePath);
File filePathDir = new File(XxlJobExecutor.logPath);
if (!filePathDir.exists()) {
filePathDir.mkdirs();
}
@ -52,20 +47,31 @@ public class XxlJobFileAppender extends AppenderSkeleton {
return logFileName;
}
@Override
protected void append(LoggingEvent event) {
/**
* append log
*
* @param logFileName
* @param appendLog
*/
public static void appendLog(String logFileName, String appendLog) {
// log
if (appendLog == null) {
appendLog = "";
}
appendLog += "\r\n";
String logFileName = contextHolder.get();
// log file
if (logFileName==null || logFileName.trim().length()==0) {
return;
}
File logFile = new File(filePath, logFileName);
File logFile = new File(XxlJobExecutor.logPath, logFileName);
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
return;
}
}
@ -75,46 +81,26 @@ public class XxlJobFileAppender extends AppenderSkeleton {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(logFile, true);
fos.write(layout.format(event).getBytes("utf-8"));
if (layout.ignoresThrowable()) {
String[] throwableInfo = event.getThrowableStrRep();
if (throwableInfo != null) {
for (int i = 0; i < throwableInfo.length; i++) {
fos.write(throwableInfo[i].getBytes("utf-8"));
fos.write(Layout.LINE_SEP.getBytes("utf-8"));
}
}
}
fos.write(appendLog.getBytes("utf-8"));
fos.flush();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public boolean requiresLayout() {
// TODO Auto-generated method stub
return false;
}
/**
* support read log-file
*
* @param logFileName
* @return log content
*/
@ -124,7 +110,7 @@ public class XxlJobFileAppender extends AppenderSkeleton {
if (logFileName==null || logFileName.trim().length()==0) {
return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
}
File logFile = new File(filePath, logFileName);
File logFile = new File(XxlJobExecutor.logPath, logFileName);
if (!logFile.exists()) {
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
@ -145,13 +131,13 @@ public class XxlJobFileAppender extends AppenderSkeleton {
}
}
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage(), e);
}
}
}
@ -195,7 +181,7 @@ public class XxlJobFileAppender extends AppenderSkeleton {
e.printStackTrace();
}
}
}
}
return null;
}

@ -1,6 +1,6 @@
package com.xxl.job.core.util;
import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.executor.XxlJobExecutor;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
@ -28,7 +28,7 @@ public class ScriptUtil {
*/
public static void markScriptFile(String scriptFileName, String content) throws IOException {
// filePath/
File filePathDir = new File(XxlJobFileAppender.filePath);
File filePathDir = new File(XxlJobExecutor.logPath);
if (!filePathDir.exists()) {
filePathDir.mkdirs();
}

@ -24,13 +24,13 @@ import java.util.concurrent.TimeUnit;
@Service
public class DemoJobHandler extends IJobHandler {
private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class);
@Override
public ReturnT<String> execute(String... params) throws Exception {
logger.info("XXL-JOB, Hello World.");
super.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) {
logger.info("beat at:{}", i);
super.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2);
}
return ReturnT.SUCCESS;

@ -36,6 +36,7 @@
<property name="dataSource" ref="xxlJobDataSource" />
</bean>
</property>
<property name="logPath" value="${xxl.job.executor.logpath}" />
</bean>
<!-- ********************************* "XXL-JOB公共数据源" 配置, 仅在启动 "DbRegistHelper" 时才需要, 否则可删除 ********************************* -->

@ -17,34 +17,11 @@
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} xxl-job-executor-example [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
</layout>
</appender>
<appender name="xxl-job" class="com.xxl.job.core.log.XxlJobFileAppender">
<param name="filePath" value="/data/applogs/xxl-job/jobhandler/"/>
<!--<param name="append" value="true"/>-->
<!--<param name="encoding" value="UTF-8"/>-->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} xxl-job-executor-example [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
</layout>
</appender>
<logger name="com.xxl.job.core" additivity="false">
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="xxl-job"/>
</logger>
<logger name="com.xxl.job.executor.service.jobhandler" additivity="false">
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="xxl-job"/>
</logger>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="xxl-job"/>
</root>
</log4j:configuration>

@ -7,4 +7,5 @@ xxl.job.db.password=root_pwd
### xxl-job executor address
xxl.job.executor.appname=xxl-job-executor-example
xxl.job.executor.ip=
xxl.job.executor.port=9999
xxl.job.executor.port=9999
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/
Loading…
Cancel
Save