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

watchtower is not sending an event to CW? Issue connected to fastapi? #174

Closed
Leem0sh opened this issue Mar 28, 2022 · 0 comments
Closed

Comments

@Leem0sh
Copy link

Leem0sh commented Mar 28, 2022

I made a simple example:

advanced_logging.py

# -*- encoding: utf-8 -*-
# ! python3

from __future__ import annotations

import logging
from logging import StreamHandler
from typing import Final

from watchtower import CloudWatchLogHandler

logger: Final = logging.getLogger(__name__)


def add_custom_variables_to_logging():
    old_factory = logging.getLogRecordFactory()

    def record_factory(
            *args,
            **kwargs
    ):
        record = old_factory(*args, **kwargs)
        old_msg = record.msg
        cid = "abc123"
        record.msg = f"[{cid}] {old_msg}"
        return record

    logging.setLogRecordFactory(record_factory)


def advanced_logging(
        **kwargs
):
    logging.Formatter(
        "[%(asctime)s] %(levelname)s [%(name)s:%(module)s - %("
        "funcName)s:%(lineno)s] [%(cid)s] %(message)s"
    )
    logging.basicConfig(
        level=logging.INFO,
        datefmt="%d.%m.%Y %H:%M:%S",
        handlers=[CloudWatchLogHandler(**kwargs), StreamHandler()]
    )

    add_custom_variables_to_logging()

and here is FastAPI application:

api.py

# -*- encoding: utf-8 -*-
# ! python3

from __future__ import annotations

import logging
import os
from typing import Final, Union

import boto3
from fastapi import FastAPI

from advanced_logging import advanced_logging

logger: Final = logging.getLogger(__name__)


def create_boto3_logs_client(
        endpoint_url: Union[str, None] = None,
        region_name: Union[str, None] = None,
        aws_access_key_id: Union[str, None] = None,
        aws_secret_access_key: Union[str, None] = None
):
    return boto3.client(
        "logs",
        endpoint_url=endpoint_url,
        region_name=region_name,
        aws_access_key_id=aws_access_key_id,
        aws_secret_access_key=aws_secret_access_key
    )


LOGS_CLIENT = create_boto3_logs_client(
    endpoint_url=os.environ.get("AWS_HOST_URL", None),
    region_name=os.environ.get("AWS_DEFAULT_REGION", None),
    aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID", None),
    aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", None)
)

advanced_logging(
    boto3_client=LOGS_CLIENT,
    log_group_name="some_lg_name",
    log_stream_name="poggers"
)

app = FastAPI()


@app.get("/")
async def root():
    logger.info(f"This message should be sent to CW, right?")
    return {"message": "Hello World"}

run with uvicorn api:app --reload and go to 127.0.0.1:8000

I'm sure the connection is established bcs when I run the application, the log_group is always created in cloudwatch. But when I run the code itself (visiting 127.0.0.1:8000), no logs are created. I'm thinking about if for few days now and I can't find any working solution. It might be connected with FastAPI somehow? Or is the issue somewhere between the monitor and keyboard and I'm just blind? :)

Thank you!

@Leem0sh Leem0sh closed this as completed Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant