-
Notifications
You must be signed in to change notification settings - Fork 31
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
structlog: add structlog logger factory #75
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import logging | ||
import sys | ||
from logging import StreamHandler | ||
|
||
import structlog | ||
|
||
|
||
class LoggerFactory: | ||
@staticmethod | ||
def configure_logging( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment/question: maybe another way to integrate would be to follow the |
||
logger_name=None, level=logging.INFO, handler=None, processors=None | ||
): | ||
""" | ||
Configures the logging for the specified logger. | ||
:param logger_name (str): The name of the logger. If not provided, a root logger will be used. | ||
:param level (int): The logging level. Defaults to logging.INFO. | ||
:param handler (logging.Handler): The logging handler to use. If not provided, a StreamHandler with stdout will be used. | ||
:param processors (list): The list of structlog processors to apply. If not provided, a default set of processors will be used. | ||
Returns: | ||
structlog.BoundLogger: The configured logger. | ||
""" | ||
if processors is None: | ||
processors = [ | ||
structlog.stdlib.filter_by_level, | ||
structlog.stdlib.add_logger_name, | ||
structlog.stdlib.add_log_level, | ||
structlog.processors.TimeStamper(fmt="iso"), | ||
structlog.processors.JSONRenderer(), | ||
] | ||
|
||
# Create a new logger | ||
logger = logging.getLogger(logger_name) | ||
logger.setLevel(level) | ||
|
||
# Use the provided handler or default to StreamHandler with stdout | ||
if handler is None: | ||
handler = StreamHandler(sys.stdout) | ||
|
||
handler.setFormatter(logging.Formatter("%(message)s")) | ||
|
||
# Add handler to logger if not already added | ||
if not logger.handlers: | ||
logger.addHandler(handler) | ||
|
||
structlog.configure( | ||
processors=processors, | ||
context_class=dict, | ||
logger_factory=structlog.stdlib.LoggerFactory(), | ||
wrapper_class=structlog.stdlib.BoundLogger, | ||
cache_logger_on_first_use=True, | ||
) | ||
return structlog.wrap_logger(logger) | ||
|
||
@staticmethod | ||
def get_logger(name=None, level=logging.INFO, handler=None, processors=None): | ||
""" | ||
Retrieves a configured logger with the specified name. | ||
:param name (str): The name of the logger. If not provided, a root logger will be used. | ||
:param level (int): The logging level. Defaults to logging.INFO. | ||
:param handler (logging.Handler): The logging handler to use. If not provided, a StreamHandler with stdout will be used. | ||
:param processors (list): The list of structlog processors to apply. If not provided, a default set of processors will be used. | ||
Comment on lines
+58
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Do we need both I'm also somewhat skeptical about having docstrings at all at this point, since we're basically documenting what |
||
Returns: | ||
structlog.BoundLogger: The configured logger. | ||
Comment on lines
+67
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be something like |
||
""" | ||
return LoggerFactory.configure_logging(name, level, handler, processors) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear how all this is used, it would be nice to see an example.
It feels like that the class is not needed, but a simple func is enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! Once I push to the vocabularies PR and it's ready to review we will see it there, but basically we can use it by simply doing:
Yes, we could change it to be simply a func that returns the logger if it's better.