Logging basicConfig without creating a log file when I run in pycharm?

When I run the code below in the terminal, create a log file

import logging 
logging.basicConfig(filename='ramexample.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 when I run the samecode (with another filename = 'ram.log') in pycharm, it does not create any log file. why?

import logging 
logging.basicConfig(filename='ram.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

What do I need to do to create a log file with pycharm?

+11
source share
4 answers

I ran into the same problem and did not find any of the answers given here. This question may have been settled by Ramnath Reddy a long time ago, but I could not find the right answer anywhere on the Internet.

Fortunately, I found a solution from my colleague's code by adding the following lines to logging.basicConfig().

Remove all handlers associated with the root registrar object.

for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

, , .

+27

. . , , :

filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ram.log')
logging.basicConfig(filename=filename, level=logging.DEBUG)

, ram.log , ( __file__).

+3

pycharm Py . , ( dir Windows pwd linux/mac). , ram.log, , , . .

logging.basicConfig(filename='/Users/Donkey/Test/ram.log', level=logging.DEBUG)
+2

I can’t remember where I got this, otherwise I would provide a link. But some time ago I had the same problem using Jupyter on laptops, and this fixed it:

import logging
logger = logging.getLogger()
fhandler = logging.FileHandler(filename='mylog.log', mode='a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fhandler.setFormatter(formatter)
logger.addHandler(fhandler)
logger.setLevel(logging.DEBUG)
0
source

All Articles