##// END OF EJS Templates
Adding a -q option to the logstart magic to suppress output of log state. Also added a command line option
Joshua Storck -
Show More
@@ -19,6 +19,7 b' import sys'
19 19 # Our own packages
20 20 from IPython.core.magic import Magics, magics_class, line_magic
21 21 from warnings import warn
22 from traitlets import Bool
22 23
23 24 #-----------------------------------------------------------------------------
24 25 # Magic implementation classes
@@ -28,11 +29,17 b' from warnings import warn'
28 29 class LoggingMagics(Magics):
29 30 """Magics related to all logging machinery."""
30 31
32 quiet = Bool(False, help=
33 """
34 Suppress output of log state when logging is enabled
35 """
36 ).tag(config=True)
37
31 38 @line_magic
32 39 def logstart(self, parameter_s=''):
33 40 """Start logging anywhere in a session.
34 41
35 %logstart [-o|-r|-t] [log_name [log_mode]]
42 %logstart [-o|-r|-t|-q] [log_name [log_mode]]
36 43
37 44 If no name is given, it defaults to a file named 'ipython_log.py' in your
38 45 current directory, in 'rotate' mode (see below).
@@ -82,12 +89,16 b' class LoggingMagics(Magics):'
82 89 -t
83 90 put timestamps before each input line logged (these are put in
84 91 comments).
92
93 -q
94 suppress output of logstate message when logging is invoked
85 95 """
86 96
87 opts,par = self.parse_options(parameter_s,'ort')
97 opts,par = self.parse_options(parameter_s,'ortq')
88 98 log_output = 'o' in opts
89 99 log_raw_input = 'r' in opts
90 100 timestamp = 't' in opts
101 quiet = 'q' in opts
91 102
92 103 logger = self.shell.logger
93 104
@@ -145,6 +156,7 b' class LoggingMagics(Magics):'
145 156 # re-enable timestamping
146 157 logger.timestamp = True
147 158
159 if not (self.quiet or quiet):
148 160 print ('Activating auto-logging. '
149 161 'Current session state plus future input saved.')
150 162 logger.logstate()
@@ -6,6 +6,7 b' Needs to be run by nose (to make ipython session available).'
6 6
7 7 import io
8 8 import os
9 import re
9 10 import sys
10 11 import warnings
11 12 from unittest import TestCase
@@ -20,7 +21,7 b' from IPython.core.error import UsageError'
20 21 from IPython.core.magic import (Magics, magics_class, line_magic,
21 22 cell_magic,
22 23 register_line_magic, register_cell_magic)
23 from IPython.core.magics import execution, script, code
24 from IPython.core.magics import execution, script, code, logging
24 25 from IPython.testing import decorators as dec
25 26 from IPython.testing import tools as tt
26 27 from IPython.utils import py3compat
@@ -977,3 +978,35 b' def test_strip_initial_indent():'
977 978 nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2")
978 979 nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc")
979 980 nt.assert_equal(sii("a\n b"), "a\n b")
981
982 def test_logging_magic_quiet_from_arg():
983 _ip.config.LoggingMagics.quiet = False
984 lm = logging.LoggingMagics(shell=_ip)
985 with TemporaryDirectory() as td:
986 try:
987 with tt.AssertNotPrints(re.compile("Activating.*")):
988 lm.logstart('-q {}'.format(
989 os.path.join(td, "quiet_from_arg.log")))
990 finally:
991 _ip.logger.logstop()
992
993 def test_logging_magic_quiet_from_config():
994 _ip.config.LoggingMagics.quiet = True
995 lm = logging.LoggingMagics(shell=_ip)
996 with TemporaryDirectory() as td:
997 try:
998 with tt.AssertNotPrints(re.compile("Activating.*")):
999 lm.logstart(os.path.join(td, "quiet_from_config.log"))
1000 finally:
1001 _ip.logger.logstop()
1002
1003 def test_logging_magic_not_quiet():
1004 _ip.config.LoggingMagics.quiet = False
1005 lm = logging.LoggingMagics(shell=_ip)
1006 with TemporaryDirectory() as td:
1007 try:
1008 with tt.AssertPrints(re.compile("Activating.*")):
1009 lm.logstart(os.path.join(td, "not_quiet.log"))
1010 finally:
1011 _ip.logger.logstop()
1012
@@ -25,7 +25,9 b' from IPython.core.history import HistoryManager'
25 25 from IPython.core.application import (
26 26 ProfileDir, BaseIPythonApplication, base_flags, base_aliases
27 27 )
28 from IPython.core.magics import ScriptMagics
28 from IPython.core.magics import (
29 ScriptMagics, LoggingMagics
30 )
29 31 from IPython.core.shellapp import (
30 32 InteractiveShellApp, shell_flags, shell_aliases
31 33 )
@@ -202,6 +204,7 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):'
202 204 PlainTextFormatter,
203 205 IPCompleter,
204 206 ScriptMagics,
207 LoggingMagics,
205 208 StoreMagics,
206 209 ]
207 210
General Comments 0
You need to be logged in to leave comments. Login now