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
• 14,960 points
366 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

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,100 points

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,320 points
24 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,320 points
345 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,710 points
32 views
0 votes
1 answer

How to download a file over HTTP using Python?

In Python 2, use urllib2 which comes ...READ MORE

answered Oct 18, 2018 in Python by SDeb
• 9,300 points
15 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 920 points
18 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,100 points
30 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,100 points
21 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.