Skip to content

Commit

Permalink
having the colored formatted be setup like it used to be
Browse files Browse the repository at this point in the history
  • Loading branch information
brifordwylie committed Jan 21, 2025
1 parent 7d433c6 commit eff67d8
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/workbench/utils/cloudwatch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class CloudWatchHandler(logging.Handler):
"""A helper class to add a CloudWatch Logs handler with buffering to a logger"""

def __init__(self, buffer_size=10, send_interval=5):
def __init__(self, buffer_size=10, send_interval=5, theme="dark"):
super().__init__() # Initialize the base Handler class

# Buffer to hold log messages
Expand All @@ -39,24 +39,26 @@ def __init__(self, buffer_size=10, send_interval=5):
self.create_log_group()
self.create_log_stream()

# Set up ColoredFormatter
from workbench.utils.workbench_logging import ColoredFormatter # Avoid circular import
ColoredFormatter.set_theme(theme) # Ensure theme is consistent
self.setFormatter(
ColoredFormatter(
"(%(filename)s:%(lineno)d) %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
)

def emit(self, record):
"""Add a log message to the buffer and send when ready"""
if self.formatter:
message = self.format(record)
else:
message = record.getMessage()

message = self.format(record)
log_event = {"timestamp": int(record.created * 1000), "message": message}
self.buffer.append(log_event)

# Check if the buffer is full or if the time interval has passed
if len(self.buffer) >= self.buffer_size or (time.time() - self.last_sent_time) >= self.send_interval:
self.send_logs()

def setFormatter(self, formatter):
"""Set the formatter for the handler"""
super().setFormatter(formatter)

def send_logs(self):
"""Send buffered log messages to CloudWatch"""
if not self.buffer:
Expand Down Expand Up @@ -147,4 +149,4 @@ def get_unique_identifier(self, job_name):
cloudwatch_handler = CloudWatchHandler()
cloudwatch_handler.setFormatter(logging.Formatter("(%(filename)s:%(lineno)d) %(levelname)s %(message)s"))
logger.addHandler(cloudwatch_handler)
logger.info("Test log message to CloudWatch")
logger.info("Test log message to CloudWatch")

0 comments on commit eff67d8

Please sign in to comment.