Skip to content

Commit

Permalink
Change logging to loguru
Browse files Browse the repository at this point in the history
  • Loading branch information
blavka committed Apr 22, 2022
1 parent 24e05ab commit 6a6ff9c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
131 changes: 130 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ hardwario = "hardwario.common.cli:main"
[tool.poetry.dependencies]
python = ">=3.6"
click = "^8.0.3"
loguru = "^0.6.0"

[tool.poetry.dev-dependencies]
pycodestyle = "^2.8.0"
Expand Down
19 changes: 15 additions & 4 deletions src/hardwario/common/cli.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import os
import sys
import logging
import pkgutil
import importlib
import click
import hardwario
from loguru import logger

DEFAULT_LOG_LEVEL = 'DEBUG'
DEFAULT_LOG_FILE = os.path.expanduser("~/.hardwario/console.log")

os.makedirs(os.path.expanduser("~/.hardwario"), exist_ok=True)


@click.group()
@click.option('--log', 'log_level', type=click.Choice(['debug', 'info', 'warning', 'error']), help='Log level', default="warning")
def cli_root(log_level):
'''HARDWARIO Command Line Tool.'''
logging.getLogger().setLevel(log_level.upper())
logger.add(sys.stderr, level=log_level.upper())


def main():
'''Application entry point.'''
logging.basicConfig(
format='%(asctime)s %(levelname)s: %(name)s: %(message)s')

logger.remove()
logger.add(DEFAULT_LOG_FILE,
format="{time} | {level} | {name}.{function}: {message}", level="TRACE", rotation="10 MB")

logger.debug('Argv: {}', sys.argv)

# discovered and load hardwario cli plugins
for finder, name, ispkg in pkgutil.iter_modules(hardwario.__path__, hardwario.__name__ + '.'):
if not ispkg or name == 'hardwario.common':
continue
logger.debug('Discovered module: {}', name)
try:
module = importlib.import_module(name + '.cli')
cli_root.add_command(module.cli)
except Exception as e:
logger.warning('Module cli import: {}', e)
if os.getenv('DEBUG', False):
raise e
pass
Expand Down

0 comments on commit 6a6ff9c

Please sign in to comment.