日志组件
This commit is contained in:
parent
c6d6a200cc
commit
79c0a318a5
44
src/main/java/com/sunyard/sge/log/LogInfo.java
Normal file
44
src/main/java/com/sunyard/sge/log/LogInfo.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.sunyard.sge.log;
|
||||
|
||||
|
||||
public class LogInfo {
|
||||
private int level;
|
||||
private long datatime;
|
||||
private String tname;
|
||||
private long tid;
|
||||
private String msg;
|
||||
private Object[] objects;
|
||||
|
||||
public LogInfo(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;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,121 @@
|
||||
package com.sunyard.sge.log;
|
||||
|
||||
import com.sunyard.sge.database.Consts;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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 {
|
||||
// 日期格式
|
||||
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
// 金交所定制日志
|
||||
public static final String logName = "sge-hsm-sydapi-log";
|
||||
|
||||
// 日志级别
|
||||
public static int iLogLevel = Consts.LogLevelOFF;
|
||||
|
||||
// 日志目录
|
||||
public static String pcLogPath = "./sydapi-logs/";
|
||||
|
||||
// 日志队列
|
||||
private static ConcurrentLinkedQueue<LogInfo> queue = new ConcurrentLinkedQueue<LogInfo>();
|
||||
|
||||
// 调试标记
|
||||
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()) {
|
||||
LogInfo 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(StandardCharsets.UTF_8)
|
||||
);
|
||||
}
|
||||
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() {
|
||||
}
|
||||
|
||||
public static SydLogger getLogger() {
|
||||
return new SydLogger(queue);
|
||||
}
|
||||
}
|
||||
|
||||
21
src/main/java/com/sunyard/sge/log/SydLogger.java
Normal file
21
src/main/java/com/sunyard/sge/log/SydLogger.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.sunyard.sge.log;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class SydLogger {
|
||||
|
||||
private ConcurrentLinkedQueue<LogInfo> queue;
|
||||
SydLogger(ConcurrentLinkedQueue<LogInfo> queue) {
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void debug(String msg, Object... params){
|
||||
this.queue.add( new LogInfo(3, msg, params) );
|
||||
}
|
||||
public void info(String msg, Object... params){
|
||||
this.queue.add( new LogInfo(2, msg, params) );
|
||||
}
|
||||
public void error(String msg, Object... params){
|
||||
this.queue.add( new LogInfo(1, msg, params) );
|
||||
}
|
||||
}
|
||||
@ -1,14 +1,46 @@
|
||||
package test;
|
||||
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.database.SydApi4Database;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.log.SydLogFactory;
|
||||
import com.sunyard.sge.log.SydLogger;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class LogTest {
|
||||
|
||||
@Test
|
||||
public void case1() throws InterruptedException {
|
||||
int max = 10000;
|
||||
int nThread = 10;
|
||||
Thread[] threads = new Thread[nThread];
|
||||
for (int i = 0; i < nThread; i++) {
|
||||
threads[i] = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SydLogger logger = SydLogFactory.getLogger();
|
||||
for (int i = 0; i < max; i++) {
|
||||
logger.debug("tttttttttttttttt");
|
||||
}
|
||||
}
|
||||
});
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
for (Thread t : threads) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Thread.sleep(10000 );
|
||||
System.out.println("over");
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Logger log = LogFactory.createLogger("./test-log/", 3);
|
||||
log.error("error");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user