• 6

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Get Output From the logging Module in IPython Notebook

When I running the following inside IPython Notebook I don't see any output:

import logging

Anyone know how to make it so I can see the "test" message inside the notebook?

Try following:

import logging
logger = logging.getLogger()

According to logging.basicConfig:

Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger. The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger.

This function does nothing if the root logger already has handlers configured for it.

It seems like ipython notebook call basicConfig (or set handler) somewhere.

  • 129
Reply Report

If you still want to use basicConfig, reload the logging module like this

from importlib import reload  # Not needed in Python 2
import logging
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG, datefmt='%I:%M:%S')
  • 64
Reply Report

My understanding is that the IPython session starts up logging so basicConfig doesn't work. Here is the setup that works for me (I wish this was not so gross looking since I want to use it for almost all my notebooks):

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

Now when I run:

logging.debug('This is a debug message')
logging.info('this is an info message')
logging.warning('tbllalfhldfhd, warning.')

I get a "mylog.log" file in the same directory as my notebook that contains:

2015-01-28 09:49:25,026 - root - ERROR - hello!
2015-01-28 09:49:25,028 - root - DEBUG - This is a debug message
2015-01-28 09:49:25,029 - root - INFO - this is an info message
2015-01-28 09:49:25,032 - root - WARNING - tbllalfhldfhd, warning.

Note that if you rerun this without restarting the IPython session it will write duplicate entries to the file since there would now be two file handlers defined

  • 27
Reply Report
      • 1
    • To make this less "gross looking", put the code in a module on your python path, and import it. Prettier and easy to upgrade in the future.

Bear in mind that stderr is the default stream for the logging module, so in IPython and Jupyter notebooks you might not see anything unless you configure the stream to stdout:

import logging
import sys

logging.basicConfig(format='%(asctime)s | %(levelname)s : %(message)s',
                     level=logging.INFO, stream=sys.stdout)

logging.info('Hello world!')
  • 14
Reply Report

What worked for me now (Jupyter, notebook server is: 5.4.1, IPython 7.0.1)

import logging
logger = logging.getLogger('Something')

Now I can use logger to print info, otherwise I would see only message from the default level (logging.WARNING) or above.

  • 13
Reply Report

You can configure logging by running %config Application.log_level="INFO"

For more information, see IPython kernel options

  • 11
Reply Report
      • 1
    • Can you add a few lines with an example? What is the logger handle to invoke to print log messages?
    • At least ipython 7.9.0 (or jupyter 6.0.2) ignores the suggested code, since it doesn't support this class from the running console. Run %config to see the supported classed, Application is not one of them. ipython 7.9.0 here.

I setup a logger for both file and I wanted it to show up on the notebook. Turns out adding a filehandler clears out the default stream handlder.

logger = logging.getLogger()

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Setup file handler
fhandler  = logging.FileHandler('my.log')

# Configure stream handler for the cells
chandler = logging.StreamHandler()

# Add both handlers

# Show the handlers

# Log Something
logger.info("Test info")
logger.debug("Test debug")
logger.error("Test error")
  • 4
Reply Report

It seems that solutions that worked for older versions of ipython/jupyter no longer work.

Here is a working solution for ipython 7.9.0 (also tested with jupyter server 6.0.2):

import logging
logger = logging.getLogger()
logging.debug("test message")

DEBUG:root:test message
  • 0
Reply Report

Trending Tags