fixup! User LOG global variable for logging

This commit is contained in:
Cédric Leporcq 2021-11-12 20:15:53 +01:00
parent e22ac84d75
commit 21be384563
5 changed files with 55 additions and 28 deletions

View File

@ -1,3 +1,3 @@
from ordigi import log
LOG = log.get_logger()
LOG = log.get_logger('ordigi')

View File

@ -7,9 +7,8 @@ import sys
import click
from ordigi import LOG
from ordigi import log, LOG
from ordigi.config import Config
from ordigi import log
from ordigi.collection import Collection
from ordigi.geolocation import GeoLocation
@ -161,7 +160,7 @@ def _import(**kwargs):
according to ordigi.conf preferences.
"""
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
root = kwargs['dest']
src_paths = kwargs['src']
@ -231,7 +230,7 @@ def _sort(**kwargs):
according to ordigi.conf preferences.
"""
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
subdirs = kwargs['subdirs']
root = kwargs['dest']
@ -318,7 +317,7 @@ def _clean(**kwargs):
dry_run = kwargs['dry_run']
folders = kwargs['folders']
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
subdirs = kwargs['subdirs']
root = kwargs['collection']
@ -376,7 +375,7 @@ def _init(**kwargs):
config = get_collection_config(root)
opt = config.get_options()
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
loc = GeoLocation(opt['geocoder'], opt['prefer_english_names'], opt['timeout'])
collection = Collection(root, exclude=opt['exclude'])
@ -397,7 +396,7 @@ def _update(**kwargs):
config = get_collection_config(root)
opt = config.get_options()
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
loc = GeoLocation(opt['geocoder'], opt['prefer_english_names'], opt['timeout'])
collection = Collection(root, exclude=opt['exclude'])
@ -417,7 +416,7 @@ def _check(**kwargs):
root = Path(kwargs['path']).expanduser().absolute()
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
config = get_collection_config(root)
opt = config.get_options()
collection = Collection(root, exclude=opt['exclude'])
@ -429,7 +428,7 @@ def _check(**kwargs):
if summary.errors:
sys.exit(1)
else:
LOG.error('Db data is not accurate run `ordigi update`')
LOG.logger.error('Db data is not accurate run `ordigi update`')
sys.exit(1)
@ -463,7 +462,7 @@ def _compare(**kwargs):
root = kwargs['collection']
log_level = log.get_level(kwargs['verbose'])
LOG = log.get_logger(level=log_level)
log.console(LOG, level=log_level)
paths, root = _get_paths(subdirs, root)
config = get_collection_config(root)

View File

@ -886,7 +886,6 @@ class Collection(SortMedias):
"""Remove empty subdir after moving files"""
parents = set()
for directory in directories:
self.log.info("remove empty subdirs")
if not directory.is_dir():
continue
@ -897,6 +896,7 @@ class Collection(SortMedias):
files = os.listdir(directory)
if len(files) == 0:
self.fileio.rmdir(directory)
self.log.info(f"remove empty subdir: {directory}")
if self.root in directory.parent.parents:
parents.add(directory.parent)

View File

@ -55,7 +55,7 @@ class Config:
elif 'dirs_path' and 'name' in self.conf['Path']:
return self.conf['Path']['dirs_path'] + '/' + self.conf['Path']['name']
return constants.DEFAULT_PATH + '/' + constants.DEFAULT_NAME
return constants.DEFAULT_PATH_FORMAT
def get_options(self):
"""Get config options

View File

@ -3,24 +3,52 @@
import logging
def get_logger(name, level=30):
"""Get logger"""
logger = logging.getLogger(name)
logger.setLevel(level)
return logger
def log_format(level):
if level > 10:
return '%(levelname)s:%(message)s'
return '%(levelname)s:%(name)s:%(message)s'
def set_formatter(handler, level):
"""create formatter and add it to the handlers"""
formatter = logging.Formatter(log_format(level))
handler.setFormatter(formatter)
def console(logger, level=30):
"""create console handler with a higher log level"""
logger.setLevel(level)
handler = logging.StreamHandler()
handler.setLevel(level)
set_formatter(handler, level)
# add the handlers to logger
logger.addHandler(handler)
def file_logger(logger, file, level=30):
"""create file handler that logs debug and higher level messages"""
logger.setLevel(level)
handler = logging.FileHandler(file)
handler.setLevel(level)
set_formatter(handler, log_format(level))
# add the handlers to logger
logger.addHandler(handler)
def get_level(verbose):
"""Return int logging level from string"""
if verbose.isnumeric():
return int(verbose)
return int(logging.getLevelName(verbose))
def get_logger(name='ordigi', level=30):
"""Get configured logger"""
if level > 10:
log_format='%(levelname)s:%(message)s'
else:
log_format='%(levelname)s:%(name)s:%(message)s'
logging.basicConfig(format=log_format, level=level)
logging.getLogger('asyncio').setLevel(level)
logger = logging.getLogger(name)
logger.setLevel(level)
return logger