Ori.livneh has uploaded a new change for review.
https://gerrit.wikimedia.org/r/59372
Change subject: Make logging to file correct and configurable
......................................................................
Make logging to file correct and configurable
* Adds '--logfile' option, which can be used to specify an alternate file to
log to. If missing, will default to the previous behavior of logging to
/var/log/adminbot.log.
* Attach traceback information to log output by using 'logging.exception'.
* Bump version to 1.7.3
Change-Id: I43eb6931a6b8aab1a63c17ba1e1104576008de08
---
M adminlogbot.py
M debian/changelog
2 files changed, 48 insertions(+), 23 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/operations/debs/adminbot
refs/changes/72/59372/1
diff --git a/adminlogbot.py b/adminlogbot.py
index 3bea8a5..e92dc03 100755
--- a/adminlogbot.py
+++ b/adminlogbot.py
@@ -5,6 +5,7 @@
import irclib
import json
import logging
+import logging.handlers
import os
import re
from socket import gethostname
@@ -13,6 +14,17 @@
import urllib
import traceback
+
+
+# stderr logging
+console_handler = logging.StreamHandler()
+console_handler.setFormatter(logging.Formatter('%(asctime)s\t%(message)s'))
+console_handler.setLevel(logging.DEBUG)
+
+log = logging.getLogger(__name__)
+log.setLevel(logging.DEBUG)
+log.addHandler(console_handler)
+
class logbot():
@@ -26,19 +38,19 @@
con.privmsg(self.config.nickserv,
"identify " + self.config.nick + " " +
self.config.nick_password)
except irclib.ServerNotConnectedError, e:
- logging.debug("Error identifying user")
+ log.exception("Error identifying user")
logging.debug("'%s' registering with nick '%s'." %
(self.name, self.config.nick))
time.sleep(1)
for target in self.config.targets:
- logging.debug("'%s' joining '%s'." % (self.name,
target))
+ log.debug("'%s' joining '%s'." % (self.name, target))
con.join(target)
if con.get_nickname() != self.config.nick:
try:
con.privmsg('nickserv', 'ghost %s %s' %
(self.config.nick,
self.config.nick_password))
except irclib.ServerNotConnectedError, e:
- logging.debug("Error ghosting user")
+ log.debug("Error ghosting user")
def on_quit(self, con, event):
source = irclib.nm_to_n(event.source())
@@ -47,12 +59,12 @@
def switch_nick(self, con, event):
con.nick(con.get_nickname() + "_")
- logging.debug("'%s' switching nick." % self.name)
+ log.debug("'%s' switching nick." % self.name)
try:
con.privmsg('nickserv', 'ghost %s %s' %
(self.config.nick,
self.config.nick_password))
except irclib.ServerNotConnectedError, e:
- logging.debug("Error ghosting user")
+ log.debug("Error ghosting user")
def get_cloak(self, source):
if re.search("/", source) and re.search("@", source):
@@ -116,7 +128,7 @@
if (line.startswith(self.config.nick) or
line.startswith("!%s" % self.config.nick) or
line.lower() == "!log help"):
- logging.debug("'%s' got '%s'; displaying help message."
% (self.name, line))
+ log.debug("'%s' got '%s'; displaying help message." %
(self.name, line))
try:
self.server.privmsg(event.target(),
"I am a logbot running on %s."
% gethostname())
@@ -129,9 +141,9 @@
self.server.privmsg(event.target(),
"To log a message, type
!log <msg>.")
except irclib.ServerNotConnectedError, e:
- logging.debug("Server connection error
when sending message")
+ log.debug("Server connection error when
sending message")
elif line.lower().startswith("!log "):
- logging.debug("'%s' got '%s'; Attempting to log." %
(self.name, line))
+ log.debug("'%s' got '%s'; Attempting to log." %
(self.name, line))
if self.config.check_users:
try:
cache_filename =
'%s/%s-users_json.cache' % (self.config.cachedir, self.name)
@@ -170,7 +182,7 @@
" to the trust list or
your user page.")
return
except irclib.ServerNotConnectedError,
e:
- logging.debug("Server
connection error when sending message")
+ log.debug("Server connection
error when sending message")
if self.config.enable_projects:
arr = line.split(" ", 2)
try:
@@ -183,7 +195,7 @@
"Message
missing. Nothing logged.")
return
except irclib.ServerNotConnectedError, e:
- logging.debug("Server connection error
when sending message")
+ log.debug("Server connection error when
sending message")
project = arr[1]
try:
cache_filename =
'%s/%s-users_json.cache' % (self.config.cachedir, self.name)
@@ -216,13 +228,13 @@
self.server.privmsg(event.target(),
"Error
reading project list from LDAP.")
except
irclib.ServerNotConnectedError, e:
- logging.debug("Server
connection error when sending message")
+ log.debug("Server
connection error when sending message")
if project not in projects:
try:
self.server.privmsg(event.target(),
project + " is
not a valid project.")
except irclib.ServerNotConnectedError,
e:
- logging.debug("Server
connection error when sending message")
+ log.debug("Server connection
error when sending message")
return
message = arr[2]
else:
@@ -231,7 +243,7 @@
try:
self.server.privmsg(event.target(), "Message missing. Nothing logged.")
except irclib.ServerNotConnectedError,
e:
- logging.debug("Server
connection error when sending message")
+ log.debug("Server connection
error when sending message")
return
project = ""
message = arr[1]
@@ -244,10 +256,9 @@
try:
self.server.privmsg(event.target(),
"Logged the message, %s" % title)
except irclib.ServerNotConnectedError, e:
- logging.debug("Server connection error
when sending message")
+ log.debug("Server connection error when
sending message")
except Exception:
- traceback.print_exc()
- logging.warning(sys.exc_info)
+ log.exception('Failed to handle message')
def connect(self):
self.server = self.irc.server()
@@ -269,7 +280,15 @@
parser = argparse.ArgumentParser(description='IRC log bot.',
epilog='When run without args it will enumerate bot configs in
/etc/adminbot.')
parser.add_argument('--config', dest='confarg', type=str, help='config file
that describes a single logbot')
+parser.add_argument('--logfile', help='write log to this file (default:
/var/log/adminbot.log)',
+ default='/var/log/adminbot.log')
args = parser.parse_args()
+
+# configure logging to a file
+logfile_handler = logging.handlers.WatchedFileHandler(filename=args.logfile,
encoding='utf8')
+logfile_handler.setLevel(logging.DEBUG)
+log.addHandler(logfile_handler)
+
bots = []
enable_projects = False
@@ -283,14 +302,13 @@
# discard if this isn't actually a bot config file
if not 'targets' in conf.__dict__:
- logging.error("%s does not appear to be a valid bot config." %
args.confarg)
+ log.error("%s does not appear to be a valid bot config." %
args.confarg)
exit(1)
if ('enable_projects' in conf.__dict__) and conf.enable_projects:
enable_projects = True
bots.append(logbot(module, conf))
- logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
else:
# Enumerate bot configs in /etc/adminbot;
# Create a logbot object for each.
@@ -311,10 +329,9 @@
if ('enable_projects' in conf.__dict__) and
conf.enable_projects:
enable_projects = True
- logging.basicConfig(filename="/var/log/adminbot.log",
level=logging.DEBUG)
if not bots:
- logging.error("No config files found, so nothing to do.")
+ log.error("No config files found, so nothing to do.")
sys.exit(1)
if enable_projects:
@@ -324,7 +341,7 @@
import ldapsupportlib
for bot in bots:
- logging.debug("'%s' starting" % bot.name)
+ log.debug("'%s' starting" % bot.name)
bot.connect()
while True:
@@ -333,5 +350,4 @@
try:
bot.irc.process_once()
except:
- traceback.print_exc()
- logging.warning(sys.exc_info)
+ log.exception("Error in bot's main loop")
diff --git a/debian/changelog b/debian/changelog
index a2829d7..952b4f6 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,12 @@
+adminbot (1.7.3) lucid-wikimedia; urgency=low
+
+ * Adds '--logfile' option, which can be used to specify an alternate file to
+ log to. If missing, will default to the previous behavior of logging to
+ /var/log/adminbot.log.
+ * Attach traceback information to log output by using 'logging.exception'.
+
+ -- Ori Livneh <[email protected]> Tue, 16 April 2013 03:53:15 +0000
+
adminbot (1.7.2) lucid-wikimedia; urgency=low
* Added configurable cachedir
--
To view, visit https://gerrit.wikimedia.org/r/59372
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I43eb6931a6b8aab1a63c17ba1e1104576008de08
Gerrit-PatchSet: 1
Gerrit-Project: operations/debs/adminbot
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits