Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should we use structlog? #788

Open
vringar opened this issue Nov 11, 2020 · 1 comment
Open

Should we use structlog? #788

vringar opened this issue Nov 11, 2020 · 1 comment

Comments

@vringar
Copy link
Contributor

vringar commented Nov 11, 2020

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.

@vringar
Copy link
Contributor Author

vringar commented Nov 11, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant