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

feat: Logger format update #82

Merged
merged 3 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.vscode/
.DS_Store
.idea
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
9 changes: 7 additions & 2 deletions eksupgrade/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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="%Y-%m-%d %H:%M:%S",
)
main(parsed_arguments)


Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/models/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,7 +19,9 @@
EKSClient = object
STSClient = object

logger = logging.getLogger(__name__)
from eksupgrade.utils import get_logger

logger = get_logger(__name__)


@dataclass
Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/models/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import base64
import datetime
import logging
import re
import time
from abc import ABC
Expand Down Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/src/boto_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/src/eks_get_image_type.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand Down
5 changes: 2 additions & 3 deletions eksupgrade/src/k8s_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from __future__ import annotations

import base64
import logging
import queue
import re
import threading
Expand All @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions eksupgrade/src/latest_ami.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/src/preflight_module.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Define the preflight module."""
from __future__ import annotations

import logging
from typing import Any, Dict, List

import boto3
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/src/self_managed.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand Down
5 changes: 3 additions & 2 deletions eksupgrade/starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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()

Expand Down
16 changes: 15 additions & 1 deletion eksupgrade/utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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):
Copy link
Contributor

@mbeacom mbeacom Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - return type and argument type here

"""Get a logger object with handler set 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