-
I'd like to use So far, i've been using a regular i think understand how rich want's me to configure the format - but using the "rich" desired format breaks file output, while using the "file-output-compatible" format will break how rich looks (or rather - duplicate all information in rich). import logging
from rich.progress import track
from rich.console import Console
from rich.logging import RichHandler
import time
from logging.handlers import RotatingFileHandler
LOGFORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
LOGFORMAT_RICH = '%(message)s'
error_console = Console(stderr=True)
logging.basicConfig(
level=logging.INFO,
# format=LOGFORMAT,
format=LOGFORMAT_RICH,
handlers=[
# logging.StreamHandler(sys.stderr),
RichHandler(console=error_console),
RotatingFileHandler('xxx.log',
maxBytes=1024 * 1024 * 10, # 10Mb
backupCount=10)
]
)
log = logging.getLogger('somefancyname')
for n in range(5) :
log.info(f"logging .. {n}")
time.sleep(0.2)
Now obviously the output in the file is no longer usable, as it's lacking almost all information (like date, ...). While i did see #1309 - it's no longer showing the log-messages in the console, and i'm heavily concerned about the handling of files - as i assume rich will not take care to rotate the files when they grow too big? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Answering my own question here, it's pretty simple to use This way, Rich get's "nice" outputs, while other handlers are not impacted. Full sample: import logging
from logging import Formatter
from rich.progress import track
from rich.console import Console
from rich.logging import RichHandler
import time
from logging.handlers import RotatingFileHandler
LOGFORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
LOGFORMAT_RICH = '%(message)s'
error_console = Console(stderr=True)
rh = RichHandler(console=error_console)
rh.setFormatter(Formatter(LOGFORMAT_RICH))
logging.basicConfig(
level=logging.INFO,
# format=LOGFORMAT,python
format=LOGFORMAT,
handlers=[
rh,
# logging.StreamHandler(sys.stderr),
RotatingFileHandler('xxx.log',
maxBytes=1024 * 1024 * 10, # 10Mb
backupCount=10)
]
)
log = logging.getLogger('somefancyname')
for n in range(5) :
log.info(f"logging .. {n}")
time.sleep(0.2) |
Beta Was this translation helpful? Give feedback.
Answering my own question here, it's pretty simple to use
setFormatter
on the Instance of the Loghandler, forcing it to use a different handler.This way, Rich get's "nice" outputs, while other handlers are not impacted.
Full sample: