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 14, 2018 in Python by charlie_brown
• 7,710 points
33 views

1 answer to this question.

0 votes

I can't reproduce it on Python 3.3. The messages are written both to the screen and the 'example2.log'. On Python <3.3 it creates the file but it is empty.

The code:

from logging_tree import printout  # pip install logging_tree
printout()

shows that FileHandler() is not attached to the root logger on Python <3.3.

The docs for logging.basicConfig() say that handlers argument is added in Python 3.3. The handlers argument isn't mentioned in Python 3.2 documentation.

answered Aug 14, 2018 by ariaholic
• 7,320 points

Related Questions In Python

0 votes
1 answer

Python using basicConfig method to log to console and file

Try this working fine(tested in python 2.7) ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 56,160 points
573 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
415 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
44 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
• 56,160 points
42 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,160 points
33 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
• 12,120 points
33 views
0 votes
1 answer

Shortest path from source to and from a negative cycle using Bellman Ford in Python

class NegativeWeightFinder: def __init__(self, graph: nx.Graph): ...READ MORE

answered Nov 13, 2018 in Python by Nymeria
• 3,500 points
76 views
+1 vote
1 answer

How to read hdfs file using python?

subprocess.Popen(["hadoop", "fs", "-cat", "/path/to/myfile"], stdout ...READ MORE

answered Dec 6, 2018 in Python by Omkar
• 67,120 points
333 views
0 votes
1 answer

How can I expose callbacks to Fortran using Python

The code that I've written below. The ...READ MORE

answered Aug 23, 2018 in Python by ariaholic
• 7,320 points
89 views
0 votes
1 answer

How to properly print timezone information using python?

Part of the problem here is that ...READ MORE

answered Feb 14 in Python by ariaholic
• 7,320 points
13 views