6

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'])

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

H20
  • 115
  • 1
  • 2
  • 6

1 Answers1

10

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

Simple example:

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")

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.

codeape
  • 97,830
  • 24
  • 159
  • 188
  • thanks for the quick response, I have seen that but to be honest with you im struggeling to udnerstand it. could you give me an example maybe? I couldnt see how i could get my results into the log and send it to a syslog server – H20 Jan 23 '12 at 11:51
  • So you want to log to both a file and to a syslog server? In that case add two handlers. – codeape Jan 23 '12 at 12:00
  • thank you for pointing out the SysLogHandler ill take a closer look from there, can you let me know how would i get my results into the log? for instance logging.warn('url') – H20 Jan 23 '12 at 12:19
  • this seems to be a bit more useable http://csl.sublevel3.org/py-syslog-win32/ but i still dont understand how i pass myresults to the logs – H20 Jan 23 '12 at 13:02
  • The example above puts "Hello world" in my syslog file. What is your scenario, networkwise? Are you on a windows box and want to log messages to a linux/unix syslog? In that case, make sure that the syslog server listens to the default syslog UDP port. Consult the OS documentation for instructions on how to do that. – codeape Jan 23 '12 at 13:16
  • ok think ive got the loggin down now but i still dont know how to get my 'url' results into the logs? – H20 Jan 23 '12 at 13:16
  • 1
    codeape im trying to get the results from (my original post) into the syslog server( remote) from a windows box – H20 Jan 23 '12 at 13:17
  • i believe this code will work for the remote syslog but as mentioned im not sure how to get i['url'] results into the logs for i in results: print i['url'] logger = logging.getLogger() logger.setLevel(logging.INFO) syslog = SysLogHandler(address=('x.x.x.x', SYSLOG_UDP_PORT) formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s') syslog.setFormatter(formatter) logger.addHandler(syslog) – H20 Jan 23 '12 at 13:19
  • Simply include the URL in the call to ``logging.warn``: ``logging.warn("Likely hit: " + i["url"])`` – codeape Jan 23 '12 at 13:59
  • ARRr i was trying that earlier but could get the syntx correct i guess – H20 Jan 23 '12 at 14:03
  • logger = logging.getLogger() logger.addHandler(SysLogHandler(address = 'x.x.x.x')) logger.addHandler(logging.FileHandler("hits.log")) logging.warn("likley Hit: " + i['url']) – H20 Jan 23 '12 at 14:15
  • does this work for you logger = logging.getLogger() logger.addHandler(SysLogHandler(address=('192.168.0.2', 514)) logger.addHandler(logging.FileHandler("hits.log")) logging.warn("likley Hit: " + i['url']) – H20 Jan 23 '12 at 14:47
  • YAY got it logging but its only logging the last line from the results any idea? – H20 Jan 23 '12 at 14:54
  • well its kinda working now, loging but not loging as expected – H20 Jan 23 '12 at 19:28
  • 1
    Your three lines of logging setup code should not be inside the loop. Move it outside the loop so it's only executed once. – codeape Jan 23 '12 at 19:45