parent
ee9eca0db7
commit
39470eabbd
@ -0,0 +1,7 @@
|
||||
<body style="color:white;background-color:black;" >
|
||||
<pre>
|
||||
<br>
|
||||
<#if result.code == 200>${result.content}
|
||||
<#else>${result.msg}</#if>
|
||||
</pre>
|
||||
</body>
|
@ -1,10 +0,0 @@
|
||||
log4j.rootLogger=info,console
|
||||
|
||||
log4j.appender.console=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.console.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.console.layout.ConversionPattern=%d - xxl-job-client-demo - %p [%c] - <%m>%n
|
||||
|
||||
log4j.appender.logFile=org.apache.log4j.DailyRollingFileAppender
|
||||
log4j.appender.logFile.File=${catalina.base}/logs/xxl-job-client-demo.log
|
||||
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.logFile.layout.ConversionPattern=%d - xxl-job-client-demo - %p [%c] - <%m>%n
|
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="null" debug="null">
|
||||
|
||||
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out" />
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%t]-[%M]-[%L]-[%p] %m%n" />
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<appender name="ROOT" class="org.apache.log4j.DailyRollingFileAppender">
|
||||
<param name="file" value="/logs/xxl-job-client-demo.log"/>
|
||||
<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} [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<appender name="xxl-job" class="com.xxl.job.client.log.XxlJobFileAppender">
|
||||
<param name="filePath" value="/logs/xxl-job/"/>
|
||||
<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} [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="com.xxl.job.service.handler" additivity="false">
|
||||
<level value="INFO" />
|
||||
<appender-ref ref="xxl-job"/>
|
||||
</logger>
|
||||
<logger name="com.xxl.job.client" additivity="false">
|
||||
<level value="INFO" />
|
||||
<appender-ref ref="xxl-job"/>
|
||||
</logger>
|
||||
|
||||
<root>
|
||||
<level value="INFO" />
|
||||
<appender-ref ref="CONSOLE" />
|
||||
<appender-ref ref="ROOT" />
|
||||
<appender-ref ref="xxl-job"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
@ -0,0 +1,180 @@
|
||||
package com.xxl.job.client.log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.log4j.AppenderSkeleton;
|
||||
import org.apache.log4j.Layout;
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
|
||||
/**
|
||||
* store trigger log in each log-file
|
||||
* @author xuxueli 2016-3-12 19:25:12
|
||||
*/
|
||||
public class XxlJobFileAppender extends AppenderSkeleton {
|
||||
|
||||
// for HandlerThread
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(LoggingEvent event) {
|
||||
String trigger_log_id = contextHolder.get();
|
||||
if (trigger_log_id==null || trigger_log_id.trim().length()==0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// filePath/
|
||||
File filePathDir = new File(filePath);
|
||||
if (!filePathDir.exists()) {
|
||||
filePathDir.mkdirs();
|
||||
}
|
||||
|
||||
// filePath/yyyy-MM-dd/
|
||||
String nowFormat = sdf.format(new Date());
|
||||
File filePathDateDir = new File(filePathDir, nowFormat);
|
||||
if (!filePathDateDir.exists()) {
|
||||
filePathDateDir.mkdirs();
|
||||
}
|
||||
|
||||
// filePath/yyyy-MM-dd/9999.log
|
||||
String logFileName = trigger_log_id.concat(".log");
|
||||
File logFile = new File(filePathDateDir, logFileName);
|
||||
if (!logFile.exists()) {
|
||||
try {
|
||||
logFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// append file content
|
||||
try {
|
||||
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.flush();
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@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 triggerDate
|
||||
* @param trigger_log_id
|
||||
* @return
|
||||
*/
|
||||
public static String readLog(Date triggerDate, String trigger_log_id ){
|
||||
if (triggerDate==null || trigger_log_id==null || trigger_log_id.trim().length()==0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// filePath/
|
||||
File filePathDir = new File(filePath);
|
||||
if (!filePathDir.exists()) {
|
||||
filePathDir.mkdirs();
|
||||
}
|
||||
|
||||
// filePath/yyyy-MM-dd/
|
||||
String nowFormat = sdf.format(triggerDate);
|
||||
File filePathDateDir = new File(filePathDir, nowFormat);
|
||||
if (!filePathDateDir.exists()) {
|
||||
filePathDateDir.mkdirs();
|
||||
}
|
||||
|
||||
// filePath/yyyy-MM-dd/9999.log
|
||||
String logFileName = trigger_log_id.concat(".log");
|
||||
File logFile = new File(filePathDateDir, logFileName);
|
||||
if (!logFile.exists()) {
|
||||
try {
|
||||
logFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
InputStream ins = null;
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
ins = new FileInputStream(logFile);
|
||||
reader = new BufferedReader(new InputStreamReader(ins, "utf-8"));
|
||||
if (reader != null) {
|
||||
String content = null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((content = reader.readLine()) != null) {
|
||||
sb.append(content).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
} finally {
|
||||
if (ins != null) {
|
||||
try {
|
||||
ins.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue