Python does not create log file
Asked Answered
F

4

17

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.

Foxworth answered 7/10, 2014 at 14:2 Comment(8)
Would you please transelate from german to english? That would be a lot easier.Waldner
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...Foxworth
Volume in Laufwerk C: hat keine Bezeichnung. --> Volume C: has no Title | Datei nicht gefunden --> File not Found | Verzeichnis von ...--> Directory from ...Uigur
Could you please post the contents of example.log?Cannoneer
@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.Foxworth
example.log is a file that you must create. Take a look at the Pyhton logging HOWTO (here for Python 2) to see some example. The logging library is not supposed to create it.Cannoneer
@Cannoneer 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.Foxworth
I'm sorry, you are right, I was mistaking logging.basicConfig with logging.config.fileConfig.Cannoneer
A
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
Anzus answered 7/10, 2014 at 17:3 Comment(3)
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.Mudd
@pyd In Anaconda you can use from importlib import reload and then reload(logging) (python 3) before you set your basicConfigAhders
I wanted to log in just to thank your answer @Arigion. I was going from pillar to post with this issue and only your solution worked for me.Perri
H
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.

Hypochondrium answered 7/10, 2014 at 14:50 Comment(4)
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...Foxworth
Are you running cygwin?Hypochondrium
What is your PYTHONPATH environment variable set to?Hypochondrium
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.Foxworth
A
1

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.

Arsenite answered 29/6, 2022 at 15:3 Comment(1)
Solved the issue for me. Thanks a lotMarmite
M
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.

Millwork answered 7/8, 2020 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.