Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging basicConfig not creating log file when I run in PyCharm?

Tags:

python

pycharm

When I run below code in terminal its 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 same code (with different filename='ram.log') in PyCharm it's not creating 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 I have to do to create a log file with PyCharm?

like image 627
Ramnath Reddy Avatar asked Jun 16 '15 07:06

Ramnath Reddy


People also ask

What is logging basicConfig in Python?

Python logging basicConfig The basicConfig configures the root logger. It does basic configuration for the logging system by creating a stream handler with a default formatter. The debug , info , warning , error and critical call basicConfig automatically if no handlers are defined for the root logger.


1 Answers

I encountered same issue and found none of the answers previously provided here would work. Maybe this issue had been solved long ago to Ramnath Reddy, but I could not find the correct answer anywhere online.

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

# Remove all handlers associated with the root logger object. for handler in logging.root.handlers[:]:     logging.root.removeHandler(handler) 

Try and see if it helps for whomever had the same issue.

Python 3.8: A new option, force, has been made available to automatically remove the root handlers while calling basicConfig().
For example:

logging.basicConfig(filename='ramexample.log', level=logging.DEBUG, force=True)` 

See logging.basicConfig parameters:

force: If this keyword argument is specified as true, any existing handlers attached to the root logger are removed and closed, before carrying out the configuration as specified by the other arguments.

like image 81
Seal Avatar answered Oct 06 '22 19:10

Seal