整理日志
# Conflicts: # src/main/java/com/sunyard/log/SydLogFactory.java # src/main/java/com/sunyard/log/SydLogInfo.java # src/main/java/com/sunyard/log/SydLogger.java # src/main/java/racal/sunyard/main/SydApi4j.java
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package com.sunyard.log;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class SydLogFactory implements ILogFactory {
|
||||
// 日期格式
|
||||
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
// 日志目录
|
||||
public static String pcLogPath = "./sydapi-logs/";
|
||||
|
||||
// 日志名
|
||||
public static final String logName = "sydapi-log";
|
||||
|
||||
// 日志级别
|
||||
public static int iLogLevel = 0;
|
||||
|
||||
// 日志队列
|
||||
private static ConcurrentLinkedQueue<SydLogInfo> queue = new ConcurrentLinkedQueue<SydLogInfo>();
|
||||
|
||||
// 调试标记
|
||||
public static boolean debug = false;
|
||||
|
||||
// 后台打印进程
|
||||
private static Executor executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
static {
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String logFileName = "";
|
||||
FileOutputStream logFos = null;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
|
||||
if (queue.isEmpty()) {
|
||||
// 释放 CPU
|
||||
Thread.sleep(500);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 有日志
|
||||
// 确认日期
|
||||
String mayLogFileName = String.format("%s_%s.log", logName, format.format( new Date()));
|
||||
if ( ! logFileName.equals( mayLogFileName ) ) {
|
||||
logFileName = mayLogFileName;
|
||||
if ( null != logFos ) {
|
||||
logFos.flush();
|
||||
logFos.close();
|
||||
}
|
||||
File folder = new File( pcLogPath );
|
||||
if ( ! folder.exists() ) {
|
||||
folder.mkdirs();
|
||||
}
|
||||
|
||||
File f = new File( pcLogPath, logFileName );
|
||||
if ( ! f.exists() ) {
|
||||
f.createNewFile();
|
||||
}
|
||||
logFos = new FileOutputStream( f , true);
|
||||
}
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
SydLogInfo info = queue.poll();
|
||||
logFos.write(String.format("[%d:%s <%s-%d>] %s\r\n",
|
||||
info.getLevel(),
|
||||
format.format( new Date(info.getDatatime()) ),
|
||||
info.getTname(),
|
||||
info.getTid(),
|
||||
info.getMsg()
|
||||
).getBytes()
|
||||
);
|
||||
}
|
||||
logFos.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
closeFileOutputStream(logFos);
|
||||
logFileName = "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private static void closeFileOutputStream(FileOutputStream fos){
|
||||
if ( null != fos ) {
|
||||
try {
|
||||
fos.flush();
|
||||
}catch (Exception e) {
|
||||
//pass
|
||||
}
|
||||
|
||||
try {
|
||||
fos.close();
|
||||
}catch (Exception e) {
|
||||
//pass
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private SydLogFactory() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ILogger getLogger(Class cls) {
|
||||
return new SydLogger(queue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user