Commits

Kacper Kowalik committed 4151cc9

Add `NullHandler` to `ytLogger` when stream logging is suppresed. Fixes #492

Comments (0)

Files changed (1)

yt/utilities/logger.py

 Affiliation: KIPAC/SLAC/Stanford
 Homepage: http://yt-project.org/
 License:
-  Copyright (C) 2007-2011 Matthew Turk.  All Rights Reserved.
+  Copyright (C) 2007-2013 Matthew Turk.  All Rights Reserved.
 
   This file is part of yt.
 
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 """
 
-import logging, os
-import logging.handlers as handlers
+import logging
 from yt.config import ytcfg
 
 # This next bit is grabbed from:
 # http://stackoverflow.com/questions/384076/how-can-i-make-the-python-logging-output-to-be-colored
+
+
 def add_coloring_to_emit_ansi(fn):
     # add methods we need to the class
     def new(*args):
         levelno = args[1].levelno
-        if(levelno>=50):
-            color = '\x1b[31m' # red
-        elif(levelno>=40):
-            color = '\x1b[31m' # red
-        elif(levelno>=30):
-            color = '\x1b[33m' # yellow
-        elif(levelno>=20):
-            color = '\x1b[32m' # green 
-        elif(levelno>=10):
-            color = '\x1b[35m' # pink
+        if(levelno >= 50):
+            color = '\x1b[31m'  # red
+        elif(levelno >= 40):
+            color = '\x1b[31m'  # red
+        elif(levelno >= 30):
+            color = '\x1b[33m'  # yellow
+        elif(levelno >= 20):
+            color = '\x1b[32m'  # green
+        elif(levelno >= 10):
+            color = '\x1b[35m'  # pink
         else:
-            color = '\x1b[0m' # normal
+            color = '\x1b[0m'  # normal
         ln = color + args[1].levelname + '\x1b[0m'
         args[1].levelname = ln
-        #print "after"
         return fn(*args)
     return new
 
 )
 
 rootLogger = logging.getLogger()
+ytLogger = logging.getLogger("yt")
 
-ytLogger = logging.getLogger("yt")
 
 def disable_stream_logging():
     # We just remove the root logger's handlers
     for handler in rootLogger.handlers:
         if isinstance(handler, logging.StreamHandler):
             rootLogger.removeHandler(handler)
+    h = logging.NullHandler()
+    ytLogger.addHandler(h)
 
 original_emitter = logging.StreamHandler.emit
+
+
 def colorize_logging():
     f = logging.Formatter(cfstring)
-    if len(rootLogger.handlers) > 0: rootLogger.handlers[0].setFormatter(f)
-    logging.StreamHandler.emit = add_coloring_to_emit_ansi(logging.StreamHandler.emit)
+    if len(rootLogger.handlers) > 0:
+        rootLogger.handlers[0].setFormatter(f)
+    logging.StreamHandler.emit = add_coloring_to_emit_ansi(
+        logging.StreamHandler.emit)
+
+
 def uncolorize_logging():
     f = logging.Formatter(ufstring)
-    if len(rootLogger.handlers) > 0: rootLogger.handlers[0].setFormatter(f)
+    if len(rootLogger.handlers) > 0:
+        rootLogger.handlers[0].setFormatter(f)
     logging.StreamHandler.emit = original_emitter
 
-if ytcfg.getboolean("yt","coloredlogs"):
+if ytcfg.getboolean("yt", "coloredlogs"):
     colorize_logging()
 
-if ytcfg.getboolean("yt","suppressStreamLogging"):
+if ytcfg.getboolean("yt", "suppressStreamLogging"):
     disable_stream_logging()
 
 ytLogger.debug("Set log level to %s", level)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.