游戏异步日志记录代码
import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.concurrent.ConcurrentLinkedQueue;
import engine.Config;
/**
* 游戏日志记录
* @author javagg.com
*
*/public class GameLogs {
/**
* 产生物品游戏世界产出,放入背包仓库容器.首字段产出途径 怪物掉落=Slay 组队获取=TeamSlay 合成获取=Composition
* 商店购买=Shop 抽取删除=equipExtractDel
*/
//合成物品=CompositionAdd 分解获取=FenJieAdd 邮件提取=MailAdd
public final static int add_item = 0; // 使用物品(使用 合成 交易 寄卖)背包使用=BagUse 战斗使用=AttackUse 合成使用=CompositionUse
// 抽取属性项=equipExtract 精炼=equipRefine 锻造=equipstrong
public final static int use_item = 1; // 背包丢弃=BagDel 战斗死亡掉落=AttackDel 合成物品=CompositionDel 抽取属性项=equipExtractDel
// 精炼删除=refinedel 锻造删除=strongdel 邮件删除=maildel
public final static int del_item = 2;// 删除 丢弃
public final static int moneys = 3;// 银两
public final static int rmb = 4;// 人民币
public final static int leCurrency = 5;// 积分
public final static int pay = 6;// 充值记录
public final static int bannedList = 7;// 加速检查日志
public final static int uplv = 8;// 升级日志
public final static int login_logout = 9;// 登入登出日志
public final static int runScript = 10;// 脚本日志
public final static int mapSkip = 11;// 用户跳转场景
public final static int userState = 12;// 用户状态
private static final int act_count = 40; private static String logsPath = null; private static ConcurrentLinkedQueue<Log>[] logsList = new ConcurrentLinkedQueue[act_count];
public static void init() { for (int i = 0; i < act_count; i++) {
logsList[i] = new ConcurrentLinkedQueue();
}
String[] str = Config.getRootPath().split("/");
str[str.length-1] = "GameLog";
logsPath = "/"; for (String s : str) {
logsPath = logsPath + s + "/";
}
logsPath = logsPath + "/log_" + Config._serverName + "/"; if(!(new File(logsPath).exists()))
logsPath = Config.getRootPath() + "/log_" + Config._serverName + "/";
System.out.println("logsPath:" + logsPath);
}
private static ConcurrentLinkedQueue<Log> logs = new ConcurrentLinkedQueue<Log>();
/**
* 增加业务日志
*
* @param a 业务动作
*@param objects
*@date 2009-6-22
*@author eric.chan
*/
public static void logs(int actCount, Object... objects) {
Log log = logs.poll(); if (log == null)
log = new Log(objects); else
log.setLog(objects); switch (actCount) { case add_item://
logsList[0].offer(log); break; case use_item://
logsList[1].offer(log); break; case del_item://
logsList[2].offer(log); break; case moneys:// 银两
logsList[moneys].offer(log); break; case rmb:// 元宝
logsList[rmb].offer(log); break; case leCurrency:// 乐子
logsList[leCurrency].offer(log); break; case pay:// 充值记录
logsList[pay].offer(log); break; case bannedList:// 加检查日志
logsList[bannedList].offer(log); break; case uplv:// 升级日志
logsList[uplv].offer(log); break; case login_logout:// 登录登出日志
logsList[login_logout].offer(log); break; case runScript:// 脚本运行日志
logsList[runScript].offer(log); break; case mapSkip:// 场景跳转
logsList[mapSkip].offer(log); break; case userState:// 用户状态
logsList[userState].offer(log); break; default: throw new NullPointerException("not found actCount" + actCount);
}
}
public static void logFlushThread() { new LogWorker().start();
}
public static void flush() { for (int i = 0; i < act_count; i++) {
File f = new File(logsPath + DateUtils.getNowDate2() + "/" + i); if (!f.exists())
f.getParentFile().mkdirs();
BufferedWriter bw = null; try {
bw = new BufferedWriter(new FileWriter(f, true));
StringBuilder sb = new StringBuilder();
Log obj = null; int t = 0;
ConcurrentLinkedQueue<Log> clq = logsList[i]; while ((obj = clq.poll()) != null) {
Object[] objs = obj.objs; if(objs==null)continue;
sb.setLength(0); for (Object o : objs) {
sb.append(o).append('@');
}
sb.append(obj.date).append('@');
sb.append('\n');
bw.write(sb.toString());
obj.clear();
logs.offer(obj); // sb = null;
t++; if (t % 50 == 0) {
bw.flush();
}
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally { if (bw != null) { try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private static class Log {
public Object[] objs; public String date = DateUtils.getNowDate3();
public Log(Object[] o) {
objs = o;
}
public void setLog(Object[] o) {
objs = o;
date = DateUtils.getNowDate3();
}
public void clear() {
date = null;
objs = null;
}
}
public static void main(String[] args) {
logFlushThread(); for (int i = 0; i < 100000; i++)
logs(213123, 123, 14, 234, 312, 412, 31, 4, 23, 123, 12, 3);
}
}
class LogWorker extends Thread { public void run() { while (Config.threadRunFlag) { try {
GameLogs.flush();
} catch (Exception e) {
e.printStackTrace();
} finally { try { this.sleep(1000 * 30);//30秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}