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

structlog: add structlog logger factory #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions invenio_logging/structlog.py
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:

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.

Copy link
Author

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:

stream_logger = LoggerFactory.get_logger("datastreams-" + stream)
stream_logger.info("Starting processing")
stream_logger.error(...)
stream_logger.exception(...)

Yes, we could change it to be simply a func that returns the logger if it's better.

@staticmethod
def configure_logging(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment/question: maybe another way to integrate would be to follow the structlog guides for specific frameworks like e.g. Flask and Celery. Especially for Celery that will really allow us to track individual tasks/jobs (though we'll need some central attribute/key to be able to group things logically).

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Do we need both get_logger and configure_logging, since this one just passes around the same parameters to the other?

I'm also somewhat skeptical about having docstrings at all at this point, since we're basically documenting what structlog accepts, in which case I would just point to the structlog docs.

Returns:
structlog.BoundLogger: The configured logger.
Comment on lines +67 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be something like :returns: ... and :rtype: ....

"""
return LoggerFactory.configure_logging(name, level, handler, processors)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ zip_safe = False
install_requires =
invenio-celery>=1.2.4
invenio-db>=1.0.12
structlog>=24.2.0

[options.extras_require]
tests =
Expand Down