5

I am trying to add logging to my script,and followed python logging tutorial

example code

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')

But I cannot find the example.log file at the directory where the script is in.And as I call os.getcwd(),it returns C:\\users\\eda.No log is there either.

Community
  • 1
  • 1
Shengxin Huang
  • 647
  • 1
  • 11
  • 25
  • 1
    Why don't you use an absolute path? – Mike Scotty Jan 19 '18 at 07:31
  • 1
    If you are using it in a script, it should be in the same folder as the script – Rakesh Jan 19 '18 at 07:33
  • look at this example https://docs.python.org/2.3/lib/node304.html – S-Wing Jan 19 '18 at 07:36
  • 2
    One gotcha is that you can't call `basicConfig` a second time; if you called it before (say in the REPL while playing around) then new calls will basically have no effect. – tripleee Jan 19 '18 at 07:37
  • 1
    @tripleee In fact, I just found out that if you call any logging call prior to calling basicConfig, it will be called for you, with no arguments! And subsequent calls won't reconfigure! I've spent a few hours trying to figure out why I wasn't creating log files!! – RufusVS Aug 13 '21 at 20:05

1 Answers1

3

There's nothing wrong with your script. However, be clear that the log file will be saved in the same directory as that from which the script was invoked, since you didn't specify an absolute path.

The screenshot below may help clarify this further:

Tremendously clarifying screen shot

tripleee
  • 175,061
  • 34
  • 275
  • 318
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • For me it is not saved in the same directory :( Look at the screen shot in the below answer. – MattiH Jul 03 '21 at 20:52
  • Probably you also need to understand the concept of current working directory. See also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Aug 14 '21 at 08:17