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
logging.basicConfig(level=logging.DEBUG,
                    filename = 'prog_log.txt',
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filemode = 'w')
logging.debug('Start of Program')

The above does print into the prog_log.txt file the logging details but nothing on the console..

In [3]: runfile('/Volumes/GoogleDrive/Mon Drive/MAC_test.py', wdir='/Volumes/GoogleDrive/Mon Drive/')
...nothing...
vvvvv
  • 25,404
  • 19
  • 49
  • 81
JoeBadAss
  • 49
  • 10
  • Does this answer your question? [logger configuration to log to file and print to stdout](https://stackoverflow.com/questions/13733552/logger-configuration-to-log-to-file-and-print-to-stdout) – Julien Sorin Aug 02 '21 at 09:31
  • @JulienSorin unfortunately, it doesn't :( I tried the question before posting this question – JoeBadAss Aug 02 '21 at 12:39

1 Answers1

0

Duplicate of logger configuration to log to file and print to stdout

You need to add and handler to set the log to StreamHandler :

logging.basicConfig(level=logging.DEBUG,
                    filename = 'prog_log.txt',
                    format='%(asctime)s - %(levelname)s - %(message)s',
                    filemode = 'w')
logger.addHandler(logging.StreamHandler())

or

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s',
    filemode = 'w',
    handlers=[
        logging.FileHandler('prog_log.txt'),
        logging.StreamHandler()
    ]
)
Julien Sorin
  • 703
  • 2
  • 12
  • Thanks @Julien Sorin for your help. The first option leads somewhere when I create a logger with logger = logging.getLogger('') first. But when running multiple times, I get 'Start of Program' 'Start of Program' 'Start of Program' (multiple times displayed and unformated).. – JoeBadAss Aug 02 '21 at 12:48