Skip to content

Commit 5ea8af3

Browse files
committed
Add local logging support
1 parent fd32d61 commit 5ea8af3

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

config/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"features": {
33
"show_log_locally": false,
4-
"use_local_config": false
4+
"write_data_locally": false
55
}
66
}

src/main.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import json
99
import logging
1010
import os
11-
from typing import Optional, Any
11+
from typing import Any, Optional
1212

1313
import boto3
1414
import github_api_toolkit
@@ -33,6 +33,8 @@
3333

3434
logger = logging.getLogger()
3535

36+
logger.setLevel(logging.INFO)
37+
3638
# Example Log Output:
3739
#
3840
# Standard output:
@@ -248,7 +250,11 @@ def create_dictionary(
248250

249251

250252
def update_s3_object(
251-
s3_client: boto3.client, bucket_name: str, object_name: str, data: dict
253+
s3_client: boto3.client,
254+
bucket_name: str,
255+
object_name: str,
256+
data: dict,
257+
write_data_locally: bool = False, # TODO write_data_locally
252258
) -> bool:
253259
"""Update an S3 object with new data.
254260
@@ -339,7 +345,9 @@ def get_config_file(path: str) -> Any:
339345
raise Exception(error_message) from None
340346

341347
if type(config) is not dict:
342-
error_message = f"{path} configuration file is not a dictionary. Please check the file contents."
348+
error_message = (
349+
f"{path} configuration file is not a dictionary. Please check the file contents."
350+
)
343351
raise Exception(error_message)
344352

345353
return config
@@ -362,20 +370,34 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument
362370
Returns:
363371
str: Completion message.
364372
"""
365-
366373
# Load config file
367374
config = get_config_file("./config/config.json")
368375

369376
features = get_dict_value(config, "features")
370377

378+
show_log_locally = get_dict_value(features, "show_log_locally")
379+
380+
write_data_locally = get_dict_value(features, "write_data_locally")
381+
382+
# Remove any existing handlers to avoid duplicate logs
383+
if logger.hasHandlers():
384+
logger.handlers.clear()
385+
386+
# Toggle local logging
387+
if show_log_locally:
388+
# Add a StreamHandler to log to the console
389+
console_handler = logging.StreamHandler()
390+
console_handler.setLevel(logging.INFO)
391+
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
392+
console_handler.setFormatter(formatter)
393+
logger.addHandler(console_handler)
394+
371395
# Create an S3 client
372396
session = boto3.Session()
373397
s3 = session.client("s3")
374398

375399
logger.info("S3 client created")
376400

377-
# TODO: Check whether to use local config or cloud config
378-
379401
# Get the .pem file from AWS Secrets Manager
380402
secret_manager = session.client("secretsmanager", region_name=secret_region)
381403

0 commit comments

Comments
 (0)