我想同时跟踪多个文件并将日志推送到scribe。 我从一个配置文件中读取文件,然后我想跟踪每个文件并将日志发送给scribe。 我尝试的是只为第一个文件发送日志,而不为其他文件发送日志。在
我希望对每个文件同时运行tailing,并同时发送每个文件的日志。在
for l in Config.items('files'):
print l[0]
print l[1]
filename = l[1]
file = open(filename,'r')
st_results = os.stat(l[1])
st_size = st_results[6]
file.seek(st_size)
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
category=l[0]
message=line
log_entry = scribe.LogEntry(category, message)
socket = TSocket.TSocket(host='localhost', port=1463)
transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False)
client = scribe.Client(iprot=protocol, oprot=protocol)
transport.open()
result = client.Log(messages=[log_entry])
transport.close()
试试这个(Inspired by this)
@Pengman's idea的不同实现:
其中
^{pr2}$log_entry()
是问题中代码的副本:follow()
可以使用FS监视工具实现,请参见tail -f in python with no time.sleep。在相关问题 更多 >
编程相关推荐