|
|
@@ -1,9 +1,9 @@
|
|
1
|
1
|
package com.xxl.job.core.log;
|
|
2
|
2
|
|
|
3
|
3
|
import com.xxl.job.core.biz.model.LogResult;
|
|
4
|
|
-import org.apache.log4j.AppenderSkeleton;
|
|
5
|
|
-import org.apache.log4j.Layout;
|
|
6
|
|
-import org.apache.log4j.spi.LoggingEvent;
|
|
|
4
|
+import com.xxl.job.core.executor.XxlJobExecutor;
|
|
|
5
|
+import org.slf4j.Logger;
|
|
|
6
|
+import org.slf4j.LoggerFactory;
|
|
7
|
7
|
|
|
8
|
8
|
import java.io.*;
|
|
9
|
9
|
import java.text.SimpleDateFormat;
|
|
|
@@ -13,18 +13,13 @@ import java.util.Date;
|
|
13
|
13
|
* store trigger log in each log-file
|
|
14
|
14
|
* @author xuxueli 2016-3-12 19:25:12
|
|
15
|
15
|
*/
|
|
16
|
|
-public class XxlJobFileAppender extends AppenderSkeleton {
|
|
|
16
|
+public class XxlJobFileAppender {
|
|
|
17
|
+ private static Logger logger = LoggerFactory.getLogger(XxlJobFileAppender.class);
|
|
17
|
18
|
|
|
18
|
19
|
// for JobThread
|
|
19
|
20
|
public static ThreadLocal<String> contextHolder = new ThreadLocal<String>();
|
|
20
|
21
|
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
21
|
22
|
|
|
22
|
|
- // trogger log file path
|
|
23
|
|
- public static volatile String filePath;
|
|
24
|
|
- public void setFilePath(String filePath) {
|
|
25
|
|
- XxlJobFileAppender.filePath = filePath;
|
|
26
|
|
- }
|
|
27
|
|
-
|
|
28
|
23
|
/**
|
|
29
|
24
|
* log filename: yyyy-MM-dd/9999.log
|
|
30
|
25
|
*
|
|
|
@@ -35,7 +30,7 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
35
|
30
|
public static String makeLogFileName(Date triggerDate, int logId) {
|
|
36
|
31
|
|
|
37
|
32
|
// filePath/
|
|
38
|
|
- File filePathDir = new File(filePath);
|
|
|
33
|
+ File filePathDir = new File(XxlJobExecutor.logPath);
|
|
39
|
34
|
if (!filePathDir.exists()) {
|
|
40
|
35
|
filePathDir.mkdirs();
|
|
41
|
36
|
}
|
|
|
@@ -52,20 +47,31 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
52
|
47
|
return logFileName;
|
|
53
|
48
|
}
|
|
54
|
49
|
|
|
55
|
|
- @Override
|
|
56
|
|
- protected void append(LoggingEvent event) {
|
|
|
50
|
+ /**
|
|
|
51
|
+ * append log
|
|
|
52
|
+ *
|
|
|
53
|
+ * @param logFileName
|
|
|
54
|
+ * @param appendLog
|
|
|
55
|
+ */
|
|
|
56
|
+ public static void appendLog(String logFileName, String appendLog) {
|
|
|
57
|
+
|
|
|
58
|
+ // log
|
|
|
59
|
+ if (appendLog == null) {
|
|
|
60
|
+ appendLog = "";
|
|
|
61
|
+ }
|
|
|
62
|
+ appendLog += "\r\n";
|
|
57
|
63
|
|
|
58
|
|
- String logFileName = contextHolder.get();
|
|
|
64
|
+ // log file
|
|
59
|
65
|
if (logFileName==null || logFileName.trim().length()==0) {
|
|
60
|
66
|
return;
|
|
61
|
67
|
}
|
|
62
|
|
- File logFile = new File(filePath, logFileName);
|
|
|
68
|
+ File logFile = new File(XxlJobExecutor.logPath, logFileName);
|
|
63
|
69
|
|
|
64
|
70
|
if (!logFile.exists()) {
|
|
65
|
71
|
try {
|
|
66
|
72
|
logFile.createNewFile();
|
|
67
|
73
|
} catch (IOException e) {
|
|
68
|
|
- e.printStackTrace();
|
|
|
74
|
+ logger.error(e.getMessage(), e);
|
|
69
|
75
|
return;
|
|
70
|
76
|
}
|
|
71
|
77
|
}
|
|
|
@@ -75,46 +81,26 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
75
|
81
|
FileOutputStream fos = null;
|
|
76
|
82
|
try {
|
|
77
|
83
|
fos = new FileOutputStream(logFile, true);
|
|
78
|
|
- fos.write(layout.format(event).getBytes("utf-8"));
|
|
79
|
|
- if (layout.ignoresThrowable()) {
|
|
80
|
|
- String[] throwableInfo = event.getThrowableStrRep();
|
|
81
|
|
- if (throwableInfo != null) {
|
|
82
|
|
- for (int i = 0; i < throwableInfo.length; i++) {
|
|
83
|
|
- fos.write(throwableInfo[i].getBytes("utf-8"));
|
|
84
|
|
- fos.write(Layout.LINE_SEP.getBytes("utf-8"));
|
|
85
|
|
- }
|
|
86
|
|
- }
|
|
87
|
|
- }
|
|
|
84
|
+ fos.write(appendLog.getBytes("utf-8"));
|
|
88
|
85
|
fos.flush();
|
|
89
|
86
|
} finally {
|
|
90
|
87
|
if (fos != null) {
|
|
91
|
88
|
try {
|
|
92
|
89
|
fos.close();
|
|
93
|
90
|
} catch (IOException e) {
|
|
94
|
|
- e.printStackTrace();
|
|
|
91
|
+ logger.error(e.getMessage(), e);
|
|
95
|
92
|
}
|
|
96
|
93
|
}
|
|
97
|
94
|
}
|
|
98
|
95
|
} catch (Exception e) {
|
|
99
|
|
- e.printStackTrace();
|
|
|
96
|
+ logger.error(e.getMessage(), e);
|
|
100
|
97
|
}
|
|
101
|
98
|
|
|
102
|
99
|
}
|
|
103
|
100
|
|
|
104
|
|
- @Override
|
|
105
|
|
- public void close() {
|
|
106
|
|
- // TODO Auto-generated method stub
|
|
107
|
|
-
|
|
108
|
|
- }
|
|
109
|
|
-
|
|
110
|
|
- @Override
|
|
111
|
|
- public boolean requiresLayout() {
|
|
112
|
|
- // TODO Auto-generated method stub
|
|
113
|
|
- return false;
|
|
114
|
|
- }
|
|
115
|
|
-
|
|
116
|
101
|
/**
|
|
117
|
102
|
* support read log-file
|
|
|
103
|
+ *
|
|
118
|
104
|
* @param logFileName
|
|
119
|
105
|
* @return log content
|
|
120
|
106
|
*/
|
|
|
@@ -124,7 +110,7 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
124
|
110
|
if (logFileName==null || logFileName.trim().length()==0) {
|
|
125
|
111
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
|
|
126
|
112
|
}
|
|
127
|
|
- File logFile = new File(filePath, logFileName);
|
|
|
113
|
+ File logFile = new File(XxlJobExecutor.logPath, logFileName);
|
|
128
|
114
|
|
|
129
|
115
|
if (!logFile.exists()) {
|
|
130
|
116
|
return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
|
|
|
@@ -145,13 +131,13 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
145
|
131
|
}
|
|
146
|
132
|
}
|
|
147
|
133
|
} catch (IOException e) {
|
|
148
|
|
- e.printStackTrace();
|
|
|
134
|
+ logger.error(e.getMessage(), e);
|
|
149
|
135
|
} finally {
|
|
150
|
136
|
if (reader != null) {
|
|
151
|
137
|
try {
|
|
152
|
138
|
reader.close();
|
|
153
|
139
|
} catch (IOException e) {
|
|
154
|
|
- e.printStackTrace();
|
|
|
140
|
+ logger.error(e.getMessage(), e);
|
|
155
|
141
|
}
|
|
156
|
142
|
}
|
|
157
|
143
|
}
|
|
|
@@ -195,7 +181,7 @@ public class XxlJobFileAppender extends AppenderSkeleton {
|
|
195
|
181
|
e.printStackTrace();
|
|
196
|
182
|
}
|
|
197
|
183
|
}
|
|
198
|
|
- }
|
|
|
184
|
+ }
|
|
199
|
185
|
return null;
|
|
200
|
186
|
}
|
|
201
|
187
|
|