7

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.

sente
  • 2,327
  • 2
  • 18
  • 24
  • A similar question was asked here: http://stackoverflow.com/questions/947810 – BugoK Jun 27 '13 at 12:02
  • @BugoK , thanks - the question in that post is a bit similar I guess, but what I'm trying to do is very specific and not addressed in that post or anywhere else, as best I can tell. – sente Jun 27 '13 at 23:33

2 Answers2

4

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
user29165
  • 41
  • 3
  • Thanks for the feedback, but I want to prevent the banner *as well as* the all the logging info. When you pass `--logfile='logfile.txt'`, IPython prints half a dozen lines of stuff which I want to suppress. – sente Jul 24 '13 at 08:18
4

After looking at the IPython source, namely:

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)
sente
  • 2,327
  • 2
  • 18
  • 24