the-algorithm-ml/ml_logging/absl_logging.py
2023-09-13 11:22:13 +05:30

44 lines
1.3 KiB
Python

"""Sets up logging through absl for training usage.
- Redirects logging to sys.stdout so that severity levels in GCP Stackdriver are accurate.
Usage:
>>> from twitter.ml.logging.absl_logging import logging
>>> logging.info(f"Properly logged as INFO level in GCP Stackdriver.")
"""
import logging as py_logging
import sys
from absl import logging as logging
def setup_absl_logging():
"""
Configure absl-py logging to direct log messages to stdout and apply a custom log message format.
This function ensures that log messages generated by the absl-py library are written to stdout
rather than stderr. It also applies a custom log message format that includes module, function,
line number, log level, and the log message content.
Note:
This function should be called once at the beginning of your script or application to
configure absl-py logging.
Example:
To use this function, simply call it at the start of your script:
```
setup_absl_logging()
```
"""
logging.get_absl_handler().python_handler.stream = sys.stdout
formatter = py_logging.Formatter(
fmt="[%(module)s.%(funcName)s:%(lineno)s - %(levelname)s] %(message)s"
)
logging.get_absl_handler().setFormatter(formatter)
logging.set_verbosity(logging.INFO)
setup_absl_logging()