0

logger configuration to log to file and print to stdout would not work with me so:

I want to both print logging details in prog_log.txt AND in the console, I have:

# Debug Settings
Import logging
#logging.disable()  #when program ready, un-indent this line to remove all logging messages
logger = logging.getLogger('')
logging.basicConfig(level=logging.DEBUG,
                    filename = 'prog_log.txt',
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filemode = 'w')
logger.addHandler(logging.StreamHandler())
logging.debug('Start of Program')

The above does print into the prog_log.txt file the logging details properly formated but it prints unformated logging messages multiple times as I re-run the program in the console like below..

In [3]: runfile('/Volumes/GoogleDrive/Mon Drive/MAC_test.py', wdir='/Volumes/GoogleDrive/Mon Drive/')
Start of Program
Start of Program
Start of Program #after third running

Any help welcome ;)

vvvvv
  • 25,404
  • 19
  • 49
  • 81
JoeBadAss
  • 49
  • 10
  • 1
    This should be the answer to your question: https://stackoverflow.com/a/13733863/13890216 – Lakshika Parihar Aug 02 '21 at 13:09
  • Thanks @LakshikaParihar for your help, I get 'Invalid syntax (pyflex E)' on 'fileHandler.setFormatter(logFormatter)', I am using python 3.8.5 – JoeBadAss Aug 02 '21 at 13:48
  • Thanks to the the question you pointed out, I added 'logging.root.handlers = []' at the beggining of the program and now it displays the logging messages, but unformated.. – JoeBadAss Aug 02 '21 at 13:53
  • 1
    ok, @LakshikaParihar, the full answer is indeed in the question you mentioned! Thanks – JoeBadAss Aug 02 '21 at 14:13

0 Answers0