From: kaliko Date: Fri, 23 Oct 2015 15:39:52 +0000 (+0200) Subject: Better use of logging X-Git-Tag: 0.1.0~34 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=f2f44c64470d0967913feb1bfb9868fa0c7d9e1b;p=sid.git Better use of logging --- diff --git a/bot.py b/bot.py index 4940900..d283db9 100644 --- a/bot.py +++ b/bot.py @@ -4,7 +4,7 @@ import getpass from sid.sid import MUCBot -from sid.echo import Echo +from sid.ping import Ping from sid.feeds import Feeds JID = 'bot@example.org' @@ -16,6 +16,7 @@ def main(): Feeds.TEMPO = 60 xmpp = MUCBot(JID, PASS, ROOM, NICK) xmpp.register_bot_plugin(Feeds) + xmpp.register_bot_plugin(Ping) # Connect to the XMPP server and start processing XMPP stanzas. if xmpp.connect(): # If you do not have the dnspython library installed, you will need diff --git a/sid/bts.py b/sid/bts.py index efe7033..a19259a 100644 --- a/sid/bts.py +++ b/sid/bts.py @@ -38,7 +38,7 @@ class Bugs(Plugin): return bugs = list() for bug_id in set(Bugs.re_bugs.findall(msg['body'].strip())): - self.log.debug('got bug id: %s' % bug_id) + self.log.debug('got bug id: %s', bug_id) query = debianbts.get_status(bug_id) if len(query) == 1: bug = query[0] diff --git a/sid/echo.py b/sid/echo.py index f0e05b8..94836d0 100644 --- a/sid/echo.py +++ b/sid/echo.py @@ -31,7 +31,7 @@ class Echo(Plugin): bot.add_event_handler("muc::%s::presence" % self.bot.room, self.log_presence) def log_presence(self, pres): - self.log.debug('{0}: {1}'.format(pres['muc']['nick'], pres['type'])) + self.log.debug('%s: %s', pres['muc']['nick'], pres['type']) nick = pres['muc']['nick'] self.presence.update({nick: (pres['muc']['role'], pres['type'])}) self.log.debug(self.presence) @@ -72,7 +72,7 @@ class Echo(Plugin): sender = message['from'].resource recipient = message['body'].split()[1] tell_msg = ' '.join(message['body'].split()[2:]) - self.log.debug('{0}: {1}'.format(recipient, tell_msg)) + self.log.debug('%s: %s', recipient, tell_msg) letter = '{0}, {1} wanted you to know: {2}'.format(recipient, sender, tell_msg) if (self.presence.get(recipient) and self.presence[recipient][1] == 'available'): diff --git a/sid/feeds.py b/sid/feeds.py index 59cf084..a40f766 100644 --- a/sid/feeds.py +++ b/sid/feeds.py @@ -58,23 +58,23 @@ class FeedMonitor(threading.Thread): # Cannot resolve address if 'status' not in parsed_feed: - self.plugin.log.error('Error from "%s": %s.' % - (feed, parsed_feed.bozo_exception.__repr__())) + self.plugin.log.error('Error from "%s": %s.', + feed, parsed_feed.bozo_exception.__repr__()) return # unusual return http code if parsed_feed.status != 200: self.plugin.log.warning( - 'Got code %(status)d from "%(href)s" (please update).' % - parsed_feed) + 'Got code %(status)d from "%(href)s" (please update).', + parsed_feed) return feed_updated = parsed_feed.feed.get('updated_parsed', None) # Avoid looping over all posts if possible if feed_updated and strtm_to_dtm(feed_updated) < self.last_check: - self.plugin.log.debug('updated : %s' % strtm_to_dtm(feed_updated)) - self.plugin.log.debug('last check: %s' % self.last_check) + self.plugin.log.debug('updated : %s', strtm_to_dtm(feed_updated)) + self.plugin.log.debug('last check: %s', self.last_check) return title = '"%s":' % parsed_feed.feed.get('title', 'n/a') @@ -117,7 +117,7 @@ class FeedMonitor(threading.Thread): try: self.new_posts(feed) except Exception as err: - self.plugin.log.error('feeds thread crashed: %s' % err) + self.plugin.log.error('feeds thread crashed: %s', err) self.plugin.log.error(''.join(traceback.format_exc())) self.thread_killed = True self.last_check = datetime.datetime.utcnow() diff --git a/sid/sid.py b/sid/sid.py index 82dcab5..373209d 100644 --- a/sid/sid.py +++ b/sid/sid.py @@ -2,7 +2,7 @@ # Copyright (C) 2007-2012 Thomas Perl # Copyright (C) 2010, 2011 Anaël Verrier -# Copyright (C) 2014 kaliko +# Copyright (C) 2014-2015 kaliko # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -75,7 +75,7 @@ class MUCBot(sleekxmpp.ClientXMPP): for name, value in inspect.getmembers(self): if inspect.ismethod(value) and getattr(value, '_bot_command', False): name = getattr(value, '_bot_command_name') - self.log.debug('Registered command: %s' % name) + self.log.debug('Registered command: %s', name) self.commands[name] = value def __set_logger(self, log_file=None, log_level=logging.INFO): @@ -87,7 +87,7 @@ class MUCBot(sleekxmpp.ClientXMPP): chandler.setFormatter(formatter) self.log.addHandler(chandler) self.log.setLevel(log_level) - self.log.debug('set logger, log level : %s' % log_level) + self.log.debug('set logger, log level : %s', log_level) def message(self, msg): if msg['type'] not in ('groupchat', 'chat'): @@ -99,21 +99,20 @@ class MUCBot(sleekxmpp.ClientXMPP): if not body.startswith(MUCBot.prefix): return if msg['from'] not in self.__seen: - self.log.warning('Will not handle message ' - 'from unseen jid: %s' % msg['from']) + self.log.warning('Will not handle message from unseen jid: %s', msg['from']) #return args = body[1:].split() cmd = args.pop(0) if cmd not in self.commands: return - self.log.debug('cmd: {0}'.format(cmd)) + self.log.debug('cmd: %s', cmd) if args: - self.log.debug('arg: {0}'.format(args)) + self.log.debug('arg: %s', args) try: self.commands[cmd](msg, args) except Exception as err: reply = ''.join(traceback.format_exc()) - self.log.exception('An error occurred processing: {0}: {1}'.format(body, reply)) + self.log.exception('An error occurred processing: %s: %s', body, reply) if self.log.level < 10 and reply: self.send_message(mto=msg['from'].bare, mbody=reply, mtype='groupchat') @@ -149,19 +148,19 @@ class MUCBot(sleekxmpp.ClientXMPP): if inspect.ismethod(value) and getattr(value, '_bot_command', False): name = getattr(value, '_bot_command_name') - self.log.debug('Registered command: %s' % name) + self.log.debug('Registered command: %s', name) self.commands[name] = value def foreach_plugin(self, method, *args, **kwds): for plugin in self.plugins: - self.log.debug('shuting down %s' % plugin.__str__) + self.log.debug('shuting down %s', plugin.__str__) getattr(plugin, method)(*args, **kwds) def shutdown_plugins(self): # TODO: why can't use event session_end|disconnected? self.log.info('shuting down') for plugin in self.plugins: - self.log.debug('shuting down %s' % plugin) + self.log.debug('shuting down %s', plugin) getattr(plugin, 'shutdown')() @botcmd