Skip to content

Instantly share code, notes, and snippets.

@toastdriven
Created September 5, 2013 09:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toastdriven/6447761 to your computer and use it in GitHub Desktop.
Save toastdriven/6447761 to your computer and use it in GitHub Desktop.
# For Django, at the bottom of ``settings.py`` after all your other settings...
# Setup a BoundLogger for the project/apps to use...
import structlog
structlog.BoundLogger.configure(
context_class=structlog.ThreadLocalDict(dict),
)
# Then within your views (or other code)...
import logging
from structlog import BoundLogger
log = BoundLogger.wrap(logging.getLogger(__name__))
# Your views here...
@montaro
Copy link

montaro commented Oct 9, 2016

Thanks, But I believe this is outdated now.

@montaro
Copy link

montaro commented Oct 9, 2016

This is working with me with structlog==16.1.0

Anywhere at the bottom of the django settings.py configure the structlog in the way you like, this is an example:

import structlog
structlog.configure(
    processors=[
        structlog.processors.JSONRenderer(sort_keys=True),
    ],
)

And anywhere in your code:

from structlog import wrap_logger
import logging

logger = wrap_logger(logger=logging.getLogger(__name__))
logger.warn('Event1')

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