Should we use structlog?
Structlog is a structured logging provider. This means instead of taking a string and formatting it to a log message as soon as it enters the system the message and the data are kept separate for as long as possible making it easy to programmatically processing it. It also allows for data bindings, which is a way to attach context to a logger that will be logged for every subsequent log call (until the attribute gets unset). Using this one can build up scopes that give full context on any error happening in the application. E.g. there could be some code in the browser manager that looks like this:
from structlog import get_logger
log = get_logger()
def BrowserManager():
log = log.bind(browser_id=123)
cs = queue.get()
log = log.bind(visit_id=cs.visit_id)
for(command in cs):
log = log.bind(command = command)
log.info("command.executing")
...
This would log
2016-04-20 16:20.13 command.executing browser_id=123, command=GetCommand(sleep=20), visit_id=456
But when we want to send the same event to Sentry we can just take the the event_dict and send it to sentry as data to contextualize what is going on in our application.
One we have this set up we could also think about removing all logging code and just writing to stdout as JSON. This way the GCP Cloud Logging solution would pick up all of our logs and allow us to search through them with a query builder