-
Due to the good feature in the rich package, i used the code below is the example code which can easily reproduce import hydra
from omegaconf import DictConfig, OmegaConf
import logging
from rich.logging import RichHandler
log = logging.getLogger(__name__)
FORMAT = "%(message)s"
log.setLevel(logging.DEBUG)
log.addHandler(RichHandler())
log.propagate = True
def log_myinfo():
log.info("This is a info message")
log.debug("This is a debug message")
log.warning("This is a warning message")
log.error("This is a error message")
log.critical("This is a critical message")
@hydra.main(version_base="1.3", config_path="./conf", config_name="test.yaml")
def my_app(cfg: DictConfig) -> None:
log_myinfo()
log.propagate = False
log_myinfo()
if __name__ == "__main__":
my_app() the first i set but in the hydra_test.log, logging used Richhandler can't be saved. What can i do to solve this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I'm not 100% sure, but there's a good chance this may be related to #2765. You could try to adapt some of the suggestions in that issue to RichHandler. |
Beta Was this translation helpful? Give feedback.
-
Thx! import logging
from rich.logging import RichHandler
import os
def get_logger():
hydra_path = hydra.core.hydra_config.HydraConfig.get().runtime.output_dir
job_name = hydra.core.hydra_config.HydraConfig.get().job.name
file_handler = logging.FileHandler(os.path.join(hydra_path, f"{job_name}.log"))
rich_handler = RichHandler()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.addHandler(rich_handler)
log.addHandler(file_handler)
log.propagate = False
log.info("Successfully create rich logger")
return log Hope I can helps guys like me. |
Beta Was this translation helpful? Give feedback.
Thx!
Inspired by #2765, by easily revise my code, I replace the original .log file by logging.FileHandler.
The code bellow is a pratically function to generate a logger.