任务日志取出slf4j依赖

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

@ -173,9 +173,7 @@
<textarea class="glueSource_java" style="display:none;" > <textarea class="glueSource_java" style="display:none;" >
package com.xxl.job.service.handler; package com.xxl.job.service.handler;
import org.slf4j.Logger; import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.LoggerFactory;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.IJobHandler;
@ -184,7 +182,7 @@ public class DemoGlueJobHandler extends IJobHandler {
@Override @Override
public ReturnT<String> execute(String... params) throws Exception { public ReturnT<String> execute(String... params) throws Exception {
logger.info("XXL-JOB, Hello World."); XxlJobLogger.log("XXL-JOB, Hello World.");
return ReturnT.SUCCESS; return ReturnT.SUCCESS;
} }

@ -1,27 +1,12 @@
package com.xxl.job.core.handler; package com.xxl.job.core.handler;
import com.xxl.job.core.biz.model.ReturnT; 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 * remote job handler
* @author xuxueli 2015-12-19 19:06:38 * @author xuxueli 2015-12-19 19:06:38
*/ */
public abstract class IJobHandler { 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 * job handler

@ -2,15 +2,13 @@ package com.xxl.job.core.handler.impl;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.IJobHandler;
import org.slf4j.Logger; import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.LoggerFactory;
/** /**
* glue job handler * glue job handler
* @author xuxueli 2016-5-19 21:05:45 * @author xuxueli 2016-5-19 21:05:45
*/ */
public class GlueJobHandler extends IJobHandler { public class GlueJobHandler extends IJobHandler {
private static Logger logger = LoggerFactory.getLogger(GlueJobHandler.class);
private long glueUpdatetime; private long glueUpdatetime;
private IJobHandler jobHandler; private IJobHandler jobHandler;
@ -24,7 +22,7 @@ public class GlueJobHandler extends IJobHandler {
@Override @Override
public ReturnT<String> execute(String... params) throws Exception { public ReturnT<String> execute(String... params) throws Exception {
logger.info("----------- glue.version:{} -----------", glueUpdatetime); XxlJobLogger.log("----------- glue.version:{} -----------" + glueUpdatetime);
return jobHandler.execute(params); return jobHandler.execute(params);
} }

@ -0,0 +1,43 @@
package com.xxl.job.core.log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by xuxueli on 17/4/28.
*/
public class XxlJobLogger {
private static Logger logger = LoggerFactory.getLogger("xxl-job logger");
private static SimpleDateFormat xxlJobLoggerFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* append log
*
* @param appendLog
*/
public static void log(String appendLog) {
// "yyyy-MM-dd HH:mm:ss [ClassName]-[MethodName]-[LineNumber]-[ThreadName] log";
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();
StackTraceElement callInfo = stackTraceElements[1];
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(xxlJobLoggerFormat.format(new Date())).append(" ")
.append("["+ callInfo.getClassName() +"]").append("-")
.append("["+ callInfo.getMethodName() +"]").append("-")
.append("["+ callInfo.getLineNumber() +"]").append("-")
.append("["+ Thread.currentThread().getName() +"]").append(" ")
.append(appendLog!=null?appendLog:"");
String formatAppendLog = stringBuffer.toString();
// appendlog
String logFileName = XxlJobFileAppender.contextHolder.get();
XxlJobFileAppender.appendLog(logFileName, formatAppendLog);
logger.warn("[{}]: {}", logFileName, formatAppendLog);
}
}

@ -5,6 +5,7 @@ import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.biz.model.TriggerParam; import com.xxl.job.core.biz.model.TriggerParam;
import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.log.XxlJobFileAppender; import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.log.XxlJobLogger;
import org.eclipse.jetty.util.ConcurrentHashSet; import org.eclipse.jetty.util.ConcurrentHashSet;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -80,7 +81,7 @@ public class JobThread extends Thread{
String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId()); String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
XxlJobFileAppender.contextHolder.set(logFileName); XxlJobFileAppender.contextHolder.set(logFileName);
logger.info("----------- xxl-job job execute start -----------"); XxlJobLogger.log("----------- xxl-job job execute start -----------");
executeResult = handler.execute(handlerParams); executeResult = handler.execute(handlerParams);
if (executeResult == null) { if (executeResult == null) {
@ -88,7 +89,7 @@ public class JobThread extends Thread{
} }
} catch (Exception e) { } catch (Exception e) {
if (toStop) { if (toStop) {
logger.error("<br>----------- xxl-job toStop, stopReason:{}", stopReason); XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
} }
logger.error("JobThread Exception:", e); logger.error("JobThread Exception:", e);
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
@ -96,8 +97,9 @@ public class JobThread extends Thread{
executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, out.toString()); executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, out.toString());
} }
logger.info("----------- xxl-job job execute end ----------- <br> Look : ExecutorParams:{}, Code:{}, Msg:{}",
new Object[]{handlerParams, executeResult.getCode(), executeResult.getMsg()}); XxlJobLogger.log("----------- xxl-job job execute end ----------- <br> " +
"Look : ExecutorParams:"+ handlerParams +", Code:"+ executeResult.getCode() +", Msg:" + executeResult.getMsg());
// callback handler info // callback handler info
if (!toStop) { if (!toStop) {
@ -111,9 +113,16 @@ public class JobThread extends Thread{
} }
} catch (Exception e) { } catch (Exception e) {
if (toStop) { if (toStop) {
logger.error("<br>----------- xxl-job toStop, stopReason:{}", stopReason); XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
} }
logger.error("----------- xxl-job JobThread Exception:", e);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter, true);
e.printStackTrace(printWriter);
printWriter.flush();
stringWriter.flush();
String errorMsg = stringWriter.toString();
XxlJobLogger.log("----------- xxl-job JobThread Exception:" + errorMsg);
} }
} }

@ -3,8 +3,7 @@ package com.xxl.job.executor.service.jobhandler;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler; import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander; import com.xxl.job.core.handler.annotation.JobHander;
import org.slf4j.Logger; import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -17,20 +16,20 @@ import java.util.concurrent.TimeUnit;
* 1 IJobHandler * 1 IJobHandler
* 2Spring @Service * 2Spring @Service
* 3 @JobHander valueJobKey;JobKey; * 3 @JobHander valueJobKey;JobKey;
* 4 "XxlJobLogger.log"
* *
* @author xuxueli 2015-12-19 19:43:36 * @author xuxueli 2015-12-19 19:43:36
*/ */
@JobHander(value="demoJobHandler") @JobHander(value="demoJobHandler")
@Service @Service
public class DemoJobHandler extends IJobHandler { public class DemoJobHandler extends IJobHandler {
private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class);
@Override @Override
public ReturnT<String> execute(String... params) throws Exception { public ReturnT<String> execute(String... params) throws Exception {
super.log("XXL-JOB, Hello World."); XxlJobLogger.log("XXL-JOB, Hello World.");
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
super.log("beat at:" + i); XxlJobLogger.log("beat at:" + i);
TimeUnit.SECONDS.sleep(2); TimeUnit.SECONDS.sleep(2);
} }
return ReturnT.SUCCESS; return ReturnT.SUCCESS;

Loading…
Cancel
Save