-
Notifications
You must be signed in to change notification settings - Fork 67
Add structured logger to epidata, and log metadata generation #404
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Structured logger utility for creating JSON logs in Delphi pipelines.""" | ||
| import logging | ||
| import sys | ||
| import threading | ||
| import structlog | ||
|
|
||
|
|
||
| def handle_exceptions(logger): | ||
| """Handle exceptions using the provided logger.""" | ||
| def exception_handler(etype, value, traceback): | ||
| logger.exception("Top-level exception occurred", | ||
| exc_info=(etype, value, traceback)) | ||
|
|
||
| def multithread_exception_handler(args): | ||
| exception_handler(args.exc_type, args.exc_value, args.exc_traceback) | ||
|
|
||
| sys.excepthook = exception_handler | ||
| threading.excepthook = multithread_exception_handler | ||
|
|
||
|
|
||
| def get_structured_logger(name=__name__, | ||
| filename=None, | ||
| log_exceptions=True): | ||
| """Create a new structlog logger. | ||
|
|
||
| Use the logger returned from this in indicator code using the standard | ||
| wrapper calls, e.g.: | ||
|
|
||
| logger = get_structured_logger(__name__) | ||
| logger.warning("Error", type="Signal too low"). | ||
|
|
||
| The output will be rendered as JSON which can easily be consumed by logs | ||
| processors. | ||
|
|
||
| See the structlog documentation for details. | ||
|
|
||
| Parameters | ||
| --------- | ||
| name: Name to use for logger (included in log lines), __name__ from caller | ||
| is a good choice. | ||
| filename: An (optional) file to write log output. | ||
| """ | ||
| # Configure the underlying logging configuration | ||
| handlers = [logging.StreamHandler()] | ||
| if filename: | ||
| handlers.append(logging.FileHandler(filename)) | ||
|
|
||
| logging.basicConfig( | ||
| format="%(message)s", | ||
| level=logging.INFO, | ||
| handlers=handlers | ||
| ) | ||
|
|
||
| # Configure structlog. This uses many of the standard suggestions from | ||
| # the structlog documentation. | ||
| structlog.configure( | ||
| processors=[ | ||
| # Filter out log levels we are not tracking. | ||
| structlog.stdlib.filter_by_level, | ||
| # Include logger name in output. | ||
| structlog.stdlib.add_logger_name, | ||
| # Include log level in output. | ||
| structlog.stdlib.add_log_level, | ||
| # Allow formatting into arguments e.g., logger.info("Hello, %s", | ||
| # name) | ||
| structlog.stdlib.PositionalArgumentsFormatter(), | ||
| # Add timestamps. | ||
| structlog.processors.TimeStamper(fmt="iso"), | ||
| # Match support for exception logging in the standard logger. | ||
| structlog.processors.StackInfoRenderer(), | ||
| structlog.processors.format_exc_info, | ||
| # Decode unicode characters | ||
| structlog.processors.UnicodeDecoder(), | ||
| # Render as JSON | ||
| structlog.processors.JSONRenderer() | ||
| ], | ||
| # Use a dict class for keeping track of data. | ||
| context_class=dict, | ||
| # Use a standard logger for the actual log call. | ||
| logger_factory=structlog.stdlib.LoggerFactory(), | ||
| # Use a standard wrapper class for utilities like log.warning() | ||
| wrapper_class=structlog.stdlib.BoundLogger, | ||
| # Cache the logger | ||
| cache_logger_on_first_use=True, | ||
| ) | ||
|
|
||
| logger = structlog.get_logger(name) | ||
|
|
||
| if log_exceptions: | ||
| handle_exceptions(logger) | ||
|
|
||
| return logger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.