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,520 points
9,579 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
• 58,090 points

selected Aug 20, 2019 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 aryya
• 7,450 points
872 views
+1 vote
0 answers

Sum the values of column matching and not matching from a .txt file and write output to a two different files using Python

Name                                                    value DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 15657 DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 156579 DR_CNDAOFSZAPZP_GPFS_VOL.2 156579 DR_CNDAOFSZAPZP_GPFS_VOL.3 ...READ MORE

Nov 20, 2019 in Python by Sagar
• 130 points
975 views
0 votes
0 answers

Cannot make connection to .accdb file using python

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

Jun 7, 2018 in Python by aryya
• 7,450 points
1,652 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,060 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,479 views
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
• 58,090 points
538 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
12,925 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP