How to redirect all error messages to logger

Viewed 187

I want to capture all error messages and warnings that are triggered from a library (in my case pytorch_lightning) in a log file. My current setup only captures some of the warnings, but I would like to capture all the warnings/errors that PyTorch sends to stdout. How can I do this?

import logging

# Config logger
logging.basicConfig(
  encoding='utf-8',
  level=logging.DEBUG,
  filename=log_path,)

# Redirect all warnings to log file
logging.captureWarnings(True)

# Redirect any error messages to log file???
# ....
# ....

# Pytorch code that will lead to warnings/errors that I
# want stored in the logger file
# ....

The above code will catch only warnings like the following:

WARNING:py.warnings:C:\Users\jared\Anaconda3\envs\base-torch\lib\site-packages\torch\functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ..\aten\src\ATen\native\TensorShape.cpp:2157.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]

but will not capture warnings like the following:

WARNING: The shape inference of prim::Constant type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic functio in symbolic function.

0 Answers
Related