From bf83eb276fa2c7b1e9c9421f63ffbaf28c3bfb23 Mon Sep 17 00:00:00 2001 From: yma Date: Tue, 21 Dec 2021 18:36:35 +0800 Subject: [PATCH 1/4] Write failed files (errors) to an errors.log and summarize to console --- charon/cmd/command.py | 3 ++- charon/config.py | 4 +++- charon/pkgs/indexing.py | 2 ++ charon/pkgs/maven.py | 2 ++ charon/pkgs/npm.py | 2 ++ charon/pkgs/pkg_utils.py | 8 ++++++-- charon/storage.py | 2 ++ charon/utils/archive.py | 2 ++ charon/utils/logs.py | 9 +++++++++ 9 files changed, 30 insertions(+), 4 deletions(-) diff --git a/charon/cmd/command.py b/charon/cmd/command.py index 799b69fa..4d2e3ad5 100644 --- a/charon/cmd/command.py +++ b/charon/cmd/command.py @@ -15,7 +15,7 @@ """ from typing import List from charon.config import CharonConfig, get_config -from charon.utils.logs import set_logging +from charon.utils.logs import set_logging, add_file_handler from charon.utils.archive import detect_npm_archive, download_archive, NpmArchiveType from charon.pkgs.maven import handle_maven_uploading, handle_maven_del from charon.pkgs.npm import handle_npm_uploading, handle_npm_del @@ -29,6 +29,7 @@ import sys logger = logging.getLogger(__name__) +add_file_handler(logger) @argument( diff --git a/charon/config.py b/charon/config.py index 3e464fb8..5c4598fb 100644 --- a/charon/config.py +++ b/charon/config.py @@ -16,6 +16,7 @@ from typing import Dict, List from ruamel.yaml import YAML from pathlib import Path +from charon.utils.logs import add_file_handler import os import logging @@ -24,6 +25,7 @@ CONFIG_FILE = "charon.yaml" logger = logging.getLogger(__name__) +add_file_handler(logger) class CharonConfig(object): @@ -48,7 +50,7 @@ def get_aws_profile(self) -> str: def get_aws_bucket(self, target: str) -> str: target_: Dict = self.__targets.get(target, None) if not target_ or not isinstance(target_, Dict): - logger.error("The target %s is not found in charon configuration.") + logger.error("The target %s is not found in charon configuration.", target) return None bucket = target_.get("bucket", None) if not bucket: diff --git a/charon/pkgs/indexing.py b/charon/pkgs/indexing.py index bcb5d1f5..e412c92f 100644 --- a/charon/pkgs/indexing.py +++ b/charon/pkgs/indexing.py @@ -17,6 +17,7 @@ from charon.storage import S3Client from charon.constants import (INDEX_HTML_TEMPLATE, NPM_INDEX_HTML_TEMPLATE, PACKAGE_TYPE_MAVEN, PACKAGE_TYPE_NPM, PROD_INFO_SUFFIX) +from charon.utils.logs import add_file_handler from jinja2 import Template import os import logging @@ -25,6 +26,7 @@ from charon.utils.strings import remove_prefix logger = logging.getLogger(__name__) +add_file_handler(logger) def __get_index_template(package_type: str) -> str: diff --git a/charon/pkgs/maven.py b/charon/pkgs/maven.py index 77b95f52..2935fd63 100644 --- a/charon/pkgs/maven.py +++ b/charon/pkgs/maven.py @@ -25,6 +25,7 @@ META_FILE_FAILED, MAVEN_METADATA_TEMPLATE, ARCHETYPE_CATALOG_TEMPLATE, ARCHETYPE_CATALOG_FILENAME, PACKAGE_TYPE_MAVEN) +from charon.utils.logs import add_file_handler from typing import Dict, List, Tuple from jinja2 import Template from datetime import datetime @@ -39,6 +40,7 @@ logger = logging.getLogger(__name__) +add_file_handler(logger) def __get_mvn_template(kind: str, default: str) -> str: diff --git a/charon/pkgs/npm.py b/charon/pkgs/npm.py index 52477e57..eb2f7311 100644 --- a/charon/pkgs/npm.py +++ b/charon/pkgs/npm.py @@ -29,8 +29,10 @@ from charon.utils.archive import extract_npm_tarball from charon.pkgs.pkg_utils import upload_post_process, rollback_post_process from charon.utils.strings import remove_prefix +from charon.utils.logs import add_file_handler logger = logging.getLogger(__name__) +add_file_handler(logger) PACKAGE_JSON = "package.json" diff --git a/charon/pkgs/pkg_utils.py b/charon/pkgs/pkg_utils.py index bf628291..c6fdef34 100644 --- a/charon/pkgs/pkg_utils.py +++ b/charon/pkgs/pkg_utils.py @@ -1,8 +1,10 @@ from typing import List +from charon.utils.logs import add_file_handler import sys import logging logger = logging.getLogger(__name__) +add_file_handler(logger) def is_metadata(file: str) -> bool: @@ -35,9 +37,11 @@ def __post_process( operation: str ): if len(failed_files) == 0 and len(failed_metas) == 0: - logger.info("Product release %s is successfully" - "%s Ronda service.", operation, product_key) + logger.info("Product release is successfully %s " + " %s Ronda service.", operation, product_key) else: + total = len(failed_files) + len(failed_metas) + logger.error("%d file(s) occur errors/warnings, please see errors.log for details.", total) logger.error("Product release %s is %s Ronda " "service, but has some failures as below:", product_key, operation) diff --git a/charon/storage.py b/charon/storage.py index 99a46d9e..cb5d1587 100644 --- a/charon/storage.py +++ b/charon/storage.py @@ -15,6 +15,7 @@ """ from boto3_type_annotations.s3.service_resource import Object from charon.utils.files import read_sha1 +from charon.utils.logs import add_file_handler from charon.constants import PROD_INFO_SUFFIX from boto3 import session @@ -28,6 +29,7 @@ import mimetypes logger = logging.getLogger(__name__) +add_file_handler(logger) PRODUCT_META_KEY = "rh-products" CHECKSUM_META_KEY = "checksum" diff --git a/charon/utils/archive.py b/charon/utils/archive.py index 3dbd2081..05e7e699 100644 --- a/charon/utils/archive.py +++ b/charon/utils/archive.py @@ -24,8 +24,10 @@ from json import load, JSONDecodeError from typing import Tuple from zipfile import ZipFile, is_zipfile +from charon.utils.logs import add_file_handler logger = logging.getLogger(__name__) +add_file_handler(logger) def extract_zip_all(zf: ZipFile, target_dir: str): diff --git a/charon/utils/logs.py b/charon/utils/logs.py index 7b0f43c4..1ead0fa1 100644 --- a/charon/utils/logs.py +++ b/charon/utils/logs.py @@ -72,3 +72,12 @@ def set_logging(name="charon", level=logging.DEBUG, handler=None): logger = logging.getLogger('charon') for hdlr in list(logger.handlers): # make a copy so it doesn't change hdlr.setFormatter(formatter) + + +def add_file_handler(logger: logging): + handler = logging.FileHandler('errors.log') + handler.setLevel(logging.ERROR) + handler.setLevel(logging.WARN) + formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + handler.setFormatter(formatter) + logger.addHandler(handler) From 26757409792eca02fd3d2a5705f194dc7a234474 Mon Sep 17 00:00:00 2001 From: yma Date: Wed, 22 Dec 2021 13:27:25 +0800 Subject: [PATCH 2/4] Rewrap the logger --- charon/cmd/command.py | 5 ++--- charon/config.py | 6 ++---- charon/pkgs/indexing.py | 6 ++---- charon/pkgs/maven.py | 15 ++++++--------- charon/pkgs/npm.py | 6 ++---- charon/pkgs/pkg_utils.py | 6 ++---- charon/storage.py | 6 ++---- charon/utils/archive.py | 6 ++---- charon/utils/logs.py | 12 ++++++++---- 9 files changed, 28 insertions(+), 40 deletions(-) diff --git a/charon/cmd/command.py b/charon/cmd/command.py index 4d2e3ad5..3e40af86 100644 --- a/charon/cmd/command.py +++ b/charon/cmd/command.py @@ -15,7 +15,7 @@ """ from typing import List from charon.config import CharonConfig, get_config -from charon.utils.logs import set_logging, add_file_handler +from charon.utils.logs import set_logging, getLogger from charon.utils.archive import detect_npm_archive, download_archive, NpmArchiveType from charon.pkgs.maven import handle_maven_uploading, handle_maven_del from charon.pkgs.npm import handle_npm_uploading, handle_npm_del @@ -28,8 +28,7 @@ import os import sys -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) @argument( diff --git a/charon/config.py b/charon/config.py index 5c4598fb..ee60b705 100644 --- a/charon/config.py +++ b/charon/config.py @@ -16,16 +16,14 @@ from typing import Dict, List from ruamel.yaml import YAML from pathlib import Path -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger import os -import logging from charon.utils.strings import remove_prefix CONFIG_FILE = "charon.yaml" -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) class CharonConfig(object): diff --git a/charon/pkgs/indexing.py b/charon/pkgs/indexing.py index e412c92f..8532e34e 100644 --- a/charon/pkgs/indexing.py +++ b/charon/pkgs/indexing.py @@ -17,16 +17,14 @@ from charon.storage import S3Client from charon.constants import (INDEX_HTML_TEMPLATE, NPM_INDEX_HTML_TEMPLATE, PACKAGE_TYPE_MAVEN, PACKAGE_TYPE_NPM, PROD_INFO_SUFFIX) -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger from jinja2 import Template import os -import logging from typing import List, Set from charon.utils.strings import remove_prefix -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) def __get_index_template(package_type: str) -> str: diff --git a/charon/pkgs/maven.py b/charon/pkgs/maven.py index 2935fd63..5cfab692 100644 --- a/charon/pkgs/maven.py +++ b/charon/pkgs/maven.py @@ -25,7 +25,7 @@ META_FILE_FAILED, MAVEN_METADATA_TEMPLATE, ARCHETYPE_CATALOG_TEMPLATE, ARCHETYPE_CATALOG_FILENAME, PACKAGE_TYPE_MAVEN) -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger from typing import Dict, List, Tuple from jinja2 import Template from datetime import datetime @@ -35,12 +35,9 @@ import os import sys -import logging import re - -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) def __get_mvn_template(kind: str, default: str) -> str: @@ -650,7 +647,7 @@ def _generate_rollback_archetype_catalog( try: local_archetypes = _parse_archetypes(f.read()) except ElementTree.ParseError: - logging.warning( + logger.warning( "Failed to parse archetype-catalog.xml from local archive with root: %s. " "SKIPPING invalid archetype processing.", root @@ -672,7 +669,7 @@ def _generate_rollback_archetype_catalog( try: remote_archetypes = _parse_archetypes(remote_xml) except ElementTree.ParseError: - logging.warning( + logger.warning( "Failed to parse archetype-catalog.xml from bucket: %s. " "CLEANING invalid remote archetype-catalog.xml", bucket @@ -751,7 +748,7 @@ def _generate_upload_archetype_catalog( try: local_archetypes = _parse_archetypes(f.read()) except ElementTree.ParseError: - logging.warning( + logger.warning( "Failed to parse archetype-catalog.xml from local archive with root: %s. " "SKIPPING invalid archetype processing.", root @@ -770,7 +767,7 @@ def _generate_upload_archetype_catalog( try: remote_archetypes = _parse_archetypes(remote_xml) except ElementTree.ParseError: - logging.warning( + logger.warning( "Failed to parse archetype-catalog.xml from bucket: %s. " "OVERWRITING bucket archetype-catalog.xml with the valid, local copy.", bucket diff --git a/charon/pkgs/npm.py b/charon/pkgs/npm.py index eb2f7311..fcb14744 100644 --- a/charon/pkgs/npm.py +++ b/charon/pkgs/npm.py @@ -13,7 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. """ -import logging import os import sys from json import load, loads, dump, JSONDecodeError @@ -29,10 +28,9 @@ from charon.utils.archive import extract_npm_tarball from charon.pkgs.pkg_utils import upload_post_process, rollback_post_process from charon.utils.strings import remove_prefix -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) PACKAGE_JSON = "package.json" diff --git a/charon/pkgs/pkg_utils.py b/charon/pkgs/pkg_utils.py index c6fdef34..ab3e3546 100644 --- a/charon/pkgs/pkg_utils.py +++ b/charon/pkgs/pkg_utils.py @@ -1,10 +1,8 @@ from typing import List -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger import sys -import logging -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) def is_metadata(file: str) -> bool: diff --git a/charon/storage.py b/charon/storage.py index cb5d1587..4fcdc9c1 100644 --- a/charon/storage.py +++ b/charon/storage.py @@ -15,7 +15,7 @@ """ from boto3_type_annotations.s3.service_resource import Object from charon.utils.files import read_sha1 -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger from charon.constants import PROD_INFO_SUFFIX from boto3 import session @@ -25,11 +25,9 @@ from boto3_type_annotations import s3 from typing import Callable, Dict, List, Optional, Tuple import os -import logging import mimetypes -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) PRODUCT_META_KEY = "rh-products" CHECKSUM_META_KEY = "checksum" diff --git a/charon/utils/archive.py b/charon/utils/archive.py index 05e7e699..dc379eff 100644 --- a/charon/utils/archive.py +++ b/charon/utils/archive.py @@ -13,7 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. """ -import logging import os import sys import tarfile @@ -24,10 +23,9 @@ from json import load, JSONDecodeError from typing import Tuple from zipfile import ZipFile, is_zipfile -from charon.utils.logs import add_file_handler +from charon.utils.logs import getLogger -logger = logging.getLogger(__name__) -add_file_handler(logger) +logger = getLogger(__name__) def extract_zip_all(zf: ZipFile, target_dir: str): diff --git a/charon/utils/logs.py b/charon/utils/logs.py index 1ead0fa1..50ae7db5 100644 --- a/charon/utils/logs.py +++ b/charon/utils/logs.py @@ -74,10 +74,14 @@ def set_logging(name="charon", level=logging.DEBUG, handler=None): hdlr.setFormatter(formatter) -def add_file_handler(logger: logging): - handler = logging.FileHandler('errors.log') - handler.setLevel(logging.ERROR) - handler.setLevel(logging.WARN) +def getLogger(name: str) -> logging: + error_log = "./errors.log" + handler = logging.FileHandler(error_log) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) + handler.setLevel(logging.ERROR) + handler.setLevel(logging.WARN) + + logger = logging.getLogger(name) logger.addHandler(handler) + return logger From 4fbe73e687bb0d8584ff3e7115973a49fbeabb13 Mon Sep 17 00:00:00 2001 From: yma Date: Fri, 28 Jan 2022 11:19:06 +0800 Subject: [PATCH 3/4] Remove duplicated log level setting for handler --- charon/utils/logs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/charon/utils/logs.py b/charon/utils/logs.py index 50ae7db5..c736cbf3 100644 --- a/charon/utils/logs.py +++ b/charon/utils/logs.py @@ -79,7 +79,6 @@ def getLogger(name: str) -> logging: handler = logging.FileHandler(error_log) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) - handler.setLevel(logging.ERROR) handler.setLevel(logging.WARN) logger = logging.getLogger(name) From 9fd6522a5b2e537a283d5788a44e9cfddb987785 Mon Sep 17 00:00:00 2001 From: yma Date: Sun, 30 Jan 2022 12:45:05 +0800 Subject: [PATCH 4/4] Define errors log location and name it with product & version --- charon/cmd/command.py | 16 ++++++++-------- charon/config.py | 4 ++-- charon/constants.py | 2 ++ charon/pkgs/indexing.py | 4 ++-- charon/pkgs/maven.py | 4 ++-- charon/pkgs/npm.py | 4 ++-- charon/pkgs/pkg_utils.py | 4 ++-- charon/storage.py | 4 ++-- charon/utils/archive.py | 4 ++-- charon/utils/logs.py | 23 +++++++++++++---------- 10 files changed, 37 insertions(+), 32 deletions(-) diff --git a/charon/cmd/command.py b/charon/cmd/command.py index 3e40af86..46ada2d7 100644 --- a/charon/cmd/command.py +++ b/charon/cmd/command.py @@ -15,7 +15,7 @@ """ from typing import List from charon.config import CharonConfig, get_config -from charon.utils.logs import set_logging, getLogger +from charon.utils.logs import set_logging from charon.utils.archive import detect_npm_archive, download_archive, NpmArchiveType from charon.pkgs.maven import handle_maven_uploading, handle_maven_del from charon.pkgs.npm import handle_npm_uploading, handle_npm_del @@ -28,7 +28,7 @@ import os import sys -logger = getLogger(__name__) +logger = logging.getLogger(__name__) @argument( @@ -126,7 +126,7 @@ def upload( """ tmp_dir = work_dir try: - __decide_mode(is_quiet=quiet, is_debug=debug) + __decide_mode(product, version, is_quiet=quiet, is_debug=debug) if dryrun: logger.info("Running in dry-run mode," "no files will be uploaded.") @@ -279,7 +279,7 @@ def delete( """ tmp_dir = work_dir try: - __decide_mode(is_quiet=quiet, is_debug=debug) + __decide_mode(product, version, is_quiet=quiet, is_debug=debug) if dryrun: logger.info("Running in dry-run mode," "no files will be deleted.") @@ -386,17 +386,17 @@ def __validate_prod_key(product: str, version: str) -> bool: return True -def __decide_mode(is_quiet: bool, is_debug: bool): +def __decide_mode(product: str, version: str, is_quiet: bool, is_debug: bool): if is_quiet: logger.info("Quiet mode enabled, " "will only give warning and error logs.") - set_logging(level=logging.WARNING) + set_logging(product, version, level=logging.WARNING) elif is_debug: logger.info("Debug mode enabled, " "will give all debug logs for tracing.") - set_logging(level=logging.DEBUG) + set_logging(product, version, level=logging.DEBUG) else: - set_logging(level=logging.INFO) + set_logging(product, version, level=logging.INFO) @group() diff --git a/charon/config.py b/charon/config.py index ee60b705..18ecdd42 100644 --- a/charon/config.py +++ b/charon/config.py @@ -16,14 +16,14 @@ from typing import Dict, List from ruamel.yaml import YAML from pathlib import Path -from charon.utils.logs import getLogger import os +import logging from charon.utils.strings import remove_prefix CONFIG_FILE = "charon.yaml" -logger = getLogger(__name__) +logger = logging.getLogger(__name__) class CharonConfig(object): diff --git a/charon/constants.py b/charon/constants.py index 242030f3..0fdc01b1 100644 --- a/charon/constants.py +++ b/charon/constants.py @@ -175,3 +175,5 @@ ''' PROD_INFO_SUFFIX = ".prodinfo" + +DEFAULT_ERRORS_LOG = "errors.log" diff --git a/charon/pkgs/indexing.py b/charon/pkgs/indexing.py index 8532e34e..bcb5d1f5 100644 --- a/charon/pkgs/indexing.py +++ b/charon/pkgs/indexing.py @@ -17,14 +17,14 @@ from charon.storage import S3Client from charon.constants import (INDEX_HTML_TEMPLATE, NPM_INDEX_HTML_TEMPLATE, PACKAGE_TYPE_MAVEN, PACKAGE_TYPE_NPM, PROD_INFO_SUFFIX) -from charon.utils.logs import getLogger from jinja2 import Template import os +import logging from typing import List, Set from charon.utils.strings import remove_prefix -logger = getLogger(__name__) +logger = logging.getLogger(__name__) def __get_index_template(package_type: str) -> str: diff --git a/charon/pkgs/maven.py b/charon/pkgs/maven.py index 5cfab692..dd60c2e5 100644 --- a/charon/pkgs/maven.py +++ b/charon/pkgs/maven.py @@ -25,7 +25,6 @@ META_FILE_FAILED, MAVEN_METADATA_TEMPLATE, ARCHETYPE_CATALOG_TEMPLATE, ARCHETYPE_CATALOG_FILENAME, PACKAGE_TYPE_MAVEN) -from charon.utils.logs import getLogger from typing import Dict, List, Tuple from jinja2 import Template from datetime import datetime @@ -35,9 +34,10 @@ import os import sys +import logging import re -logger = getLogger(__name__) +logger = logging.getLogger(__name__) def __get_mvn_template(kind: str, default: str) -> str: diff --git a/charon/pkgs/npm.py b/charon/pkgs/npm.py index fcb14744..52477e57 100644 --- a/charon/pkgs/npm.py +++ b/charon/pkgs/npm.py @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. """ +import logging import os import sys from json import load, loads, dump, JSONDecodeError @@ -28,9 +29,8 @@ from charon.utils.archive import extract_npm_tarball from charon.pkgs.pkg_utils import upload_post_process, rollback_post_process from charon.utils.strings import remove_prefix -from charon.utils.logs import getLogger -logger = getLogger(__name__) +logger = logging.getLogger(__name__) PACKAGE_JSON = "package.json" diff --git a/charon/pkgs/pkg_utils.py b/charon/pkgs/pkg_utils.py index ab3e3546..a3e0b354 100644 --- a/charon/pkgs/pkg_utils.py +++ b/charon/pkgs/pkg_utils.py @@ -1,8 +1,8 @@ from typing import List -from charon.utils.logs import getLogger import sys +import logging -logger = getLogger(__name__) +logger = logging.getLogger(__name__) def is_metadata(file: str) -> bool: diff --git a/charon/storage.py b/charon/storage.py index 4fcdc9c1..99a46d9e 100644 --- a/charon/storage.py +++ b/charon/storage.py @@ -15,7 +15,6 @@ """ from boto3_type_annotations.s3.service_resource import Object from charon.utils.files import read_sha1 -from charon.utils.logs import getLogger from charon.constants import PROD_INFO_SUFFIX from boto3 import session @@ -25,9 +24,10 @@ from boto3_type_annotations import s3 from typing import Callable, Dict, List, Optional, Tuple import os +import logging import mimetypes -logger = getLogger(__name__) +logger = logging.getLogger(__name__) PRODUCT_META_KEY = "rh-products" CHECKSUM_META_KEY = "checksum" diff --git a/charon/utils/archive.py b/charon/utils/archive.py index dc379eff..3dbd2081 100644 --- a/charon/utils/archive.py +++ b/charon/utils/archive.py @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. """ +import logging import os import sys import tarfile @@ -23,9 +24,8 @@ from json import load, JSONDecodeError from typing import Tuple from zipfile import ZipFile, is_zipfile -from charon.utils.logs import getLogger -logger = getLogger(__name__) +logger = logging.getLogger(__name__) def extract_zip_all(zf: ZipFile, target_dir: str): diff --git a/charon/utils/logs.py b/charon/utils/logs.py index c736cbf3..a832b562 100644 --- a/charon/utils/logs.py +++ b/charon/utils/logs.py @@ -15,10 +15,10 @@ """ import logging import sys +import os from locale import nl_langinfo, CODESET -from os import fdopen, dup -from charon.constants import CHARON_LOGGING_FMT +from charon.constants import CHARON_LOGGING_FMT, DEFAULT_ERRORS_LOG class EncodedStream(object): @@ -26,7 +26,7 @@ class EncodedStream(object): # over stderr. Normal techniques were not working, so we dup # the file handler and force it UTF-8. :-( def __init__(self, fileno, encoding): - self.binarystream = fdopen(dup(fileno), 'wb') + self.binarystream = os.fdopen(os.dup(fileno), 'wb') self.encoding = encoding def write(self, text): @@ -45,7 +45,7 @@ def __del__(self): pass -def set_logging(name="charon", level=logging.DEBUG, handler=None): +def set_logging(product: str, version: str, name="charon", level=logging.DEBUG, handler=None): # create logger logger = logging.getLogger(name) for hdlr in list(logger.handlers): # make a copy so it doesn't change @@ -69,18 +69,21 @@ def set_logging(name="charon", level=logging.DEBUG, handler=None): # add ch to logger logger.addHandler(handler) + set_log_file_handler(product, version, logger) + logger = logging.getLogger('charon') for hdlr in list(logger.handlers): # make a copy so it doesn't change hdlr.setFormatter(formatter) -def getLogger(name: str) -> logging: - error_log = "./errors.log" +def set_log_file_handler(product: str, version: str, logger: logging): + log_loc = os.getenv("ERROR_LOG_LOCATION") + error_log = "".join([product, "-", version, ".", DEFAULT_ERRORS_LOG]) + if log_loc: + os.makedirs(log_loc, exist_ok=True) + error_log = os.path.join(log_loc, error_log) handler = logging.FileHandler(error_log) - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') + formatter = logging.Formatter(fmt=CHARON_LOGGING_FMT) handler.setFormatter(formatter) handler.setLevel(logging.WARN) - - logger = logging.getLogger(name) logger.addHandler(handler) - return logger