Python does not create log file

Please state which operating system you are using.

Are you running Windows, perhaps using Cygwin? Even if not this looks very much like an environment variable problem. Ensure PYTHONPATH is set correctly. I suggest making a system variable named PYTHONPATH that contains your python install directory (probably something like C:/Python27), and the sub-directories 'Lib', 'Lib/lib-tk' and 'DLLs' directory within this folder as well. See here.

And less likely... Are you running this on a Linux system? Ensure you the appropriate permissions are set on the directory. In bash use 'ls -la' to show the permissions. From the directory run 'chmod u+w .' to ensure you have write permission.


The reason for your unexpected result is that you are using something on top of Python (looks like IPython) which configures the root logger itself. As per the documentation for basicConfig(),

This function does nothing if the root logger already has handlers configured for it.

What you get with just Python is something like this:

C:\temp>python
ActivePython 2.6.1.1 (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec  5 2008, 13:58:38) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.basicConfig(filename='example.log', level=logging.DEBUG)
>>> logging.debug('This message should go to the log file')
>>> logging.info('And so should this')
>>> logging.warning('And this, too')
>>> ^Z

C:\temp>type example.log
DEBUG:root:This message should go to the log file
INFO:root:And so should this
WARNING:root:And this, too

Tags:

Python

Logging