Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are python logs default stored when ran through IPython notebook?

In an IPython notebook cell I wrote:

import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
handler = logging.FileHandler('model.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

Notice that I am supplying a file name, but not a path.

Where could I find that log? (ran a 'find' and couldn't locate it...)

like image 695
user2808117 Avatar asked Jul 13 '14 11:07

user2808117


2 Answers

There's multiple ways to set the IPython working directory. If you don't set any of that in your IPython profile/config, environment or notebook, the log should be in your working directory. Also try $ ipython locate to print the default IPython directory path, the log may be there.

What about giving it an absolute file path to see if it works at all?

Other than that the call to logging.basicConfig doesn't seem to do anything inside an IPython notebook:

# In:
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
logger.debug('root debug test')

There's no output.

As per the docs, the logging.basicConfig doesn't do anything if the root logger already has handlers configured for it. This seems to be the case, IPython apparently already has the root logger set up. We can confirm it:

# In:
import logging
logger = logging.getLogger()
logger.handlers

# Out:
[<logging.StreamHandler at 0x106fa19d0>]

So we can try setting the root logger level manually:

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug('root debug test')

which yields a formatted output in the notebook:

enter image description here

Now onto setting up the file logger:

# In:
import logging

# set root logger level
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG)

# setup custom logger
logger = logging.getLogger(__name__)
handler = logging.FileHandler('model.log')
handler.setLevel(logging.INFO)
logger.addHandler(handler)

# log
logger.info('test info my')

which results in writing the output both to the notebook and the model.log file, which for me is located in a directory I started IPython and notebook from.

Mind that repeated calls to this piece of code without restarting the IPython kernel will result in creating and attaching yet another handler to the logger on every run and the number of messages being logged to the file with each log call will grow.

like image 77
famousgarkin Avatar answered Oct 04 '22 22:10

famousgarkin


Declare the path of the log file in the basicConfig like this :

log_file_path = "/your/path/"
logging.basicConfig(level   =   logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    filename    =   log_file_path,
                    filemode    =   'w')

You can then start logging and why not add a different log format to the console if you want :

# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger().addHandler(console)
logger = logging.getLogger()

et voilà .

like image 20
4m1nh4j1 Avatar answered Oct 04 '22 23:10

4m1nh4j1