Skip to content

Commit

Permalink
💩 chore(logging): write logging in the right way
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Mar 13, 2024
1 parent 5d3ac9d commit ece5f61
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 67 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tab_width = 4

[*.{js,ts,tsx,json,yml,yaml,toml}]
indent_size = 2
tab_width = 2

[*.sh]
end_of_line = lf
Expand Down
25 changes: 13 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
from __future__ import annotations


from src.utils.logger import ToolLogger

# Have to read the logger config file before all.
ToolLogger().setup()


from pprint import pformat # pylint: disable=wrong-import-position, wrong-import-order
from multiprocessing import freeze_support # pylint: disable=wrong-import-position, wrong-import-order
from logging import getLogger, Logger # pylint: disable=wrong-import-position, wrong-import-order
from src import ( # pylint: disable=wrong-import-position, wrong-import-order
from pprint import pformat
from multiprocessing import freeze_support
from logging import getLogger, Logger
from urllib3 import disable_warnings as urllib3_disable_warnings
from urllib3.exceptions import InsecureRequestWarning
from src import (
ToolLogger,
Action,
Browser,
Config,
Expand All @@ -27,12 +24,16 @@
pause,
print_title,
)
from src.constants import DOWNLOAD_DIR # pylint: disable=wrong-import-position
from src.constants import DOWNLOAD_DIR


ToolLogger().setup()
urllib3_disable_warnings(InsecureRequestWarning)


def main() -> None:
"""Main function to run VNULIB Downloader."""
logger: Logger = getLogger("vnulib_downloader")
logger: Logger = getLogger(__name__)

PrintIntro()

Expand Down
1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
setup_argparse,
)
from .utils import (
ToolLogger,
create_directory,
pause,
print_title,
Expand Down
2 changes: 1 addition & 1 deletion src/bot/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from ..utils import datetime_name, slugify


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


class Action:
Expand Down
11 changes: 1 addition & 10 deletions src/bot/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@

from __future__ import annotations

import logging
import os
from logging import getLogger
import urllib3
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.webdriver import WebDriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.logger import set_logger
from src.constants import BROWSER_ARGUMENTS


logger = getLogger("vnulib_downloader")

set_logger(logger)
os.environ["WDM_LOG"] = str(logging.DEBUG)
os.environ["WDM_SSL_VERIFY"] = "0"
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
logger = getLogger(__name__)


class Browser:
Expand Down
2 changes: 1 addition & 1 deletion src/bot/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..constants import LOGIN_URL


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


class Login:
Expand Down
2 changes: 1 addition & 1 deletion src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
BUNDLE_DIR = ""


VERSION: str = "1.14"
VERSION: str = "1.15"
AUTHORS: str = "KevinNitroG & NTGNguyen"
BANNER_FILE: str = f"{BUNDLE_DIR}assets/utils/ascii_banner.txt"
with open(BANNER_FILE, encoding="utf-8") as banner_content:
Expand Down
37 changes: 10 additions & 27 deletions src/logging_configuration.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
version: 1
disable_existing_loggers: true
disable_existing_loggers: false
formatters:
simple:
"format": "[%(levelname)s]: %(message)s"
verbose:
"format": "%(asctime)s [%(levelname)-8s] - %(message)s (%(filename)s:%(lineno)d)"
detail:
"format": "%(asctime)s [%(levelname)-8s] - %(message)s (%(pathname)s:%(lineno)d)"
"format": "%(asctime)s [%(levelname)-8s] - %(message)s (%(filename)s:%(lineno)d) - %(name)s"
handlers:
stdout_handler:
class: logging.StreamHandler
stream: ext://sys.stdout
formatter: simple
level: INFO
file_handler:
class: logging.FileHandler
formatter: verbose
filename: "log/tool.log"
level: DEBUG
encoding: utf-8
rotating_file_handler: # Problem of RotatingFileHandler: https://python-forum.io/thread-19104.html
class: logging.handlers.RotatingFileHandler
formatter: verbose
formatter: detail
filename: "log/tool.log"
maxBytes: 50000 # 5KB
maxBytes: 50000 # 50KB
backupCount: 3
level: DEBUG
encoding: utf-8
delay: false
loggers:
vnulib_downloader:
level: DEBUG
handlers:
- stdout_handler
- rotating_file_handler
propogate: false
vnulib_downloader_queue_listener:
level: DEBUG
handlers:
- stdout_handler
- file_handler
propogate: false
vnulib_downloader_sub_process:
level: DEBUG
propogate: false
root:
level: DEBUG
propogate: false
handlers:
- stdout_handler
- rotating_file_handler
2 changes: 1 addition & 1 deletion src/modules/clean_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .link_parse import Link


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


class CleanIMG:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..constants import CONFIG_FILE, CONFIG_SAMPLE_FILE


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


class Config:
Expand Down
6 changes: 3 additions & 3 deletions src/modules/create_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from logging import Logger
import img2pdf
from .link_parse import Link
from ..utils import get_queue_logger, logger_listener
from ..utils import get_subprocess_logger, logger_listener


class CreatePDF:
Expand Down Expand Up @@ -49,7 +49,7 @@ def process(directory: str, name: str, queue: Queue) -> None:
directory (str): The directory containing the images.
name (str): Name of pdf file.
"""
logger: Logger = get_queue_logger(queue)
logger: Logger = get_subprocess_logger(queue)
pdf_file_name: str = os.path.join(directory, f"{name}.pdf")
logger.info('Creating PDF: "%s"', pdf_file_name)
files: list[str] = [os.path.join(directory, item) for item in os.listdir(directory)]
Expand Down Expand Up @@ -109,7 +109,7 @@ def preview_and_page_handler(self, download_directory: str, name: str) -> None:

def create_pdf(self) -> None:
"""Create PDF."""
listener = Process(target=logger_listener, args=("vnulib_downloader_queue_listener", self.queue))
listener = Process(target=logger_listener, args=(__name__, self.queue))
listener.start()
for link in self.links:
match link.original_type:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/download_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ..utils import create_directory


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


def get_error_page_bytes() -> bytes:
Expand Down
2 changes: 1 addition & 1 deletion src/modules/link_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..utils.utils import datetime_name


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


PATTERN_BOOK = re_compile(r"^https?:\/\/ir\.vnulib\.edu\.vn\/handle\/VNUHCM\/\d+$")
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .argpase import setup_argparse


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


@dataclass(slots=True)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from __future__ import annotations

from .logger import logger_listener, get_queue_logger
from .logger import ToolLogger, logger_listener, get_subprocess_logger
from .prints import print_title
from .utils import pause, create_directory, datetime_name, slugify
25 changes: 20 additions & 5 deletions src/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from __future__ import annotations

from logging import Logger, getLogger
from logging import Logger, getLogger, DEBUG
from logging.config import dictConfig
from logging.handlers import QueueHandler
from multiprocessing import Queue
from os import makedirs, path
from os import makedirs, path, environ as os_environ
from webdriver_manager.core.logger import set_logger as webdriver_manager_set_logger
from yaml import safe_load
from src.constants import LOGGING_CONFIG_FILE_PATH, LOGGING_PATH

Expand Down Expand Up @@ -39,10 +40,22 @@ def read_logging_config(self) -> None:
with open(self.config_path, encoding="utf-8") as config_file:
dictConfig(safe_load(config_file)) # skipcq: PY-A6006

@staticmethod
def setup_other_logger() -> None:
"""Disable other loggers and change some loggers."""
getLogger("img2pdf").disabled = True
getLogger("PIL.PngImagePlugin").disabled = True
getLogger("urllib3.connectionpool").disabled = True
getLogger("selenium.webdriver.remote.remote_connection").disabled = True
webdriver_manager_set_logger(getLogger())
os_environ["WDM_LOG"] = str(DEBUG)
os_environ["WDM_SSL_VERIFY"] = "0"

def setup(self) -> None:
"""Setup the logger folder and read logging config file."""
self.log_folder()
self.read_logging_config()
self.setup_other_logger()


def logger_listener(logger_name: str, queue: Queue) -> None:
Expand All @@ -60,8 +73,8 @@ def logger_listener(logger_name: str, queue: Queue) -> None:
logger.handle(record=record)


def get_queue_logger(queue: Queue) -> Logger:
"""Get the queue logger which is pre-configured in config file.
def get_subprocess_logger(queue: Queue) -> Logger:
"""Get the subprocess logger which is pre-configured in config file.
Then add the Queue into the Logger in order to send records to the Queue.
Args:
Expand All @@ -70,6 +83,8 @@ def get_queue_logger(queue: Queue) -> Logger:
Returns:
Logger: The logger.
"""
logger: Logger = getLogger("vnulib_downloader_sub_process")
logger: Logger = getLogger(__name__ + ".subprocess")
logger.addHandler(QueueHandler(queue))
logger.propagate = False
logger.setLevel(DEBUG)
return logger
2 changes: 1 addition & 1 deletion src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..constants import USER_INPUT_YES


logger = getLogger("vnulib_downloader")
logger = getLogger(__name__)


def pause() -> None:
Expand Down

0 comments on commit ece5f61

Please sign in to comment.