Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_random_order(self, event_dict):
rv = KeyValueRenderer()(None, None, event_dict)
assert isinstance(rv, str)
def test_sort_keys(self, event_dict):
"""
Keys are sorted if sort_keys is set.
"""
rv = KeyValueRenderer(sort_keys=True)(None, None, event_dict)
assert r"a= b=[3, 4] x=7 y='test' z=(1, 2)" == rv
def test_order_extra(self, event_dict):
"""
Extra keys get sorted if sort_keys=True.
"""
event_dict["B"] = "B"
event_dict["A"] = "A"
rv = KeyValueRenderer(
key_order=["c", "y", "b", "a", "z", "x"], sort_keys=True
)(None, None, event_dict)
assert (
r"c=None y='test' b=[3, 4] a= z=(1, 2) x=7 A='A' B='B'"
) == rv
def test_order_sorted_missing_dropped(self, event_dict):
"""
Keys get sorted if sort_keys=True and extras get dropped.
"""
event_dict["B"] = "B"
event_dict["A"] = "A"
rv = KeyValueRenderer(
key_order=["c", "y", "b", "a", "z", "x"],
sort_keys=True,
drop_missing=True,
)(None, None, event_dict)
assert r"y='test' b=[3, 4] a= z=(1, 2) x=7 A='A' B='B'" == rv
def test_order_complete(self, event_dict):
assert (
r"y='test' b=[3, 4] a= z=(1, 2) x=7" ==
KeyValueRenderer(key_order=['y', 'b', 'a', 'z', 'x'])
(None, None, event_dict)
)
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"json_formatter": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.JSONRenderer(),
},
"colored": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.dev.ConsoleRenderer(colors=True),
},
"key_value": {
"()": structlog.stdlib.ProcessorFormatter,
"processor": structlog.processors.KeyValueRenderer(
key_order=["timestamp", "level", "event", "logger"]
),
},
},
"handlers": {
"colored_stream": {"class": "logging.StreamHandler", "formatter": "colored"},
"json_file": {
"class": "logging.handlers.WatchedFileHandler",
"filename": "logs/json.log",
"formatter": "json_formatter",
},
"flat_line_file": {
"class": "logging.handlers.WatchedFileHandler",
"filename": "logs/flat_line.log",
"formatter": "key_value",
},
}
logging.config.dictConfig(logging_config)
structlog.configure(
processors=[
contextvars.merge_contextvars,
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt='iso'),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.KeyValueRenderer(
key_order=['timestamp', 'logger', 'level', 'event']
),
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
def override_sanic_loggers():
# Override Sanic loggers with structlog loggers. Unfortunately
# there isn't a great way of doing this because the Sanic modules
# already hold a reference to the logging.Logger at import load,
# so we are stuck with just replacing those references in their
# respective modules.
def main(index, collections, cache_folder, dry_run):
# type: (Index, List[str], str, bool) -> None
# Direct stuctlog into standard logging.
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M.%S"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
# Coloured output if to terminal.
CleanConsoleRenderer() if sys.stdout.isatty() else structlog.processors.KeyValueRenderer(),
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
cache = Path(cache_folder)
for collection_name in collections:
collection = NCI_COLLECTIONS[collection_name]
log = _LOG.bind(collection=collection_name)
collection_cache = cache.joinpath(query_name(collection.query))
fileutils.mkdir_p(str(collection_cache))
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import logbook
import logbook.more
import raven
import raven.handlers.logbook
import structlog
class UnstructuredRenderer(structlog.processors.KeyValueRenderer):
def __call__(self, logger, method_name, event_dict):
event = None
if "event" in event_dict:
event = event_dict.pop("event")
if event_dict or event is None:
# if there are other keys, use the parent class to render them
# and append to the event
rendered = super(UnstructuredRenderer, self).__call__(
logger, method_name, event_dict
)
return f"{event} ({rendered})"
else:
return event
def setup_papertrail(project_name, channel, PAPERTRAIL_HOST, PAPERTRAIL_PORT):
log.info("user logged in", user="test-user")
# gives you:
# event='user logged in' request_id='ffcdc44f-b952-4b5f-95e6-0f1f3a9ee5fd' user='test-user'
# ...
some_function()
# ...
return "logged in!"
if __name__ == "__main__":
logging.basicConfig(
format="%(message)s", stream=sys.stdout, level=logging.INFO
)
structlog.configure(
processors=[
structlog.processors.KeyValueRenderer(
key_order=["event", "request_id"]
)
],
context_class=structlog.threadlocal.wrap_dict(dict),
logger_factory=structlog.stdlib.LoggerFactory(),
)
app.run()