1 Star 0 Fork 4

幽兰/log_server

forked from sumdas/log_server 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
socket_logserver.py 3.99 KB
一键复制 编辑 原始数据 按行查看 历史
sumdas 提交于 2017-09-10 18:51 . 日志并发测试完成
#!/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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/yunhang2020/log_server.git
git@gitee.com:yunhang2020/log_server.git
yunhang2020
log_server
log_server
master

搜索帮助