Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting logging output and stdout in correct order in Jupyter Notebook

logging output is out of order in Jupyter Notebook, here is an example.

Plain ipython session:

In [1]: import logging                                                          

In [2]: for i in range(5): 
   ...:   print(i) 
   ...:   if i == 3: 
   ...:     logging.critical("critical") 
   ...:                                                                         
0
1
2
3
CRITICAL:root:critical
4

In [3]:           

but in a Jupyter Notebook the output is out of order:

enter image description here

The logging output is printed before everything else. This makes debugging impossible as one loses any indication where the logging statement occured.

This happens with any logging level.

How can I get output in the correct order, as opposed to logging output getting its own special place?

like image 411
bugmenot123 Avatar asked Nov 19 '25 10:11

bugmenot123


1 Answers

You can fix this by setting the stream to sys.stdout. This will remove any special treatment for logging messages in Jupyter Notebook I guess.

import logging
import sys
logging.basicConfig(stream=sys.stdout)

enter image description here

like image 111
bugmenot123 Avatar answered Nov 21 '25 23:11

bugmenot123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!