Add Logger configured via cli args to main cmd group.

This commit is contained in:
Sebastian Lenzlinger 2024-06-25 22:56:16 +02:00
parent dd91da6c50
commit fdc59cf93a
4 changed files with 103 additions and 0 deletions

9
README.md Normal file
View File

@ -0,0 +1,9 @@
# Iottb
## Basic Invocation
## Configuration
### Env Vars
- IOTTB_CONF_HOME
By setting this variable you control where the basic iottb application
configuration should be looked for

0
iottb/__init__.py Normal file
View File

80
iottb/main.py Normal file
View File

@ -0,0 +1,80 @@
import subprocess
import click
from pathlib import PurePath
import configparser
import logging
from logging.handlers import RotatingFileHandler
import sys
APP_NAME = 'iottb'
MAX_VERBOSITY = 3
def setup_logging(verbosity, debug):
# Formats
console_log_formats = {
0: '%(levelname)s - %(message)s',
1: '%(levelname)s - %(module)s - %(message)s',
2: '%(levelname)s - %(asctime)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s'
}
file_log_formats = {
0: '%(levelname)s - %(asctime)s - %(module)s - %(message)s',
1: '%(levelname)s - %(asctime)s - %(module)s - %(funcName)s - %(message)s',
2: '%(levelname)s - %(asctime)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s'
}
# set default log level, change later based on parameters
log_level = logging.ERROR
logger = logging.getLogger()
date_format = '%Y-%m-%d %H:%M:%S'
if verbosity > 0:
log_level = logging.WARNING
if verbosity > MAX_VERBOSITY:
verbosity = MAX_VERBOSITY
log_level = logging.INFO
assert verbosity <= MAX_VERBOSITY, f'Verbosity must be <= {MAX_VERBOSITY}'
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter(console_log_formats[verbosity], datefmt=date_format))
console_handler.setLevel(logging.DEBUG) # can keep at debug since it depends on global level?
logger.addHandler(console_handler)
if debug:
log_level = logging.DEBUG
# Logfile logs INFO+, no debugs though
file_handler = RotatingFileHandler(f'{APP_NAME}.log', maxBytes=10240, backupCount=5)
file_handler.setFormatter(logging.Formatter(file_log_formats[verbosity], datefmt=date_format))
file_handler.setLevel(logging.INFO)
# finnish setting up logging
logger.addHandler(file_handler)
logger.setLevel(log_level)
return logger
class Environment:
def __init__(self, cfg=None, db=None, device=None):
self.cfg = cfg
self.db = db
self.device = device
pass
def load_config(cfg_file_path):
cfg_file = PurePath(click.get_app_dir(APP_NAME)).joinpath('iottb.cfg')
@click.group()
@click.option('-v', '--verbosity', type=click.IntRange(0, MAX_VERBOSITY), default=0,
help='Verbosity level')
@click.option('-d', '--debug', is_flag=True, default=False, help='Debug mode')
@click.option('--cfg-file', type=click.Path(), default=PurePath(click.get_app_dir(APP_NAME)).joinpath('iottb.cfg'),
envvar='IOTTB_CONF_HOME',)
def main(debug, verbose):
setup_logging(verbose, debug)
if __name__ == '__main__':
main(auto_envvar_prefix='IOTTB')

14
pyproject.toml Normal file
View File

@ -0,0 +1,14 @@
[tool.poetry]
name = "iottb"
version = "0.1.0"
description = "IoT Testbed"
authors = ["Sebastian Lenzlinger <sebastian.lenzlinger@unibas.ch>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"