Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't create logging handler multiple times (DEV-2891) #609

Merged
merged 17 commits into from
Oct 30, 2023
12 changes: 6 additions & 6 deletions src/dsp_tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ def _make_parser(
parser_upload_files.add_argument("-p", "--password", default=root_user_pw, help=password_text)

# fast-xmlupload
parser_fast_xmlupload_files = subparsers.add_parser(
parser_fast_xmlupload = subparsers.add_parser(
name="fast-xmlupload",
help="For internal use only: create resources with already uploaded files",
)
parser_fast_xmlupload_files.set_defaults(action="fast-xmlupload")
parser_fast_xmlupload_files.add_argument("-s", "--server", default=default_dsp_api_url, help=dsp_server_text)
parser_fast_xmlupload_files.add_argument("-u", "--user", default=root_user_email, help=username_text)
parser_fast_xmlupload_files.add_argument("-p", "--password", default=root_user_pw, help=password_text)
parser_fast_xmlupload_files.add_argument("xml_file", help="path to XML file containing the data")
parser_fast_xmlupload.set_defaults(action="fast-xmlupload")
parser_fast_xmlupload.add_argument("-s", "--server", default=default_dsp_api_url, help=dsp_server_text)
parser_fast_xmlupload.add_argument("-u", "--user", default=root_user_email, help=username_text)
parser_fast_xmlupload.add_argument("-p", "--password", default=root_user_pw, help=password_text)
parser_fast_xmlupload.add_argument("xml_file", help="path to XML file containing the data")

# excel2json
parser_excel2json = subparsers.add_parser(
Expand Down
2 changes: 1 addition & 1 deletion src/dsp_tools/fast_xmlupload/process_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.shared import http_call_with_retry, make_chunks

logger = get_logger(__name__, filesize_mb=100, backupcount=36)
logger = get_logger(__name__)
sipi_container: Optional[Container] = None
export_moving_image_frames_script: Optional[Path] = None

Expand Down
76 changes: 51 additions & 25 deletions src/dsp_tools/utils/create_logger.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import logging
import logging.handlers
from pathlib import Path
from typing import cast

rotating_file_handler: logging.handlers.RotatingFileHandler | None = None
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved

def get_logger(
name: str,
level: int = logging.INFO,
filesize_mb: int = 5,
backupcount: int = 4,
) -> logging.Logger:

def _make_handler(
logfile_directory: Path,
filesize_mb: int,
backupcount: int,
) -> None:
"""
Create a logger instance,
set its level to INFO,
and configure it to write to a file in the user's home directory.
Create a formatter and a rotating file handler.
They must live on module level, so that they are created only once.

Args:
name: name of the logger
level: logging level, defaults to logging.INFO
filesize_mb: maximum size per log file in MB, defaults to 5
backupcount: number of log files to keep, defaults to 4
A RotatingFileHandler fills "filename" until it is "maxBytes" big,
then appends ".1" to it and starts with a new file "filename",
fills it until it is "maxBytes" big,
then appends ".1" to it (replacing the old ".1" file)

Returns:
the logger instance
Args:
logfile_directory: directory to store the logfiles in
filesize_mb: maximum size of a logfile in MB
backupcount: number of logfiles to keep
"""
_logger = logging.getLogger(name)
_logger.setLevel(level)
formatter = logging.Formatter(fmt="{asctime} {filename: <20} {levelname: <8} {message}", style="{")
formatter.default_time_format = "%Y-%m-%d %H:%M:%S"
formatter.default_msec_format = "%s.%03d"
# a RotatingFileHandler fills "filename" until it is "maxBytes" big,
# then appends ".1" to it and starts with a new file "filename",
# fills it until it is "maxBytes" big,
# then appends ".1" to it (replacing the old ".1" file)
logfile_directory = Path.home() / Path(".dsp-tools")

logfile_directory.mkdir(exist_ok=True)
handler = logging.handlers.RotatingFileHandler(
filename=logfile_directory / "logging.log",
Expand All @@ -41,5 +37,35 @@ def get_logger(
backupCount=backupcount,
)
handler.setFormatter(formatter)
_logger.addHandler(handler)
return _logger

global rotating_file_handler
rotating_file_handler = handler


def get_logger(
name: str,
level: int = logging.DEBUG,
) -> logging.Logger:
"""
Create a logger instance,
and configure it to write to a file in the user's home directory.

Args:
name: name of the logger
level: logging level, defaults to logging.DEBUG

Returns:
the logger instance
"""
global rotating_file_handler
if not rotating_file_handler:
_make_handler(
Path.home() / Path(".dsp-tools"),
filesize_mb=20,
backupcount=10,
)
rotating_file_handler = cast(logging.handlers.RotatingFileHandler, rotating_file_handler)
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(rotating_file_handler)
return logger