Commit 960cb854 authored by Brian Moe's avatar Brian Moe
Browse files

Added Leo's convenience classes for logging.

parent 0ca2e27f
"""
Some convenience logging classes courtesy of Leo Singer, provided as is.
Usage:
import logging
import ligo.gracedb.rest
import ligo.gracedb.logger
logging.basicConfig()
log = logging.getLogger('testing')
gracedb = ligo.gracedb.rest.GraceDb()
graceid = 'T62829'
log.addHandler(ligo.gracedb.logger.GraceDbLogHandler(gracedb, graceid))
# The following will create a log entry on the gracedb server
# (if the log level permits)
#
log.warn("this is a warning")
"""
import logging
class GraceDbLogStream(object):
def __init__(self, gracedb, graceid):
self.gracedb = gracedb
self.graceid = graceid
def flush(self):
pass
def write(self, text):
self.gracedb.writeLog(self.graceid, text)
class GraceDbLogFormatter(logging.Formatter):
def __init__(self):
logging.Formatter.__init__(self, logging.BASIC_FORMAT)
def format(self, record):
s = logging.Formatter.format(self, record)
return '<div style="white-space:pre-wrap">' + s.strip("\n") + '</div>'
class GraceDbLogHandler(logging.StreamHandler):
def __init__(self, gracedb, graceid):
stream = GraceDbLogStream(gracedb, graceid)
logging.StreamHandler.__init__(self, stream)
self.formatter = GraceDbLogFormatter()
......@@ -194,6 +194,25 @@ class TestGracedb(unittest.TestCase):
r = gracedb.writeFile(eventId, uploadFile)
self.assertEqual(r.status, 201) # CREATED
def test_logger(self):
import logging
import ligo.gracedb.rest
import ligo.gracedb.logger
logging.basicConfig()
log = logging.getLogger('testing')
log.propagate = False # Don't write to console
#gracedb = ligo.gracedb.rest.GraceDb()
graceid = eventId
log.addHandler(ligo.gracedb.logger.GraceDbLogHandler(gracedb, graceid))
message = "Message is {0}".format(random.random())
log.warn(message)
event_logs = gracedb.logs(graceid).read()
self.assertTrue(message in event_logs)
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment