sx_log.py 710 B

123456789101112131415161718192021222324
  1. # coding=utf-8
  2. import datetime
  3. import sys
  4. def format_print():
  5. class GeneralWriter:
  6. def __init__(self, *writers):
  7. self.writers = writers
  8. def write(self, buf):
  9. now = datetime.datetime.now()
  10. ts = '{},{}'.format(now.strftime('%Y-%m-%d %H:%M:%S'), '%03d' % (now.microsecond // 1000))
  11. for w in self.writers:
  12. for line in buf.rstrip().splitlines():
  13. msg = line.rstrip()
  14. if len(msg):
  15. w.write('\033[1;32;1m{}| {}\033[0m\n'.format(ts, msg))
  16. def flush(self):
  17. pass
  18. sys.stdout = GeneralWriter(sys.stdout)
  19. sys.stderr = GeneralWriter(sys.stdout)