Skip to content

Reduce CloudWatch logger verbosity to prevent log spam #2

@AnthonyRonning

Description

@AnthonyRonning

Problem

The CloudWatch logger in the logging container generates excessive log entries, creating noise and making it difficult to find important information. Every connection and message is logged at INFO level, leading to redundant entries.

Solution

Reduce logging verbosity by changing INFO level logs to DEBUG for routine operations.

Code Changes

# cloudwatch_logger.py changes

def handle_client(conn, addr, cloudwatch, log_group, log_stream):
    logger.debug(f"Connected by {addr}")  # Changed from logger.info
    try:
        while True:
            data = conn.recv(1024)
            if not data:
                break
            message = data.decode()
            # Only log the actual message being sent, not the content
            logger.debug(f"Forwarding log to CloudWatch")  # Changed from logger.info with message content
            try:
                cloudwatch.put_log_events(
                    logGroupName=log_group,
                    logStreamName=log_stream,
                    logEvents=[{
                        'timestamp': int(time.time() * 1000),
                        'message': message
                    }]
                )
                # Don't log the message content again - it's already in CloudWatch
                # Removed redundant logger.info here
            except Exception as e:
                logger.error(f"Error sending log to CloudWatch: {e}")
    except Exception as e:
        logger.error(f"Error handling connection: {e}")
    finally:
        conn.close()
        logger.debug(f"Connection closed for {addr}")  # Changed from logger.info

Benefits

  1. Reduced log noise: Connection events logged at DEBUG level
  2. No duplicate logging: Message content only appears once (in CloudWatch)
  3. Cleaner logs: Easier to find actual errors and important events
  4. Better debugging: Can still enable DEBUG logging when needed

Impact

  • Reduces Docker container log size
  • Makes CloudWatch logs more readable
  • Preserves all actual application logs while removing infrastructure noise

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions