Python using basicConfig method to log to console and file

0 votes

I don't know why this code prints to the screen, but not to the file? File "example1.log" is created, but nothing is written there.

#!/usr/bin/env python3 
import logging 
logging.basicConfig(level=logging.DEBUG,
            format='%(asctime)s %(message)s',
            handlers=[logging.FileHandler("example1.log"),
                  logging.StreamHandler()]) 
                logging.debug('This message should go to the log file
                         and to the console') 
                logging.info('So should this') 
                logging.warning('And this, too')

I have "bypassed" this problem by creating a logging object (example code), but it keeps bugging me why basicConfig() approach failed?

PS. If I change basicConfig call to:

logging.basicConfig(level=logging.DEBUG,
            filename="example2.log",
            format='%(asctime)s %(message)s', 
            handlers=[logging.StreamHandler()])

Then all logs are in the file and nothing is displayed in the console
Aug 27, 2018 in Python by bug_seeker
• 15,350 points
1,005 views

1 answer to this question.

+1 vote
Best answer

Try this working fine(tested in python 2.7) for both console and file

# set up logging to file 
logging.basicConfig( 
    filename='twitter_effect.log',
    level=logging.INFO, 
    format='[%(asctime)s]{%(pathname)s:%(lineno)d}%(levelname)s- %(message)s', 
    datefmt='%H:%M:%S'
) 
# set up logging to console 
console = logging.StreamHandler() 
console.setLevel(logging.DEBUG) 
# set a format which is simpler for console use 
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter) 
# add the handler to the root logger 
logging.getLogger('').addHandler(console) 
logger = logging.getLogger(__name__)
answered Aug 27, 2018 by Priyaj
• 56,900 points

selected Aug 20 by Kalgi
Thanks  :) This was helpful to me.

Related Questions In Python

0 votes
1 answer

Python using basicConfig method to log to console and file

I can't reproduce it on Python 3.3. ...READ MORE

answered Aug 14, 2018 in Python by ariaholic
• 7,340 points
46 views
0 votes
0 answers

Cannot make connection to .accdb file using python

I am writing a script that needs ...READ MORE

Jun 6, 2018 in Python by ariaholic
• 7,340 points
585 views
0 votes
1 answer

Cannot make connection to .accdb file using python

key is just a variable name. for key in ...READ MORE

answered Sep 21, 2018 in Python by charlie_brown
• 7,720 points
71 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 330 views
+4 votes
6 answers
0 votes
1 answer

Cannot make connection to .accdb file using python

That link has instructions for connecting to ...READ MORE

answered Oct 5, 2018 in Python by Priyaj
• 56,900 points
61 views
0 votes
1 answer

Cannot make connection to .accdb file using python

You can refer to this link for ...READ MORE

answered Oct 9, 2018 in Python by Priyaj
• 56,900 points
55 views