logger: use color automatically for a tty
authorJoe Guo <joeg@catalyst.net.nz>
Wed, 24 Oct 2018 03:35:50 +0000 (16:35 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 21 Nov 2018 09:46:20 +0000 (10:46 +0100)
Signed-off-by: Joe Guo <joeg@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Wed Nov 21 10:46:20 CET 2018 on sn-devel-144

python/samba/logger.py

index a3db3be..667c648 100644 (file)
@@ -39,7 +39,7 @@ class ColoredFormatter(logging.Formatter):
 
 
 def get_samba_logger(
-        name='samba', stream=sys.stderr, use_color=True,
+        name='samba', stream=sys.stderr,
         level=None, verbose=False, quiet=False,
         fmt=('%(levelname)s %(asctime)s pid:%(process)d '
              '%(pathname)s #%(lineno)d: %(message)s'),
@@ -56,7 +56,10 @@ def get_samba_logger(
 
     logger.setLevel(level)
 
-    Formatter = use_color and ColoredFormatter or logging.Formatter
+    if (hasattr(stream, 'isatty') and stream.isatty()):
+        Formatter = ColoredFormatter
+    else:
+        Formatter = logging.Formatter
     formatter = Formatter(fmt=fmt, datefmt=datefmt)
 
     handler = logging.StreamHandler(stream=stream)