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

2

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.

Uuge answered 27/6, 2013 at 11:33 Comment(2)
A similar question was asked here: stackoverflow.com/questions/947810European
@European , 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.Uuge
U
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 https://mcmap.net/q/25717/-silence-the-stdout-of-a-function-in-python-without-trashing-sys-stdout-and-restoring-each-function-call

@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)
Uuge answered 24/7, 2013 at 8:38 Comment(0)
R
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
Rodi answered 28/6, 2013 at 11:12 Comment(1)
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.Uuge
U
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 https://mcmap.net/q/25717/-silence-the-stdout-of-a-function-in-python-without-trashing-sys-stdout-and-restoring-each-function-call

@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)
Uuge answered 24/7, 2013 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.