Add --log-level argument and move env parsing

This commit is contained in:
Brian Whicheloe 2024-10-17 13:02:51 -07:00
parent ea5d67fe73
commit 0dbca17002
3 changed files with 22 additions and 12 deletions

View file

@ -740,6 +740,14 @@ def parse_args(argv):
options_parser.add_argument( options_parser.add_argument(
"-q", "--quiet", help="Disable all ESPHome logs.", action="store_true" "-q", "--quiet", help="Disable all ESPHome logs.", action="store_true"
) )
options_parser.add_argument(
"-l",
"--log-level",
help="Set the log level.",
default=os.getenv("ESPHOME_LOG_LEVEL", "INFO"),
action="store",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
)
options_parser.add_argument( options_parser.add_argument(
"--dashboard", help=argparse.SUPPRESS, action="store_true" "--dashboard", help=argparse.SUPPRESS, action="store_true"
) )
@ -969,11 +977,16 @@ def run_esphome(argv):
args = parse_args(argv) args = parse_args(argv)
CORE.dashboard = args.dashboard CORE.dashboard = args.dashboard
# Override log level if verbose is set
if args.verbose:
args.log_level = "DEBUG"
elif args.quiet:
args.log_level = "CRITICAL"
setup_log( setup_log(
args.verbose, log_level=args.log_level,
args.quiet,
# Show timestamp for dashboard access logs # Show timestamp for dashboard access logs
args.command == "dashboard", include_timestamp=args.command == "dashboard",
) )
if args.command in PRE_CONFIG_ACTIONS: if args.command in PRE_CONFIG_ACTIONS:

View file

@ -52,8 +52,6 @@ if TYPE_CHECKING:
from requests import Response from requests import Response
LOG_LEVEL = os.getenv("ESPHOME_LOG_LEVEL", "INFO").upper()
logging.getLogger().setLevel(LOG_LEVEL)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ENV_DEV = "ESPHOME_DASHBOARD_DEV" ENV_DEV = "ESPHOME_DASHBOARD_DEV"

View file

@ -1,4 +1,5 @@
import logging import logging
import os
from esphome.core import CORE from esphome.core import CORE
@ -67,20 +68,18 @@ class ESPHomeLogFormatter(logging.Formatter):
def setup_log( def setup_log(
debug: bool = False, quiet: bool = False, include_timestamp: bool = False log_level = logging.INFO,
include_timestamp: bool = False,
) -> None: ) -> None:
import colorama import colorama
colorama.init() colorama.init()
if debug: if log_level == logging.DEBUG:
log_level = logging.DEBUG
CORE.verbose = True CORE.verbose = True
elif quiet: elif log_level == logging.CRITICAL:
log_level = logging.CRITICAL
CORE.quiet = True CORE.quiet = True
else:
log_level = logging.INFO
logging.basicConfig(level=log_level) logging.basicConfig(level=log_level)
logging.getLogger("urllib3").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING)