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 Jesse Hills
parent f0089b7940
commit 70b62f272e
No known key found for this signature in database
GPG Key ID: BEAAE804EFD8E83A
2 changed files with 16 additions and 5 deletions

View File

@ -758,7 +758,12 @@ def run_esphome(argv):
args = parse_args(argv) args = parse_args(argv)
CORE.dashboard = args.dashboard 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": if args.deprecated_argv_suggestion is not None and args.command != "vscode":
_LOGGER.warning( _LOGGER.warning(
"Calling ESPHome with the configuration before the command is deprecated " "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): class ESPHomeLogFormatter(logging.Formatter):
def __init__(self): def __init__(self, *, include_timestamp: bool):
super().__init__(fmt="%(asctime)s %(levelname)s %(message)s", style="%") fmt = "%(asctime)s " if include_timestamp else ""
fmt += "%(levelname)s %(message)s"
super().__init__(fmt=fmt, style="%")
def format(self, record): def format(self, record):
formatted = super().format(record) formatted = super().format(record)
@ -64,7 +66,9 @@ class ESPHomeLogFormatter(logging.Formatter):
return f"{prefix}{formatted}{Style.RESET_ALL}" 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 import colorama
if debug: if debug:
@ -79,4 +83,6 @@ def setup_log(debug=False, quiet=False):
logging.getLogger("urllib3").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING)
colorama.init() colorama.init()
logging.getLogger().handlers[0].setFormatter(ESPHomeLogFormatter()) logging.getLogger().handlers[0].setFormatter(
ESPHomeLogFormatter(include_timestamp=include_timestamp)
)