日志样例:
1068 1529597015396 g60-database-380.i.nease.net /home/g60/gamedata/log/g60_GameStatistic_20180622.log 380_game02 G60_GameStatistic 1529596878_35 [2018-06-22 00:01:18][MercLevelUp],{"merc_capacity":2739,"cur_level":70,"accountid":"aebfr2ualeolqplc@ad.netease.win.163.com","type_id":307,"udid":"66e0b0278fbef358","ip":"101.90.127.224","role_sex":1,"app_channel":"netease","server":"380","role_occ":4,"role_id":"698382","vip":6,"pre_level":69,"guid":6882678726665895936,"role_level":80}
import jsondef parse_log_line(line):try:# 查找JSON部分: 从最后一个 '],' 后面开始json_start = line.rindex('],') + 2json_str = line[json_start:].strip()# 解析JSONdata = json.loads(json_str)# 提取role_id和vip,如果vip不存在则返回0role_id = data.get('role_id', '')vip = data.get('vip', 0)return role_id, vipexcept Exception as e:print(f"Error parsing line: {e}")return '', 0# 测试代码
test_log = '''1068 1529597015396 g60-database-380.i.nease.net /home/g60/gamedata/log/g60_GameStatistic_20180622.log 380_game02 G60_GameStatistic 1529596878_35 [2018-06-22 00:01:18][MercLevelUp],{"merc_capacity":2739,"cur_level":70,"accountid":"aebfr2ualeolqplc@ad.netease.win.163.com","type_id":307,"udid":"66e0b0278fbef358","ip":"101.90.127.224","role_sex":1,"app_channel":"netease","server":"380","role_occ":4,"role_id":"698382","vip":6,"pre_level":69,"guid":6882678726665895936,"role_level":80}'''# 测试示例日志
role_id, vip = parse_log_line(test_log)
print(f"Role ID: {role_id}, VIP: {vip}")# 测试vip缺失的情况
test_log_no_vip = '''1068 1529597015396 g60-database-380.i.nease.net /home/g60/gamedata/log/g60_GameStatistic_20180622.log 380_game02 G60_GameStatistic 1529596878_35 [2018-06-22 00:01:18][MercLevelUp],{"merc_capacity":2739,"cur_level":70,"role_id":"698382","role_level":80}'''role_id, vip = parse_log_line(test_log_no_vip)
print(f"\nTesting log without VIP field:")
print(f"Role ID: {role_id}, VIP: {vip}")
运行结果: