changeset 2221:230748e76f53

reformat logs, set term bg color
author drewp@bigasterisk.com
date Tue, 23 May 2023 23:39:03 -0700
parents e6ae7bfc12f2
children b0e6fe19dc3b
files light9/run_local.py
diffstat 1 files changed, 30 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/light9/run_local.py	Tue May 23 20:08:24 2023 -0700
+++ b/light9/run_local.py	Tue May 23 23:39:03 2023 -0700
@@ -1,10 +1,13 @@
 # bootstrap
 
-import sys, os, socket
+import logging
+import os
+import socket
+import sys
+from colorsys import hsv_to_rgb
 
-from twisted.python.failure import Failure
+import coloredlogs
 
-import coloredlogs, logging, time
 try:
     import faulthandler
     faulthandler.enable()
@@ -13,43 +16,43 @@
 
 if 0:
     from IPython.core import ultratb
-    sys.excepthook = ultratb.FormattedTB(mode='Verbose',
-                                         color_scheme='Linux',
-                                         call_pdb=1)
+    sys.excepthook = ultratb.FormattedTB(mode='Verbose', color_scheme='Linux', call_pdb=1)
 
 progName = sys.argv[0].split('/')[-1]
 log = logging.getLogger()  # this has to get the root logger
 log.name = progName  # but we can rename it for clarity
 
-
-def fracTimeLogger():
-    class FractionTimeFilter(logging.Filter):
+coloredlogs.install(
+    level='DEBUG',
+    milliseconds=True,
+    datefmt='t=%H:%M:%S',
+    fmt='%(asctime)s.%(msecs)03d %(levelname)1.1s %(name)s %(message)s',
+    # try `pdm run humanfriendly --demo`
+    field_styles=dict(
+        asctime=dict(color=30),
+        levelname=dict(color=247),
+        name=dict(color='blue'),
+    ),
+    level_styles={
+        'debug': dict(color=115),
+        'info': dict(color=250),
+        'warning': dict(color=208),
+        'error': dict(color=161),
+        'critical': dict(color=196),
+    },
+)
 
-        def filter(self, record):
-            record.fractionTime = (
-                time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(record.created)) +
-                ("%.3f" % (record.created % 1)).lstrip('0'))
-            # Don't filter the record.
-            return 1
-
-    coloredlogs.install(
-        level='DEBUG',
-        fmt='%(fractionTime)s %(name)s[%(process)d] %(levelname)s %(message)s')
-    logging.getLogger().handlers[0].addFilter(FractionTimeFilter())
-
-# pytest's log replacement would try to use our fmt with fractionTime but not our handler that supports it. See https://github.com/pytest-dev/pytest/issues/2987
-if 'pytest' not in sys.modules:
-    fracTimeLogger()
 
 def setTerminalTitle(s):
     if os.environ.get('TERM', '') in ['xterm', 'rxvt', 'rxvt-unicode-256color']:
         print("\033]0;%s\007" % s)  # not escaped/protected correctly
+        hue = (hash(progName) % 255) / 255
+        r, g, b = [int(x * 255) for x in hsv_to_rgb(hue, s=.2, v=.1)]
+        print(f"\033]11;#{r:02x}{g:02x}{b:02x}\007")
 
 
 if 'listsongs' not in sys.argv[0] and 'homepageConfig' not in sys.argv[0]:
-    setTerminalTitle(
-        '[%s] %s' %
-        (socket.gethostname(), ' '.join(sys.argv).replace('bin/', '')))
+    setTerminalTitle('[%s] %s' % (socket.gethostname(), ' '.join(sys.argv).replace('bin/', '')))
 
 # see http://www.youtube.com/watch?v=3cIOT9kM--g for commands that make
 # profiles and set background images