简体   繁体   中英

python logging info level to a file

I am starting using logging to keep track of what is going on in my code and started here:

https://realpython.com/python-logging/

According to that article this code should create and add a line to a log file called moses_data_extraction.log

import logging
import os

logging.basicConfig(filename='moses_data_extraction.log',
                    filemode='w',
                    format='%(asctime)s - %(message)s',
                    datefmt='%d-%b-%y %H:%M:%S',
                   level=logging.INFO)
logging.info('This will get logged to a file')
print(os.path.isfile('moses_data_extraction.log'))

the last line gives me FALSE

But the file is not created. Actually nothing happens, no file no error.

Some clue what I am doing wrong?

If the last print statement is giving you True , the file must be getting created in your current working directory.

To check the directory where the file would be present, you can check you current working directory using :

import os
print(os.getcwd())

Alternatively, you can specify full file path name in your code to place the log file at your desired location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM