Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log IPython's output without the ugly 7 lines of logging-info on every load?

My objective is to call ipython, while also logging all input/output to IPython, and see something like:

stu@stumac ~  $ ipython

In [1]: exit
stu@stumac ~  $

The banner can be easily removed if I set

c.TerminalIPythonApp.display_banner = False

in my ~/.ipython/profile-default/ipython_config.py file.

But how do I get this clean of a startup while also logging things?

On a fresh install, if I start IPython with no parameters I see:

sente@og ~ $ ipython
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: exit
sente@og ~ $

If I pass a logfile=logfile.txt argument when invoking IPython I see:

sente@og ~ $ ipython --logfile=logfile.txt
Activating auto-logging. Current session state plus future input saved.
Filename       : logfile.txt
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: exit
sente@og ~ $

How can I use logging without adding the extra clutter to my terminal:

Activating auto-logging. Current session state plus future input saved.
Filename       : logfile.txt
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active

On other machines I have IPython configured to automatically log things by having a .ipython/profile_default/startup/01-log-everything.py which contains the lines:

from time import strftime
import os.path

ip = get_ipython()

ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)

ip.run_line_magic('logstart', '-o %s append' % filename)

which results in the same clutter as when I add --logfile=logfile.txt

Any help on how to properly do this would be appreciated. I could if nothing else redirect sys.stdout, configure logging and then reset sys.stdout but I'm hoping there's a less hackish solution.

like image 901
sente Avatar asked Jun 27 '13 11:06

sente


2 Answers

To log with no banner use the --no-banner flag.

ipython --no-banner --logfile='logfile.txt'

This can be found in the help file displayed with the --help-all flag.

ipython --help-all
like image 159
user29165 Avatar answered Nov 11 '22 01:11

user29165


After looking at the IPython source, namely:

  • https://github.com/ipython/ipython/blob/f645e5c044efeacf1aa523ec43f6a25d439e287b/IPython/core/logger.py#L155
  • https://github.com/ipython/ipython/blob/f645e5c044efeacf1aa523ec43f6a25d439e287b/IPython/core/magics/logging.py#L135

It seems the only way to actually achieve what I want -- complete logging without anything echo'd upon startup -- is to redirect or mute sys.stdout while initializing the logging.

I've created a startup file: ~/.ipython/profile_default/startup/01-log-everything.py which does this, the code is:

from time import strftime
import os.path
import contextlib
import cStringIO
import sys


# create a context which we can use for any block which we can use for any
# block which we do not want to print stdout
# -- taken from http://stackoverflow.com/a/2829036/217652

@contextlib.contextmanager
def nostdout():
    save_stdout = sys.stdout
    sys.stdout = cStringIO.StringIO()
    yield
    sys.stdout = save_stdout



ip = get_ipython()

ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)


# stdout is now muted
with nostdout():
    ip.run_line_magic('logstart', '-o %s append' % filename)
like image 32
sente Avatar answered Nov 11 '22 02:11

sente