Python 应用程序输出到系统日志服务器 [英] Python App ouput to syslog server

查看:38
本文介绍了Python 应用程序输出到系统日志服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 google 上进行一些搜索(每 5 分钟左右循环一次).当它受到攻击时,我希望它将结果推送到系统日志服务器.我对 python 很陌生,所以请原谅我的无知,我已经搜索了很长时间,但找不到我的问题的答案.

I'm trying to do some searching on google (looped for every 5 min or so). When it gets a hit I want it to push the results to a syslog server. I'm very new to python so please forgive the ignorance, I have searched for ages and can't find an answer to my question.

我打算添加多个查询以根据日志事件不同的查询结果寻找不同的结果.

I intend to add multiple queries looking for diffirent results depending on the query results the logevent differs.

WARN "possible hit"
CRITICAL "definatly a hit"
etc

我希望输出例如:日志类型、网址、日期/时间

I would like the output to be for example like: log type, url, date/time

以下是我迄今为止一直在玩的代码.我可以搜索并登录到一个文件,但不是我想要的.我只得到时间和偶数类型的格式,我没有在日志中得到我的查询结果.而且我不知道如何登录到系统日志服务器.

Below is the code I've been playing with so far. I can search and log to a file but not how I would like to. I'm only getting the formatting for time and the even type, I'm not getting my query results in the log. And i have no idea how to log to a syslog server.

#!/usr/bin/python
import urllib
import simplejson, logging

query = urllib.urlencode({'q' : 'SEARCHTERMHERE'})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' \ % (query)
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
results = json['responseData']['results']
for i in results:
  logging.basicConfig(format='%(asctime)s %(message)s', filename='hits.log')
  logging.warning ('Likley hit')
  print i['url']

#!/usr/bin/python
import urllib
import simplejson 
import logging
from logging.handlers import SysLogHandler

query = urllib.urlencode({'q' : 'SEARCHTERMHERE'})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' \
% (query)

search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
results = json['responseData']['results']

for i in results:
  print i['url']
  logger = logging.getLogger()
  logger.addHandler(SysLogHandler(address=('192.168.0.2', 514), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
  logger.addHandler(logging.FileHandler("hits.log"))
  logging.warn("likley Hit: " + i['url'])

我得到:文件gog.py",第 18 行logger.addHandler(logging.FileHandler("hits.log"))^语法错误:无效语法

I get : File "gog.py", line 18 logger.addHandler(logging.FileHandler("hits.log")) ^ SyntaxError: invalid syntax

推荐答案

您可以将日志模块配置为输出到 syslog,请参阅 http://docs.python.org/library/logging.handlers.html#sysloghandler

You can configure the logging module to output to syslog, see http://docs.python.org/library/logging.handlers.html#sysloghandler

简单例子:

from logging.handlers import SysLogHandler
import logging

logger = logging.getLogger()
logger.addHandler(SysLogHandler('/dev/log'))
logger.addHandler(logging.FileHandler("filename.log"))

logging.warn("Hello world")

以上使用 Unix 域套接字记录到本地系统日志.您还可以指定主机名以使用 UDP 登录到系统日志.有关详细信息,请参阅文档.

The above logs to the local syslog using a Unix domain socket. You can also specify a hostname to log to syslog using UDP. See the docs for more info.

这篇关于Python 应用程序输出到系统日志服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆