From eda25adc6c558dab1337b0492c53d03e5c177f09 Mon Sep 17 00:00:00 2001 From: quixoticmonk Date: Fri, 17 Mar 2023 18:34:32 -0400 Subject: [PATCH 1/3] feat: Logger format updated to include timestamp --- .gitignore | 1 + eksupgrade/cli.py | 9 +++++++-- eksupgrade/models/base.py | 5 +++-- eksupgrade/models/eks.py | 5 +++-- eksupgrade/src/boto_aws.py | 5 +++-- eksupgrade/src/eks_get_image_type.py | 5 +++-- eksupgrade/src/k8s_client.py | 5 ++--- eksupgrade/src/latest_ami.py | 6 +++--- eksupgrade/src/preflight_module.py | 5 +++-- eksupgrade/src/self_managed.py | 5 +++-- eksupgrade/starter.py | 5 +++-- eksupgrade/utils.py | 16 +++++++++++++++- 12 files changed, 49 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 4753b92..4d2513c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .vscode/ .DS_Store +.idea # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/eksupgrade/cli.py b/eksupgrade/cli.py index 3d81b5c..a0b345c 100644 --- a/eksupgrade/cli.py +++ b/eksupgrade/cli.py @@ -8,8 +8,9 @@ from eksupgrade import __version__ from eksupgrade.starter import main +from eksupgrade.utils import get_logger -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def entry(args: Optional[List[str]] = None) -> None: @@ -76,7 +77,11 @@ def entry(args: Optional[List[str]] = None) -> None: ) parser.add_argument("--version", action="version", version=f"eksupgrade {__version__}") parsed_arguments = parser.parse_args(args) - logging.basicConfig(level=parsed_arguments.log_level.upper()) + logging.basicConfig( + level=parsed_arguments.log_level.upper(), + format="[%(levelname)s] : %(asctime)s : %(name)s : %(message)s", + datefmt="%d-%b-%y %H:%M:%S", + ) main(parsed_arguments) diff --git a/eksupgrade/models/base.py b/eksupgrade/models/base.py index 33fdd07..c6a11f2 100644 --- a/eksupgrade/models/base.py +++ b/eksupgrade/models/base.py @@ -1,7 +1,6 @@ """Define the base models to be used across the EKS upgrade tool.""" from __future__ import annotations -import logging from abc import ABC from dataclasses import dataclass, field from functools import cached_property @@ -20,7 +19,9 @@ EKSClient = object STSClient = object -logger = logging.getLogger(__name__) +from eksupgrade.utils import get_logger + +logger = get_logger(__name__) @dataclass diff --git a/eksupgrade/models/eks.py b/eksupgrade/models/eks.py index 77ffd20..66783ac 100644 --- a/eksupgrade/models/eks.py +++ b/eksupgrade/models/eks.py @@ -3,7 +3,6 @@ import base64 import datetime -import logging import re import time from abc import ABC @@ -58,7 +57,9 @@ AutoScalingGroupsTypeTypeDef = object AutoScalingGroupTypeDef = object -logger = logging.getLogger(__name__) +from eksupgrade.utils import get_logger + +logger = get_logger(__name__) TOKEN_PREFIX: str = "k8s-aws-v1" TOKEN_HEADER_KEY: str = "x-k8s-aws-id" diff --git a/eksupgrade/src/boto_aws.py b/eksupgrade/src/boto_aws.py index 9434f5e..6547e78 100644 --- a/eksupgrade/src/boto_aws.py +++ b/eksupgrade/src/boto_aws.py @@ -2,14 +2,15 @@ from __future__ import annotations import datetime -import logging import time import uuid from typing import Any, Dict, List, Optional import boto3 -logger = logging.getLogger(__name__) +from eksupgrade.utils import get_logger + +logger = get_logger(__name__) def status_of_cluster(cluster_name: str, region: str) -> List[str]: diff --git a/eksupgrade/src/eks_get_image_type.py b/eksupgrade/src/eks_get_image_type.py index 960d853..80d7b44 100644 --- a/eksupgrade/src/eks_get_image_type.py +++ b/eksupgrade/src/eks_get_image_type.py @@ -1,14 +1,15 @@ """Define the image type logic for EKS.""" from __future__ import annotations -import logging from typing import Optional import boto3 +from eksupgrade.utils import get_logger + from .k8s_client import find_node -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def image_type(node_type: str, image_id: str, region: str) -> Optional[str]: diff --git a/eksupgrade/src/k8s_client.py b/eksupgrade/src/k8s_client.py index f46c15d..f39a924 100644 --- a/eksupgrade/src/k8s_client.py +++ b/eksupgrade/src/k8s_client.py @@ -7,7 +7,6 @@ from __future__ import annotations import base64 -import logging import queue import re import threading @@ -29,9 +28,9 @@ from kubernetes import client, watch from kubernetes.client.rest import ApiException -from eksupgrade.utils import get_package_dict +from eksupgrade.utils import get_logger, get_package_dict -logger = logging.getLogger(__name__) +logger = get_logger(__name__) queue = queue.Queue() diff --git a/eksupgrade/src/latest_ami.py b/eksupgrade/src/latest_ami.py index 85d732b..9d61331 100644 --- a/eksupgrade/src/latest_ami.py +++ b/eksupgrade/src/latest_ami.py @@ -1,11 +1,11 @@ """Define the AMI specific logic.""" from __future__ import annotations -import logging - import boto3 -logger = logging.getLogger(__name__) +from eksupgrade.utils import get_logger + +logger = get_logger(__name__) def get_latest_ami(cluster_version: str, instance_type: str, image_to_search: str, region: str) -> str: diff --git a/eksupgrade/src/preflight_module.py b/eksupgrade/src/preflight_module.py index 1c1abf9..f528285 100644 --- a/eksupgrade/src/preflight_module.py +++ b/eksupgrade/src/preflight_module.py @@ -1,7 +1,6 @@ """Define the preflight module.""" from __future__ import annotations -import logging from typing import Any, Dict, List import boto3 @@ -15,7 +14,9 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) -logger = logging.getLogger(__name__) +from eksupgrade.utils import get_logger + +logger = get_logger(__name__) # Function declaration for pre flight checks diff --git a/eksupgrade/src/self_managed.py b/eksupgrade/src/self_managed.py index a9dfdc1..76d9447 100644 --- a/eksupgrade/src/self_managed.py +++ b/eksupgrade/src/self_managed.py @@ -1,15 +1,16 @@ """Define the self-managed node logic.""" from __future__ import annotations -import logging import time from typing import Any, Dict, List, Optional import boto3 +from eksupgrade.utils import get_logger + from .latest_ami import get_latest_ami -logger = logging.getLogger(__name__) +logger = get_logger(__name__) def status_of_cluster(cluster_name: str, region: str) -> List[str]: diff --git a/eksupgrade/starter.py b/eksupgrade/starter.py index 51c0ded..2936c2f 100644 --- a/eksupgrade/starter.py +++ b/eksupgrade/starter.py @@ -2,12 +2,13 @@ from __future__ import annotations import datetime -import logging import queue import sys import threading import time +from eksupgrade.utils import get_logger + from .exceptions import ClusterInactiveException from .models.eks import Cluster from .src.boto_aws import ( @@ -33,7 +34,7 @@ from .src.preflight_module import pre_flight_checks from .src.self_managed import update_nodegroup -logger = logging.getLogger(__name__) +logger = get_logger(__name__) queue = queue.Queue() diff --git a/eksupgrade/utils.py b/eksupgrade/utils.py index f8419e3..5ad21c3 100644 --- a/eksupgrade/utils.py +++ b/eksupgrade/utils.py @@ -1,7 +1,8 @@ """Define module level utilities to be used across the EKS Upgrade package.""" - import json +import logging import pkgutil +import sys def get_package_asset(filename: str, base_path: str = "src/S3Files/") -> str: @@ -13,3 +14,16 @@ def get_package_dict(filename: str, base_path: str = "src/S3Files/"): """Get the specified package asset data dictionary.""" _data = get_package_asset(filename, base_path) return json.loads(_data) + + +def get_logger(logger_name): + """Get a logger object with handler to StreamHandler""" + logger = logging.getLogger(logger_name) + console_handler = logging.StreamHandler(sys.stdout) + log_formatter = logging.Formatter( + "[%(levelname)s] : %(asctime)s : %(name)s.%(lineno)d : %(message)s", "%Y-%m-%d %H:%M:%S" + ) + console_handler.setFormatter(log_formatter) + logger.addHandler(console_handler) + logger.propagate = False + return logger From b0cebbaa0cb2e13ea6cd4306976141df3bb1c986 Mon Sep 17 00:00:00 2001 From: quixoticmonk Date: Fri, 17 Mar 2023 21:55:26 -0400 Subject: [PATCH 2/3] fix: logger fomat to match across methods --- eksupgrade/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eksupgrade/cli.py b/eksupgrade/cli.py index a0b345c..6db1937 100644 --- a/eksupgrade/cli.py +++ b/eksupgrade/cli.py @@ -80,7 +80,7 @@ def entry(args: Optional[List[str]] = None) -> None: logging.basicConfig( level=parsed_arguments.log_level.upper(), format="[%(levelname)s] : %(asctime)s : %(name)s : %(message)s", - datefmt="%d-%b-%y %H:%M:%S", + datefmt="%Y-%m-%d %H:%M:%S", ) main(parsed_arguments) From a69bc4cd0e6dab2275a52107b135f4470ad33659 Mon Sep 17 00:00:00 2001 From: quixoticmonk Date: Fri, 17 Mar 2023 22:31:25 -0400 Subject: [PATCH 3/3] Fixed typo in logger --- eksupgrade/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eksupgrade/utils.py b/eksupgrade/utils.py index 5ad21c3..dc0dfe1 100644 --- a/eksupgrade/utils.py +++ b/eksupgrade/utils.py @@ -17,7 +17,7 @@ def get_package_dict(filename: str, base_path: str = "src/S3Files/"): def get_logger(logger_name): - """Get a logger object with handler to StreamHandler""" + """Get a logger object with handler set to StreamHandler""" logger = logging.getLogger(logger_name) console_handler = logging.StreamHandler(sys.stdout) log_formatter = logging.Formatter(