XxlJobFileAppender.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.xxl.job.client.log;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import org.apache.log4j.AppenderSkeleton;
  12. import org.apache.log4j.Layout;
  13. import org.apache.log4j.spi.LoggingEvent;
  14. /**
  15. * store trigger log in each log-file
  16. * @author xuxueli 2016-3-12 19:25:12
  17. */
  18. public class XxlJobFileAppender extends AppenderSkeleton {
  19. // for HandlerThread
  20. public static ThreadLocal<String> contextHolder = new ThreadLocal<String>();
  21. public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  22. // trogger log file path
  23. public static volatile String filePath;
  24. public void setFilePath(String filePath) {
  25. XxlJobFileAppender.filePath = filePath;
  26. }
  27. @Override
  28. protected void append(LoggingEvent event) {
  29. String trigger_log_id = contextHolder.get();
  30. if (trigger_log_id==null || trigger_log_id.trim().length()==0) {
  31. return;
  32. }
  33. // filePath/
  34. File filePathDir = new File(filePath);
  35. if (!filePathDir.exists()) {
  36. filePathDir.mkdirs();
  37. }
  38. // filePath/yyyy-MM-dd/
  39. String nowFormat = sdf.format(new Date());
  40. File filePathDateDir = new File(filePathDir, nowFormat);
  41. if (!filePathDateDir.exists()) {
  42. filePathDateDir.mkdirs();
  43. }
  44. // filePath/yyyy-MM-dd/9999.log
  45. String logFileName = trigger_log_id.concat(".log");
  46. File logFile = new File(filePathDateDir, logFileName);
  47. if (!logFile.exists()) {
  48. try {
  49. logFile.createNewFile();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. return;
  53. }
  54. }
  55. // append file content
  56. try {
  57. FileOutputStream fos = null;
  58. try {
  59. fos = new FileOutputStream(logFile, true);
  60. fos.write(layout.format(event).getBytes("utf-8"));
  61. if (layout.ignoresThrowable()) {
  62. String[] throwableInfo = event.getThrowableStrRep();
  63. if (throwableInfo != null) {
  64. for (int i = 0; i < throwableInfo.length; i++) {
  65. fos.write(throwableInfo[i].getBytes("utf-8"));
  66. fos.write(Layout.LINE_SEP.getBytes("utf-8"));
  67. }
  68. }
  69. }
  70. fos.flush();
  71. } finally {
  72. if (fos != null) {
  73. try {
  74. fos.close();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. }
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. @Override
  85. public void close() {
  86. // TODO Auto-generated method stub
  87. }
  88. @Override
  89. public boolean requiresLayout() {
  90. // TODO Auto-generated method stub
  91. return false;
  92. }
  93. /**
  94. * support read log-file
  95. * @param triggerDate
  96. * @param trigger_log_id
  97. * @return
  98. */
  99. public static String readLog(Date triggerDate, String trigger_log_id ){
  100. if (triggerDate==null || trigger_log_id==null || trigger_log_id.trim().length()==0) {
  101. return null;
  102. }
  103. // filePath/
  104. File filePathDir = new File(filePath);
  105. if (!filePathDir.exists()) {
  106. filePathDir.mkdirs();
  107. }
  108. // filePath/yyyy-MM-dd/
  109. String nowFormat = sdf.format(triggerDate);
  110. File filePathDateDir = new File(filePathDir, nowFormat);
  111. if (!filePathDateDir.exists()) {
  112. filePathDateDir.mkdirs();
  113. }
  114. // filePath/yyyy-MM-dd/9999.log
  115. String logFileName = trigger_log_id.concat(".log");
  116. File logFile = new File(filePathDateDir, logFileName);
  117. if (!logFile.exists()) {
  118. try {
  119. logFile.createNewFile();
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. return null;
  123. }
  124. }
  125. try {
  126. InputStream ins = null;
  127. BufferedReader reader = null;
  128. try {
  129. ins = new FileInputStream(logFile);
  130. reader = new BufferedReader(new InputStreamReader(ins, "utf-8"));
  131. if (reader != null) {
  132. String content = null;
  133. StringBuilder sb = new StringBuilder();
  134. while ((content = reader.readLine()) != null) {
  135. sb.append(content).append("\n");
  136. }
  137. return sb.toString();
  138. }
  139. } finally {
  140. if (ins != null) {
  141. try {
  142. ins.close();
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. }
  146. }
  147. if (reader != null) {
  148. try {
  149. reader.close();
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. }
  158. return null;
  159. }
  160. }