Using logging with rich.progress #1173
-
Hi, import logging
import time
from rich.logging import RichHandler
from rich.progress import Progress
logging.basicConfig(handlers=[RichHandler()])
logger = logging.getLogger('rich')
with Progress(expand=True) as progress:
task = progress.add_task("Loading...",)
while not progress.finished:
logger.info('some log output')
progress.update(task, advance=10)
time.sleep(0.1) Unfortunately, the progress bar breaks on each log entry. How can I accomplish this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Your example didn't run as is because you didn't set the level on import logging
import time
from rich.logging import RichHandler
from rich.progress import Progress
logging.basicConfig(level="NOTSET", handlers=[RichHandler(level="NOTSET")])
logger = logging.getLogger('rich')
with Progress(expand=True) as progress:
task = progress.add_task("Loading...",)
while not progress.finished:
logger.info('some log output')
progress.update(task, advance=0.1)
time.sleep(1) |
Beta Was this translation helpful? Give feedback.
-
Hello, thanks for the tip logging.basicConfig(level="NOTSET", handlers=[RichHandler(level="NOTSET")])
logger = logging.getLogger('rich')
with console.status('Parsing...'):
for x in range(5):
logger.info('some log output')
sleep(1) I've tried example above with latest rich release (13.7.7) and unfortunately log messages breaks |
Beta Was this translation helpful? Give feedback.
-
Hi, thanks for your tips. I guess I cannot use logging setupdef setup_logging(logfile: Path=Path()):
"""
Set up the logging framework:
1) Extend the Logger class with custom 'bcdebug', 'verbose' and 'success' logging levels / methods
2) Add a console streamhandler
3) If given a log file, then add a regular log + a warning/error filehandler
:param logfile: Name of the log file
"""
# Set the default formats
if DEBUG:
fmt = '%(asctime)s - %(name)s | %(message)s'
else:
fmt = '%(asctime)s - %(levelname)s | %(message)s'
datefmt = '%Y-%m-%d %H:%M:%S'
# Register custom log levels
for name, level in {'BCDEBUG': 11, 'VERBOSE': 15, 'SUCCESS': 25}.items():
logging.addLevelName(level, name)
setattr(logging, name, level)
# Register custom log methods
def bcdebug(self, message, *args, **kws):
if self.isEnabledFor(logging.BCDEBUG):
self._log(logging.BCDEBUG, message, args, **kws)
logging.getLoggerClass().bcdebug = bcdebug
def verbose(self, message, *args, **kws):
if self.isEnabledFor(logging.VERBOSE):
self._log(logging.VERBOSE, message, args, **kws)
logging.getLoggerClass().verbose = verbose
def success(self, message, *args, **kws):
if self.isEnabledFor(logging.SUCCESS):
self._log(logging.SUCCESS, message, args, **kws)
logging.getLoggerClass().success = success
# Set the root logging level
logger = logging.getLogger()
logger.setLevel('BCDEBUG' if DEBUG else 'VERBOSE')
# Add the Rich console handler and bring some color to those boring logs! :-)
console = Console(theme = Theme({
'logging.level.verbose': 'grey50',
'logging.level.success': 'green bold',
'logging.level.bcdebug': 'bright_yellow'
}))
consolehandler = RichHandler(
console = console,
show_time = False,
show_level = True,
show_path = DEBUG,
rich_tracebacks = True,
markup = True,
level = 'BCDEBUG' if DEBUG else 'VERBOSE' if not logfile.name else 'INFO'
)
consolehandler.set_name('console')
logger.addHandler(consolehandler)
if logfile.name:
# Add the verbose filehandler
logfile.parent.mkdir(parents=True, exist_ok=True)
formatter = logging.Formatter(fmt=fmt, datefmt=datefmt)
loghandler = logging.FileHandler(logfile)
loghandler.setLevel('BCDEBUG')
loghandler.setFormatter(formatter)
loghandler.set_name('loghandler')
logger.addHandler(loghandler)
# Add the error/warnings filehandler
errorhandler = logging.FileHandler(logfile.with_suffix('.errors'), mode='w')
errorhandler.setLevel('WARNING')
errorhandler.setFormatter(formatter)
errorhandler.set_name('errorhandler')
logger.addHandler(errorhandler) |
Beta Was this translation helpful? Give feedback.
Your example didn't run as is because you didn't set the level on
basicConfig
, but the following works as expected for me: