16

I am trying to implement some logging for recording messages. I am getting some weird behavior so I tried to find a minimal example, which I found here. When I just copy the easy example described there into my interpreter the file is not created as you can see here:

In [1]: import logging
   ...: logging.basicConfig(filename='example.log',level=logging.DEBUG)
   ...: logging.debug('This message should go to the log file')
   ...: logging.info('So should this')
   ...: logging.warning('And this, too')
WARNING:root:And this, too

In [2]: ls example.log

File not found

Can anybody help me to understand what I am doing wrong? Thanks...

EDIT: changed the output after the second input to English and removed the unnecessary parts. The only important thing is that Python does not create the file example.log.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Chris
  • 899
  • 2
  • 17
  • 25
  • 2
    Would you please transelate from german to english? That would be a lot easier. – Vincent Beltman Oct 07 '14 at 14:07
  • 1
    I just edited my original post. The only German there basically says that the file 'example.log' does not exist. This is what I wanted to show but I did not realize that there was some German in the output. Sorry for that... – Chris Oct 07 '14 at 14:14
  • Volume in Laufwerk C: hat keine Bezeichnung. --> Volume C: has no Title | Datei nicht gefunden --> File not Found | Verzeichnis von ...--> Directory from ... – Top Questions Oct 07 '14 at 14:29
  • Could you please post the contents of `example.log`? – jdehesa Oct 07 '14 at 14:31
  • @javidcf: The problem is that there is no $example.log$. The logging module does not create it. But according to my understanding, the file should be present in the folder my Python interpreter is currently in after executing the first 5 lines. – Chris Oct 07 '14 at 14:37
  • `example.log` is a file that you must create. Take a look at the [Pyhton logging HOWTO](https://docs.python.org/3/howto/logging.html) ([here for Python 2](https://docs.python.org/2/howto/logging.html)) to see some example. The logging library is not supposed to create it. – jdehesa Oct 07 '14 at 14:44
  • @javidcf Actually, I copy pasted the code from the tutorial that you linked. Nowhere they say that one has to create the log file by hand. That should be done automatically... anyways, I tried to create it by hand but I still could not find any log messages there after running the code. – Chris Oct 07 '14 at 15:48
  • I'm sorry, you are right, I was mistaking `logging.basicConfig` with `logging.config.fileConfig`. – jdehesa Oct 07 '14 at 15:55

4 Answers4

23

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
Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • This answer hinted for me to try my script directly in a terminal... and logging worked as it should. I was trying to get it to work from Thonny IDE (python code editor) and I think the built-in logging/output window does block/override the logging to file. – Vince K May 26 '18 at 21:41
  • 3
    @pyd In Anaconda you can use `from importlib import reload` and then `reload(logging)` (python 3) before you set your basicConfig – Arigion Nov 19 '18 at 14:00
1

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.

Community
  • 1
  • 1
DanRea
  • 19
  • 2
  • I am using Windows... I tried to set the environment variable as explained in your link but no luck so far. Something's a bit fishy about my Python install I guess. If I start Python from the windows command line and then type `import logging` followed by `logging.basicConfig()` I get an error. Python claims that logging has no attribute 'basicConfig'. This is all really strange... – Chris Oct 07 '14 at 15:45
  • Are you running cygwin? – DanRea Oct 07 '14 at 15:51
  • What is your PYTHONPATH environment variable set to? – DanRea Oct 07 '14 at 15:52
  • No, I am not using cygwin. I set the PYTHONPATH variable exactly as described in your link. However, I think the answer is really that I am using IPython and that behaves a bit different from just plain Python. See the accepted answer... anyway, thanks for your time and effort. – Chris Oct 08 '14 at 06:30
0

I found out that my issue was that I was trying to create a log file within nested a nested folder. Once I dumped that idea it became stable.

Norfeldt
  • 8,272
  • 23
  • 96
  • 152
0

In the spirit of an answer above, my problem was that before setting logging.basicConfig, I had some logging.info() statements which set the logging.basicConfig first. Setting up logs before these statements solved it.

vpap
  • 1,389
  • 2
  • 21
  • 32