用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java 异步日志记录方法

2015-10-10 作者: java源代码大全举报

[java]代码库

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;

public class GameLogs {
    public final static int insert_item = 0;// 
    public final static int update_item = 1;// 
    public final static int delete_item = 2;//

    public final static int dummys = 3;// 游戏币
    public final static int moneys = 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 other = 10;
    public final static int runScript = 11;// 脚本日志
    public final static int mapSkip = 12;// 用户跳转场景
    public final static int userState = 13;// 用户状态
    public final static int social = 14;// 社交 1好友 2组队 3家族
    public final static int transaction = 15 ; // 交易
    // 强化成功
    // 强化失败
    // 交易
    // 合成
    // 镶嵌
    // 打孔
    // 卖出
    // 购买
    // 杀死后获取
    // 使用
    // 修理
    // 丢弃
    // 装备
    // 卸下
    // 存入
    // 取出

    private static final int act_count = 16;
    private static String logsPath = Def.APP_ROOT + "/logs/";
    private static ConcurrentLinkedQueue<Log>[] logsList = new ConcurrentLinkedQueue[act_count];
    static {
        for (int i = 0; i < act_count; i++) {
            logsList[i] = new ConcurrentLinkedQueue();
        }
    }
    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 insert_item:// 
            logsList[0].offer(log);
            break;
        case update_item:// 
            logsList[1].offer(log);
            break;
        case delete_item:// 
            logsList[2].offer(log);
            break;
        case dummys:// 游戏币
            logsList[dummys].offer(log);
            break;
        case moneys:// 元宝
            logsList[moneys].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 other:// 其它
            logsList[other].offer(log);
            break;
        case runScript:// 脚本运行日志
            logsList[runScript].offer(log);
            break;
        case mapSkip://场景跳转
            logsList[mapSkip].offer(log);
            break;
        case userState:// 用户状态
            logsList[userState].offer(log);
            break;
        case social://社交 1好友 2组队 3家族
            logsList[social].offer(log);
            break;
        case transaction: // 交易
            logsList[transaction].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;
                    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 * 60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...