Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging module does not print in IPython

The following code does print 'I want this to print' in 'ipython qtconsole', however it does not print in plain IPython.

import logging
import logging.handlers

log = logging.getLogger()
f = logging.Formatter("%(asctime)s - %(module)s.   %(funcName)s - %(levelname)s - %(message)s")
fh = logging.handlers.TimedRotatingFileHandler('log.txt', 'W6')
fh.setFormatter(f)
log.addHandler(fh)
log.setLevel(logging.INFO)
log.info('I want this to print')

In 'IPython qtconsole' however i get different problems, that i tried to explain here (which did not go so well, no need to read!).

Can you please tell me why?

EDIT: I use Python 2.7

EDIT2: Maybe i really just need to add logging.StreamHandler.

like image 342
HeinzKurt Avatar asked Jun 17 '14 09:06

HeinzKurt


People also ask

How do I print logging info in Python?

Python - Print Logs in a File. If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter. The format of the message can be specified by using format parameter in basicConfig() method.

How do I print from Ipython notebook?

In JupyterLab select Help -> Launch classic notebook, then from your browser menu (e.g. 3 dot menu) choose print and print to pdf.

Is logging a module in Python?

Python comes with a logging module in the standard library that can provide a flexible framework for emitting log messages from Python programs. This module is widely used by libraries and is often the first go-to point for most developers when it comes to logging.


3 Answers

It seems like qtconsole adds an handler to the root logger:

In [1]: import logging
   ...: root = logging.getLogger()
   ...: root.handlers
   ...: 
Out[1]: [<logging.StreamHandler at 0x7fd8e00e1f98>]

While using the normal python interpreter or just ipython:

In [1]: import logging

In [2]: root = logging.getLogger()

In [3]: root.handlers
Out[3]: []

If you want both to behave the same you should either add a StreamHandler to the root logger for normal ipython, or remove the StreamHandler from the qtconsole interpreter.

If you want the former just add:

root = logging.getLogger()
root.addHandler(logging.StreamHandler())

If you want the latter, before adding your own handler, do:

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

Note that IPython already provides some machinery for logging to a file. See the documentation. If you want to use the code only inside ipython using its magics might be simpler.

like image 94
Bakuriu Avatar answered Oct 11 '22 14:10

Bakuriu


what worked for me

import logger BEFORE any other library/code, in separate cell. this is actually the main requirement. If I load logging and other libraries in one cell, no matter what hierarchy in that cell is, logging does not work

import logging
reload(logging)
logger = logging.getLogger(__name__)

only after loading libraries I set logging config to avoid printing pyspark load debug

logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', 
                    level=logging.INFO, 
                    datefmt='%I:%M:%S')
like image 36
Sergey Makarevich Avatar answered Oct 11 '22 13:10

Sergey Makarevich


If you use Bakuriu's latter solution, it won't work properly if root has more than one handler because of removing-while-iterating problems.

Use instead:

while len(root.handlers):
    root.removeHandler(root.handlers[0])
like image 38
Ronan Paixão Avatar answered Oct 11 '22 13:10

Ronan Paixão