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...
2

1 Answer 1

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()
    ]
)
Sign up to request clarification or add additional context in comments.

1 Comment

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)..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.