Skip to content

Commit

Permalink
Log in 1 file while multiprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Mar 12, 2024
1 parent 0faceb3 commit 69029e5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 33 deletions.
7 changes: 2 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

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
from multiprocessing import freeze_support
Expand All @@ -22,6 +17,7 @@
Login,
PrintIntro,
UserOptions,
ToolLogger,
create_directory,
pause,
print_title,
Expand All @@ -31,6 +27,7 @@

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

PrintIntro()
Expand Down
7 changes: 6 additions & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
UserOptions,
setup_argparse,
)
from .utils import create_directory, pause, print_title
from .utils import (
ToolLogger,
create_directory,
pause,
print_title,
)
2 changes: 1 addition & 1 deletion src/bot/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import logging
import os
import urllib3
from logging import getLogger
import urllib3
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.webdriver import WebDriver
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.13.1-beta"
VERSION: str = "1.13.2-beta"
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
33 changes: 13 additions & 20 deletions src/logging_configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,34 @@ formatters:
detail:
"format": "%(asctime)s [%(levelname)-8s] - %(message)s (%(pathname)s:%(lineno)d)"
handlers:
stdout:
stdout_handler:
class: logging.StreamHandler
stream: ext://sys.stdout
formatter: simple
level: INFO
rotating_file:
file_handler:
class: logging.handlers.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
filename: "log/tool.log"
maxBytes: 10000 # 20000 # 2KB
maxBytes: 5000 # 20000 # 2KB
backupCount: 3
level: DEBUG
encoding: utf-8
mode: a
queue_handler:
class: logging.handlers.QueueHandler
handlers:
- stdout
- rotating_file
respect_handler_level: true
loggers:
vnulib_downloader:
level: DEBUG
handlers:
- stdout
- rotating_file
propogate: false
- stdout_handler
- rotating_file_handler
vnulib_downloader_queue:
level: DEBUG
handlers:
- queue_handler
- stdout_handler
- file_handler
propogate: false
# root:
# handlers:
# - stdout
# - rotating_file
# level: DEBUG
# propogate: false
2 changes: 1 addition & 1 deletion src/modules/create_pdf.py
Original file line number Diff line number Diff line change
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", self.queue))
listener = Process(target=logger_listener, args=("vnulib_downloader_queue", self.queue))
listener.start()
for link in self.links:
match link.original_type:
Expand Down
6 changes: 3 additions & 3 deletions src/modules/download_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import threading
from concurrent.futures import ThreadPoolExecutor
from itertools import count
from typing import Any, override
from typing import override
from logging import getLogger
import requests
from requests.sessions import Session
from requests import Response
import urllib3
from logging import getLogger
from alive_progress import alive_bar
from .link_parse import Link, LinkFile
from ..constants import ERROR_PAGE_IMAGE_PATH
Expand Down Expand Up @@ -55,7 +55,7 @@ def __init__(self, link: LinkFile, download_path: str, timeout: int) -> None:
self.link: LinkFile = link
self.download_path: str = download_path
self.timeout: int = timeout
self.session: Any
self.session: Session

def get_images_bytes(self, link: str, page: str) -> bytes:
"""Get images bytes.
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_queue_logger
from .prints import print_title
from .utils import pause, create_directory, datetime_name, slugify
1 change: 1 addition & 0 deletions src/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def read_logging_config(self) -> None:
dictConfig(safe_load(config_file)) # skipcq: PY-A6006

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

Expand Down

0 comments on commit 69029e5

Please sign in to comment.