Only show timestamp for dashboard access logs (#2540)

This commit is contained in:
Otto Winter 2021-10-17 21:01:51 +02:00 committed by GitHub
parent 12fce7a08d
commit 5425e45851
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View file

@ -758,7 +758,12 @@ def run_esphome(argv):
args = parse_args(argv)
CORE.dashboard = args.dashboard
setup_log(args.verbose, args.quiet)
setup_log(
args.verbose,
args.quiet,
# Show timestamp for dashboard access logs
args.command == "dashboard",
)
if args.deprecated_argv_suggestion is not None and args.command != "vscode":
_LOGGER.warning(
"Calling ESPHome with the configuration before the command is deprecated "

View file

@ -49,8 +49,10 @@ def color(col: str, msg: str, reset: bool = True) -> bool:
class ESPHomeLogFormatter(logging.Formatter):
def __init__(self):
super().__init__(fmt="%(asctime)s %(levelname)s %(message)s", style="%")
def __init__(self, *, include_timestamp: bool):
fmt = "%(asctime)s " if include_timestamp else ""
fmt += "%(levelname)s %(message)s"
super().__init__(fmt=fmt, style="%")
def format(self, record):
formatted = super().format(record)
@ -64,7 +66,9 @@ class ESPHomeLogFormatter(logging.Formatter):
return f"{prefix}{formatted}{Style.RESET_ALL}"
def setup_log(debug=False, quiet=False):
def setup_log(
debug: bool = False, quiet: bool = False, include_timestamp: bool = False
) -> None:
import colorama
if debug:
@ -79,4 +83,6 @@ def setup_log(debug=False, quiet=False):
logging.getLogger("urllib3").setLevel(logging.WARNING)
colorama.init()
logging.getLogger().handlers[0].setFormatter(ESPHomeLogFormatter())
logging.getLogger().handlers[0].setFormatter(
ESPHomeLogFormatter(include_timestamp=include_timestamp)
)