代码拉取完成,页面将自动刷新
同步操作将从 sumdas/log_server 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
##################################
# logserver
# 通过socket接收并存储日志信息
# 注:测试程序为test_logserver.py
# 修改时间:2013-11-22
###################################
# standard lib
import os, sys
import time
import twisted
import logging
import traceback
import struct
import cPickle # use cPickle for speed
from logging.handlers import TimedRotatingFileHandler
from twisted.internet.protocol import Protocol, Factory
# our own code
import settings
from log_record import initlog
# 根据logger name生成一个
def gen_log_name(name):
name = name.replace('.', '_')
return name + '.log'
class ReceiveProtocol(Protocol):
HEADER_LENGTH = 4
def __init__(self):
self.buf = ''
self.state = 'init'
self.require_length = ReceiveProtocol.HEADER_LENGTH
# 记录当前完成初始化的logger名称
self.logger_dict = {}
def connectionMade(self):
logger = logging.getLogger('log_server')
logger.info(u'[makeConnection]与logger_client建立socket连接。')
def dataReceived(self, data):
logger = logging.getLogger('log_server')
try:
if len(self.buf + data) >= self.require_length:
self.buf = self.buf + data
# 数据就绪进行相应动作
data = self.buf[0:self.require_length]
# 把数据从缓冲区中取走
self.buf = self.buf[self.require_length:]
self.solve(data)
# 可能一次读到了多条日志记录
if self.buf:
self.dataReceived('')
else:
self.buf += data
except BaseException, e:
logger.error(u'处理日志记录失败' + str(e) + '\n' + traceback.format_exc())
def solve(self, data):
logger = logging.getLogger('log_server')
statehandler = None
try:
pto = 'proto_' + self.state
statehandler = getattr(self, pto)
except AttributeError:
logger.error('callback', self.state, 'not found')
self.transport.loseConnection()
statehandler(data)
if self.state == 'done':
self.transport.loseConnection()
def connectionLost(self, reason):
logger = logging.getLogger('log_server')
logger.info(u'[connectionLost]与logger_client的socket连接关闭。')
# 记录日志,对日志进行分拣
def proto_record(self, data):
print 'proto_record'
logRecord = logging.makeLogRecord(cPickle.loads(data))
# 此处进行规则判断
print 'AAA:', logRecord.name, logRecord.msg
if logRecord.name not in self.logger_dict:
logger = initlog(logRecord.name, settings.PATH, gen_log_name(logRecord.name), logLevel=logging.DEBUG)
self.logger_dict[logRecord.name] = logger
self.logger_dict[logRecord.name].handle(logRecord)
# 修改下一步动作以及所需长度
self.state = 'init'
self.require_length = ReceiveProtocol.HEADER_LENGTH
# 处理头部信息
def proto_init(self, data):
length = struct.unpack('!I', data[0:ReceiveProtocol.HEADER_LENGTH])[0]
# 修改下一步动作以及所需长度
self.state = 'record'
self.require_length = length
if len(self.buf) >= self.require_length:
data = self.buf[0:self.require_length]
# 把数据从缓冲区中取走
self.buf = self.buf[self.require_length:]
self.solve(data)
class ReceiveFactory(Factory):
def buildProtocol(self, addr):
return ReceiveProtocol()
def main():
print 'logserver has started.'
logger = logging.getLogger('log_server')
logger.info('logserver has started.')
from twisted.internet import epollreactor
epollreactor.install()
reactor = twisted.internet.reactor
reactor.listenTCP(settings.PORT, ReceiveFactory())
reactor.run()
if __name__ == '__main__':
main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。