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

Integrate Watchtower logging into swarms #704

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions swarms/agents/ape_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from swarms.prompts.prompt_generator_optimizer import (
prompt_generator_sys_prompt,
)
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.loguru_logger import initialize_logger, initialize_watchtower_logger

logger = initialize_logger(log_folder="ape_agent")
logger = initialize_watchtower_logger(log_folder="ape_agent")


@retry(
Expand Down
4 changes: 2 additions & 2 deletions swarms/agents/tool_agent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any, Optional, Callable
from swarms.tools.json_former import Jsonformer
from swarms.utils.loguru_logger import initialize_logger
from swarms.utils.loguru_logger import initialize_logger, initialize_watchtower_logger
from swarms.utils.lazy_loader import lazy_import_decorator

logger = initialize_logger(log_folder="tool_agent")
logger = initialize_watchtower_logger(log_folder="tool_agent")


@lazy_import_decorator
Expand Down
1 change: 1 addition & 0 deletions swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from swarms.utils.wrapper_clusterop import (
exec_callable_with_clusterops,
)
from swarms.utils.loguru_logger import initialize_watchtower_logger


# Utils
Expand Down
11 changes: 10 additions & 1 deletion swarms/utils/loguru_logger.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os
import uuid
from loguru import logger
import watchtower

def initialize_watchtower_logger(log_group: str = "swarms_logs"):
handler = watchtower.CloudWatchLogHandler(log_group=log_group)
logger.add(handler)
return logger

def initialize_logger(log_folder: str = "logs"):
def initialize_logger(log_folder: str = "logs", use_watchtower: bool = False, log_group: str = "swarms_logs"):

AGENT_WORKSPACE = "agent_workspace"

Expand Down Expand Up @@ -34,4 +39,8 @@ def initialize_logger(log_folder: str = "logs"):
retention="10 days",
# compression="zip",
)

if use_watchtower:
initialize_watchtower_logger(log_group)

return logger
53 changes: 53 additions & 0 deletions terraform/logging.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
provider "aws" {
region = "us-west-2"
}

resource "aws_cloudwatch_log_group" "swarms_log_group" {
name = "swarms-log-group"
retention_in_days = 14
}

resource "aws_iam_role" "swarms_logging_role" {
name = "swarms-logging-role"

assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
}

resource "aws_iam_policy" "swarms_logging_policy" {
name = "swarms-logging-policy"
description = "Policy for allowing swarms to create and manage CloudWatch log groups"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutRetentionPolicy",
"logs:TagLogGroup"
]
Effect = "Allow"
Resource = "*"
}
]
})
}

resource "aws_iam_role_policy_attachment" "swarms_logging_role_attachment" {
role = aws_iam_role.swarms_logging_role.name
policy_arn = aws_iam_policy.swarms_logging_policy.arn
}
Loading