[Java] java 异步日志记录方法 游戏记录日志 →→→→→进入此内容的聊天室

来自 , 2020-12-17, 写在 Java, 查看 148 次.
URL http://www.code666.cn/view/2ab56412
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.concurrent.ConcurrentLinkedQueue;
  6.  
  7. public class GameLogs {
  8.     public final static int insert_item = 0;//
  9.     public final static int update_item = 1;//
  10.     public final static int delete_item = 2;//
  11.  
  12.     public final static int dummys = 3;// 游戏币
  13.     public final static int moneys = 4;// 元宝
  14.     public final static int leCurrency = 5;//
  15.     public final static int pay = 6;// 充值记录
  16.     public final static int bannedList = 7;// 加还检查日志
  17.     public final static int uplv = 8;// 升级日志
  18.     public final static int login_logout = 9;// 登入登出日志
  19.     public final static int other = 10;
  20.     public final static int runScript = 11;// 脚本日志
  21.     public final static int mapSkip = 12;// 用户跳转场景
  22.     public final static int userState = 13;// 用户状态
  23.     public final static int social = 14;// 社交 1好友 2组队 3家族
  24.     public final static int transaction = 15 ; // 交易
  25.     // 强化成功
  26.     // 强化失败
  27.     // 交易
  28.     // 合成
  29.     // 镶嵌
  30.     // 打孔
  31.     // 卖出
  32.     // 购买
  33.     // 杀死后获取
  34.     // 使用
  35.     // 修理
  36.     // 丢弃
  37.     // 装备
  38.     // 卸下
  39.     // 存入
  40.     // 取出
  41.  
  42.     private static final int act_count = 16;
  43.     private static String logsPath = Def.APP_ROOT + "/logs/";
  44.     private static ConcurrentLinkedQueue<Log>[] logsList = new ConcurrentLinkedQueue[act_count];
  45.     static {
  46.         for (int i = 0; i < act_count; i++) {
  47.             logsList[i] = new ConcurrentLinkedQueue();
  48.         }
  49.     }
  50.     private static ConcurrentLinkedQueue<Log> logs = new ConcurrentLinkedQueue<Log>();
  51.  
  52.     /**
  53.      * 增加业务日志
  54.      *
  55.      * @param a
  56.      *            业务动作
  57.      *@param objects
  58.      *@date 2009-6-22
  59.      *@author eric.chan
  60.      */
  61.     public static void logs(int actCount, Object... objects) {
  62.         Log log=logs.poll();
  63.         if(log==null)log=new Log(objects);
  64.         else
  65.             log.setLog(objects);
  66.         switch (actCount) {
  67.         case insert_item://
  68.             logsList[0].offer(log);
  69.             break;
  70.         case update_item://
  71.             logsList[1].offer(log);
  72.             break;
  73.         case delete_item://
  74.             logsList[2].offer(log);
  75.             break;
  76.         case dummys:// 游戏币
  77.             logsList[dummys].offer(log);
  78.             break;
  79.         case moneys:// 元宝
  80.             logsList[moneys].offer(log);
  81.             break;
  82.         case leCurrency://
  83.             logsList[leCurrency].offer(log);
  84.             break;
  85.         case pay:// 充值记录
  86.             logsList[pay].offer(log);
  87.             break;
  88.         case bannedList:// 加检查日志
  89.             logsList[bannedList].offer(log);
  90.             break;
  91.         case uplv:// 升级日志
  92.             logsList[uplv].offer(log);
  93.             break;
  94.         case login_logout:// 登录登出日志
  95.             logsList[login_logout].offer(log);
  96.             break;
  97.         case other:// 其它
  98.             logsList[other].offer(log);
  99.             break;
  100.         case runScript:// 脚本运行日志
  101.             logsList[runScript].offer(log);
  102.             break;
  103.         case mapSkip://场景跳转
  104.             logsList[mapSkip].offer(log);
  105.             break;
  106.         case userState:// 用户状态
  107.             logsList[userState].offer(log);
  108.             break;
  109.         case social://社交 1好友 2组队 3家族
  110.             logsList[social].offer(log);
  111.             break;
  112.         case transaction: // 交易
  113.             logsList[transaction].offer(log);
  114.             break;
  115.         default:
  116.             throw new NullPointerException("not found actCount" + actCount);
  117.         }
  118.     }
  119.  
  120.     public static void logFlushThread() {
  121.         new LogWorker().start();
  122.     }
  123.  
  124.     public static void flush() {
  125.         for (int i = 0; i < act_count; i++) {
  126.             File f = new File(logsPath + DateUtils.getNowDate2() + "/" + i);
  127.             if (!f.exists())
  128.                 f.getParentFile().mkdirs();
  129.             BufferedWriter bw = null;
  130.             try {
  131.                 bw = new BufferedWriter(new FileWriter(f, true));
  132.                 StringBuilder sb = new StringBuilder();
  133.                 Log obj = null;
  134.                 int t = 0;
  135.                 ConcurrentLinkedQueue<Log> clq = logsList[i];
  136.                 while ((obj = clq.poll()) != null) {
  137.                     Object[] objs = obj.objs;
  138.                     sb.setLength(0);
  139.                     for (Object o : objs) {
  140.                         sb.append(o).append('@');
  141.                     }
  142.                     sb.append(obj.date).append('@');
  143.                     sb.append("\\n");
  144.                     bw.write(sb.toString());
  145.                     obj.clear();
  146.                     logs.offer(obj);
  147. //                  sb = null;
  148.                     t++;
  149.                     if (t % 50 == 0) {
  150.                         bw.flush();
  151.                     }
  152.                 }
  153.                 bw.flush();
  154.             } catch (IOException e) {
  155.                 e.printStackTrace();
  156.             } finally {
  157.                 if (bw != null) {
  158.                     try {
  159.                         bw.close();
  160.                     } catch (IOException e) {
  161.                         e.printStackTrace();
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.     }
  167.  
  168.     private static class Log {
  169.         public Object[] objs;
  170.         public String date = DateUtils.getNowDate3();
  171.  
  172.         public Log(Object[] o) {
  173.             objs = o;
  174.         }
  175.  
  176.         public void setLog(Object[] o) {
  177.             objs = o;
  178.             date = DateUtils.getNowDate3();
  179.         }
  180.         public void clear(){
  181.             date=null;
  182.             objs=null;
  183.         }
  184.     }
  185.  
  186.     public static void main(String[] args) {
  187.         logFlushThread();
  188.         for (int i = 0; i < 100000; i++)
  189.             logs(213123, 123, 14, 234, 312, 412, 31, 4, 23, 123, 12, 3);
  190.     }
  191. }
  192.  
  193. class LogWorker extends Thread {
  194.     public void run() {
  195.         while (Config.threadRunFlag) {
  196.             try {
  197.                 GameLogs.flush();
  198.             } catch (Exception e) {
  199.                 e.printStackTrace();
  200.             } finally {
  201.                 try {
  202.                     this.sleep(1000 * 60);
  203.                 } catch (InterruptedException e) {
  204.                     e.printStackTrace();
  205.                 }
  206.             }
  207.         }
  208.     }
  209. }
  210. //该片段来自于http://yuncode.net
  211.  

回复 "java 异步日志记录方法 游戏记录日志"

这儿你可以回复上面这条便签

captcha