Repository: ipython/ipython

Issue:
Option to disable printing logstate on log activation
/usr/lib/python2.7/site-packages/IPython/core/magics/logging.py Line 135

printing the log state everytime shell is opened is annoying, please make it configurable.



Discussion:
I'd like to try my hand at fixing this, I was looking at the source for logging.py and saw I could add another option,
`-n`, for 'no logstate'? Any comments/tips would be great, let me know what you guys think.
EDIT: Maybe even add something in the IPython configuration to set a default 'logstate on startup' option.

Hi John! Sure, give it a go. If you're adding a flag to the magic, I'd probably call it `-q` for quiet. A config option to log on startup might be good as well, but that's probably a little bit more complicated, because it might take a few config options to define the different parameters for logging.

Okay, thanks very much for the input.
I'll start out adding `-q` and later on work on a config option for it.


Resolving patch:
diff --git a/IPython/core/magics/logging.py b/IPython/core/magics/logging.py
--- a/IPython/core/magics/logging.py
+++ b/IPython/core/magics/logging.py
@@ -19,6 +19,7 @@
 # Our own packages
 from IPython.core.magic import Magics, magics_class, line_magic
 from warnings import warn
+from traitlets import Bool
 
 #-----------------------------------------------------------------------------
 # Magic implementation classes
@@ -28,11 +29,17 @@
 class LoggingMagics(Magics):
     """Magics related to all logging machinery."""
 
+    quiet = Bool(False, help=
+        """
+        Suppress output of log state when logging is enabled
+        """
+    ).tag(config=True)
+
     @line_magic
     def logstart(self, parameter_s=''):
         """Start logging anywhere in a session.
 
-        %logstart [-o|-r|-t] [log_name [log_mode]]
+        %logstart [-o|-r|-t|-q] [log_name [log_mode]]
 
         If no name is given, it defaults to a file named 'ipython_log.py' in your
         current directory, in 'rotate' mode (see below).
@@ -82,12 +89,16 @@ def logstart(self, parameter_s=''):
           -t
             put timestamps before each input line logged (these are put in
             comments).
+
+          -q 
+            suppress output of logstate message when logging is invoked
         """
 
-        opts,par = self.parse_options(parameter_s,'ort')
+        opts,par = self.parse_options(parameter_s,'ortq')
         log_output = 'o' in opts
         log_raw_input = 'r' in opts
         timestamp = 't' in opts
+        quiet = 'q' in opts
 
         logger = self.shell.logger
 
@@ -145,9 +156,10 @@ def logstart(self, parameter_s=''):
                 # re-enable timestamping
                 logger.timestamp = True
 
-            print ('Activating auto-logging. '
-                   'Current session state plus future input saved.')
-            logger.logstate()
+            if not (self.quiet or quiet):
+                print ('Activating auto-logging. '
+                       'Current session state plus future input saved.')
+                logger.logstate()
 
     @line_magic
     def logstop(self, parameter_s=''):
diff --git a/IPython/terminal/ipapp.py b/IPython/terminal/ipapp.py
--- a/IPython/terminal/ipapp.py
+++ b/IPython/terminal/ipapp.py
@@ -25,7 +25,9 @@
 from IPython.core.application import (
     ProfileDir, BaseIPythonApplication, base_flags, base_aliases
 )
-from IPython.core.magics import ScriptMagics
+from IPython.core.magics import (
+    ScriptMagics, LoggingMagics
+)
 from IPython.core.shellapp import (
     InteractiveShellApp, shell_flags, shell_aliases
 )
@@ -202,6 +204,7 @@ def _classes_default(self):
             PlainTextFormatter,
             IPCompleter,
             ScriptMagics,
+            LoggingMagics,
             StoreMagics,
         ]
 


Test patch:
diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py
--- a/IPython/core/tests/test_magic.py
+++ b/IPython/core/tests/test_magic.py
@@ -6,6 +6,7 @@
 
 import io
 import os
+import re
 import sys
 import warnings
 from unittest import TestCase
@@ -20,7 +21,7 @@
 from IPython.core.magic import (Magics, magics_class, line_magic,
                                 cell_magic,
                                 register_line_magic, register_cell_magic)
-from IPython.core.magics import execution, script, code
+from IPython.core.magics import execution, script, code, logging
 from IPython.testing import decorators as dec
 from IPython.testing import tools as tt
 from IPython.utils import py3compat
@@ -977,3 +978,35 @@ def sii(s):
     nt.assert_equal(sii("  a = 1\nb = 2"), "a = 1\nb = 2")
     nt.assert_equal(sii("  a\n    b\nc"), "a\n  b\nc")
     nt.assert_equal(sii("a\n  b"), "a\n  b")
+
+def test_logging_magic_quiet_from_arg():
+    _ip.config.LoggingMagics.quiet = False
+    lm = logging.LoggingMagics(shell=_ip)
+    with TemporaryDirectory() as td:
+        try:
+            with tt.AssertNotPrints(re.compile("Activating.*")):
+                lm.logstart('-q {}'.format(
+                        os.path.join(td, "quiet_from_arg.log")))
+        finally:
+            _ip.logger.logstop()
+
+def test_logging_magic_quiet_from_config():
+    _ip.config.LoggingMagics.quiet = True
+    lm = logging.LoggingMagics(shell=_ip)
+    with TemporaryDirectory() as td:
+        try:
+            with tt.AssertNotPrints(re.compile("Activating.*")):
+                lm.logstart(os.path.join(td, "quiet_from_config.log"))
+        finally:
+            _ip.logger.logstop()
+    
+def test_logging_magic_not_quiet():
+    _ip.config.LoggingMagics.quiet = False
+    lm = logging.LoggingMagics(shell=_ip)
+    with TemporaryDirectory() as td:
+        try:
+            with tt.AssertPrints(re.compile("Activating.*")):
+                lm.logstart(os.path.join(td, "not_quiet.log"))
+        finally:
+            _ip.logger.logstop()
+