
Company News
Socket Joins the OpenJS Foundation
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.
logextractx
Advanced tools
This small humble library is for making logging extra parameters in logging a bit easier.
I have blognote about it.
(look also into docstrings of modules and classes)
Use getLogger from logextractx.logger, and then create local logger with local
context:
from logextractx.logger import getLogger
logger = getLogger(__name__)
[...]
loclogger = logger.local(extra={'DATA_IN': 'CURRENT_CONTEXT'})
Eg:
from logextractx.logger import getLogger
logger = getLogger(__name__)
def send_message(environment: str, requester: str, recipient: str, text: str) -> bool:
""" Function send_message sends MSG to the specified recipient. """
# extra data to be indexed
loclogger = logger.local(extra={'ACTION_TYPE': 'SEND_MSG',
'requester': requester,
'recipient': recipient,
'user': str(request.user),
'environment': env_type})
try:
r = requests.post(settings.MSG_PROVIDER, json={'recipient': recipient, 'content': text},
... < other params > ....)
r.raise_for_status()
except requests.exceptions.RequestException as e:
loclogger.error('Sending MSG failed. Response text: "%s"', e)
loclogger.debug("headers=%r", r.result.headers)
return False
loclogger.info('Sending MSG success.')
return True
To tie all log records with common request-id and session-id, do the following:
logextractx.middleware.LogCtxDjangoMiddleware to your MIDDLEWARE in settings:MIDDLEWARE = [
[...]
'django.contrib.sessions.middleware.SessionMiddleware',
[...]
'logextractx.middleware.LogCtxDjangoMiddleware',
]
And instead of logextractx.logger use logextractx.middleware so:
from logextractx.logger import getLogger
logger = getLogger(__name__)
[...]
Also, you need to add filter into logging
'filters': {
'RidFilter': {
'()': 'logextractx.middleware.RidFilter'
}
}
And that's all. Now every log entry will contain request_id and session_id fields.
If you want to also pass request/session-id to working
Huey/DjHuey by such modifications:
Instead
from huey.contrib.djhuey import db_periodic_task, db_task, task
you should use
from logextractx.djhuey import db_periodic_task, db_task, task
If you do so, then all extra context, including request-id and session-id will be
passed to logger on the Djhuey side.
LogExtraCtx in Flask is quite similar to usage in Django:
from logextractx.flask import init_logctx
from logextractx.middleware import getLogger
[...]
app = flask.Flask(__name__)
app.secret_key = "don't tell anyone"
init_logctx(app)
[...]
logger = getLogger(__name__)
If you use plain logging format, you may be interested in using
logextractx.formatter.ExtraFormatter. Just add following in your formatter definition (DictConfig):
'formatters': {
'simple': {
'()': 'logextractx.formatter.ExtraFormatter',
'fmt': '%(levelname)s %(asctime)s %(name)s: %(message)s [%(extras)s]'
}
}
And then you will have all extra in single log line.
Licensed under the Apache License, Version 2.0
FAQs
LoggerAdapter, which helps you to propagate context information
We found that logextractx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.