Python Forum
Can rich.logging output to file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can rich.logging output to file?
#1
Hi,

I am trying to use rich.logging to have more readable logs. Can this be done? As you will see I'm not doing well! ;)

Originally I had the following setup for my logging to file which works fine:

logging.basicConfig(
    format='%(asctime)s [%(levelname)s] [%(name)s] - %(message)s (%(filename)s:%(lineno)d)',
    level='INFO',
    datefmt='%Y-%m-%d %H:%M:%S',
    filename='/tmp/foo.log')
logger = logging.getLogger('Main')
I tried a few variants of the following:
logging.basicConfig(
    format='%(asctime)s [%(levelname)s] [%(name)s] - %(message)s (%(filename)s:%(lineno)d)',
    level='INFO', 
    datefmt='%Y-%m-%d %H:%M:%S',
    handlers=[
        logging.FileHandler('/tmp/foo.log', mode='a'), 
        RichHandler(rich_tracebacks=True, console=None,show_path=True)])
logger = logging.getLogger('Main')

logger.info('info log')
What the above seems to do is output the following to the terminal:

Output:
info log INFO info log <stdin>:3 [03/21/24 11:00:48] INFO info log <stdin>:3 [03/21/24 11:00:48] INFO info log <stdin>:3 [03/21/24 11:00:48] INFO info log <stdin>:3
and the following to the foo.log logfile:

Output:
info log info log info log info log info log
The formatting of the log line is wrong, I output to terminal and log file rather than just the log file and the log file does not take the format into account. I think it's clear I need help. I could not find documentation that allowed me to use rich.logging to output colour and formatting to a log file.

Thanks for any advice you can give.
Reply
#2
I'm sorry. I see no way of removing this thread.

What I wrote is complete nonsense. I was doing my tests in ptpython and I guess I was piling up bad code on top of each other.

Sorry for the noise.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rich output to PDF HEbO61 1 191 Apr-16-2024, 07:30 AM
Last Post: Pedroski55
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 417 Feb-15-2024, 11:15 AM
Last Post: rawatg
  logging: change log file permission with RotatingFileHandler erg 0 1,050 Aug-09-2023, 01:24 PM
Last Post: erg
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Help with TypeWriter Effect in Python Rich Extra 0 1,180 May-23-2022, 09:44 PM
Last Post: Extra
  Help adding prompt text in a Layout using Rich TUI Extra 2 1,664 May-23-2022, 07:15 PM
Last Post: Extra
  create new log file on logging? korenron 6 2,315 Mar-22-2022, 07:14 AM
Last Post: korenron
  Logging to a file - Formatting Maxximiliann 0 1,448 Dec-18-2020, 02:06 AM
Last Post: Maxximiliann
  Output CSV file with filepath and file contents glittergirl 1 1,758 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  csv file output rturus 7 3,263 Jan-30-2020, 02:09 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020