From cd1482e4a1ed81ace2a97d8a4ebcccfc7ad4084c Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 25 Sep 2022 20:56:41 +0200 Subject: [PATCH] Unifying logger object. --- pycatalog/__init__.py | 2 +- pycatalog/dbcommon.py | 2 +- pycatalog/dbobjects.py | 2 +- pycatalog/logger.py | 9 ++++++--- pycatalog/misc.py | 4 ++-- pycatalog/scan.py | 2 +- pycatalog/video.py | 2 +- 7 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pycatalog/__init__.py b/pycatalog/__init__.py index 46a3631..1a5b0fb 100644 --- a/pycatalog/__init__.py +++ b/pycatalog/__init__.py @@ -20,7 +20,7 @@ RESET_SEQ = '\033[0m' COLOR_SEQ = '\033[1;%dm' BOLD_SEQ = '\033[1m' -LOG = logger.get_logger(__name__) +LOG = logger.get_logger() def colorize(txt, color): diff --git a/pycatalog/dbcommon.py b/pycatalog/dbcommon.py index 0355599..3b32f02 100644 --- a/pycatalog/dbcommon.py +++ b/pycatalog/dbcommon.py @@ -18,7 +18,7 @@ Base = declarative_base(metadata=Meta) Session = sessionmaker() DbFilename = None -LOG = get_logger("dbcommon") +LOG = get_logger() def connect(filename=None): diff --git a/pycatalog/dbobjects.py b/pycatalog/dbobjects.py index 0675344..61ad0c5 100644 --- a/pycatalog/dbobjects.py +++ b/pycatalog/dbobjects.py @@ -16,7 +16,7 @@ from pycatalog.dbcommon import Base from pycatalog.logger import get_logger -LOG = get_logger(__name__) +LOG = get_logger() tags_files = Table("tags_files", Base.metadata, Column("file_id", Integer, ForeignKey("files.id")), diff --git a/pycatalog/logger.py b/pycatalog/logger.py index 80a829c..bd08664 100644 --- a/pycatalog/logger.py +++ b/pycatalog/logger.py @@ -63,11 +63,10 @@ class ColoredFormatter(logging.Formatter): log_obj = None -def get_logger(module_name, level='INFO', to_file=True, to_console=True): +def get_logger(level='INFO', to_file=True, to_console=True): """ Prepare and return log object. Standard formatting is used for all logs. Arguments: - @module_name - String name for Logger object. @level - Log level (as string), one of DEBUG, INFO, WARN, ERROR and CRITICAL. @to_file - If True, additionally stores full log in file inside @@ -75,10 +74,13 @@ def get_logger(module_name, level='INFO', to_file=True, to_console=True): is only redirected to stderr. Returns: object of logging.Logger class """ + global log_obj + if log_obj: + return log_obj path = os.path.join(os.path.expanduser("~"), ".pycatalog", "app.log") - log = logging.getLogger(module_name) + log = logging.getLogger("pycatalog") log.setLevel(LEVEL[level]) if to_console: @@ -104,4 +106,5 @@ def get_logger(module_name, level='INFO', to_file=True, to_console=True): dummy_handler.setFormatter(dummy_formatter) log.addHandler(dummy_handler) + log_obj = log return log diff --git a/pycatalog/misc.py b/pycatalog/misc.py index 0b4491e..c307d57 100644 --- a/pycatalog/misc.py +++ b/pycatalog/misc.py @@ -5,9 +5,9 @@ Author: Roman 'gryf' Dobosz, gryf73@gmail.com Created: 2009-04-05 """ -from pycatalog.logger import get_logger +from pycatalog import logger -LOG = get_logger(__name__) +LOG = logger.get_logger() def float_to_string(float_length): diff --git a/pycatalog/scan.py b/pycatalog/scan.py index 90bc4f5..cbee049 100644 --- a/pycatalog/scan.py +++ b/pycatalog/scan.py @@ -17,7 +17,7 @@ from pycatalog.logger import get_logger from pycatalog.video import Video -LOG = get_logger(__name__) +LOG = get_logger() RE_FN_START = re.compile(r'(?P' r'(\[[^\]]*\]\s)?' r'([^(]*)\s' diff --git a/pycatalog/video.py b/pycatalog/video.py index 89bf6fe..bbc1ecd 100644 --- a/pycatalog/video.py +++ b/pycatalog/video.py @@ -17,7 +17,7 @@ from pycatalog.misc import float_to_string from pycatalog.logger import get_logger -LOG = get_logger("Video") +LOG = get_logger() class Video(object):