整理日志
# 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:
parent
d7fe309b08
commit
d0ea067870
5
src/main/java/com/sunyard/log/ILogFactory.java
Normal file
5
src/main/java/com/sunyard/log/ILogFactory.java
Normal file
@ -0,0 +1,5 @@
|
||||
package com.sunyard.log;
|
||||
|
||||
public interface ILogFactory {
|
||||
ILogger getLogger(Class cls);
|
||||
}
|
||||
11
src/main/java/com/sunyard/log/ILogger.java
Normal file
11
src/main/java/com/sunyard/log/ILogger.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.sunyard.log;
|
||||
|
||||
public interface ILogger {
|
||||
|
||||
public int getCurrentLogLevel();
|
||||
|
||||
public void error(String msg, Object ...params);
|
||||
public void info(String msg, Object ...params);
|
||||
public void debug(String msg, Object ...params);
|
||||
|
||||
}
|
||||
118
src/main/java/com/sunyard/log/SydLogFactory.java
Normal file
118
src/main/java/com/sunyard/log/SydLogFactory.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
44
src/main/java/com/sunyard/log/SydLogInfo.java
Normal file
44
src/main/java/com/sunyard/log/SydLogInfo.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.sunyard.log;
|
||||
|
||||
|
||||
public class SydLogInfo {
|
||||
private int level;
|
||||
private long datatime;
|
||||
private String tname;
|
||||
private long tid;
|
||||
private String msg;
|
||||
private Object[] objects;
|
||||
|
||||
public SydLogInfo(int level, String msg, Object[] objects) {
|
||||
this.level = level;
|
||||
this.msg = msg;
|
||||
this.objects = objects;
|
||||
this.datatime = System.currentTimeMillis();
|
||||
this.tname = Thread.currentThread().getName();
|
||||
this.tid = Thread.currentThread().getId();
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public long getDatatime() {
|
||||
return datatime;
|
||||
}
|
||||
|
||||
public String getTname() {
|
||||
return tname;
|
||||
}
|
||||
|
||||
public long getTid() {
|
||||
return tid;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public Object[] getObjects() {
|
||||
return objects;
|
||||
}
|
||||
}
|
||||
42
src/main/java/com/sunyard/log/SydLogger.java
Normal file
42
src/main/java/com/sunyard/log/SydLogger.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.sunyard.log;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class SydLogger implements ILogger{
|
||||
|
||||
private ConcurrentLinkedQueue<SydLogInfo> queue;
|
||||
|
||||
SydLogger(ConcurrentLinkedQueue<SydLogInfo> queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void debug(String msg, Object... params) {
|
||||
if (SydLogFactory.iLogLevel == 3) {
|
||||
this.queue.add(new SydLogInfo(3, msg, params));
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void info(String msg, Object... params) {
|
||||
if (SydLogFactory.iLogLevel >= 2) {
|
||||
this.queue.add(new SydLogInfo(2, msg, params));
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void error(String msg, Object... params) {
|
||||
if (SydLogFactory.iLogLevel >= 1) {
|
||||
this.queue.add(new SydLogInfo(1, msg, params));
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCurrentLogLevel() {
|
||||
return SydLogFactory.iLogLevel;
|
||||
}
|
||||
|
||||
}
|
||||
@ -8,6 +8,8 @@ import com.sunyard.SydApiException;
|
||||
import com.sunyard.cert.X509;
|
||||
import com.sunyard.constant.CertUsage;
|
||||
import com.sunyard.entity.Struct;
|
||||
import com.sunyard.log.ILogFactory;
|
||||
import com.sunyard.log.ILogger;
|
||||
import com.sunyard.proto.Packet;
|
||||
import com.sunyard.proto.PacketSection;
|
||||
import com.sunyard.proto.Util;
|
||||
@ -76,11 +78,16 @@ public class SydApi4j implements SydApi {
|
||||
}
|
||||
}
|
||||
|
||||
private static void delta() {
|
||||
long m = System.currentTimeMillis();
|
||||
System.out.print("delta \t");
|
||||
System.out.println(m - sTime);
|
||||
sTime = m;
|
||||
private static ILogFactory logFactory = null;
|
||||
private static ILogger logger = null;
|
||||
|
||||
public static ILogFactory getLogFactory() {
|
||||
return logFactory;
|
||||
}
|
||||
|
||||
public static void setLogFactory(ILogFactory logFactory) {
|
||||
SydApi4j.logFactory = logFactory;
|
||||
SydApi4j.logger = logFactory.getLogger( SydApi4j.class );
|
||||
}
|
||||
|
||||
|
||||
@ -1119,14 +1126,18 @@ public class SydApi4j implements SydApi {
|
||||
if (debug) {
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
if ( null != SydApi4j.logger ) {
|
||||
SydApi4j.logger.debug( "{}", i );
|
||||
}
|
||||
}
|
||||
|
||||
private static void print(Object i) {
|
||||
if (debug) {
|
||||
System.out.print(i);
|
||||
}
|
||||
|
||||
if ( null != SydApi4j.logger ) {
|
||||
SydApi4j.logger.debug("{}", i );
|
||||
}
|
||||
}
|
||||
|
||||
//gc回收
|
||||
|
||||
Loading…
Reference in New Issue
Block a user