6

I am using Django watchtower to log events to Cloudwatch and have configured my logging in my settings file.

development.py

boto3_session = Session(
    aws_access_key_id=AWS_ACCESS_KEY_ID,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
    region_name=AWS_REGION)


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    # 'root': {
    #     'level': 'INFO',
    #     'handlers': ['console'],
    # },
    'formatters': {
        'simple': {
            'format': "%(asctime)s [%(levelname)-8s] %(message)s",
            'datefmt': "%Y-%m-%d %H:%M:%S"
        },
        'aws': {
            # you can add specific format for aws here
            'format': "%(asctime)s [%(levelname)-8s] %(message)s",
            'datefmt': "%Y-%m-%d %H:%M:%S"
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
        'watchtower': {
            'level': 'INFO',
            'class': 'watchtower.CloudWatchLogHandler',
            'boto3_session': boto3_session,
            'log_group': 'StagingBeagleApi',
            'stream_name': 'ApplicationLogStream',
            'formatter': 'aws',
        },
    },
    'loggers': {
        'django': {
            'level': 'INFO',
            'handlers': ['watchtower'],
            'propagate': True,
        },
    },
}

However when I run my server, I don't get any error in the console but my site is not accessible anymore via locahost:3000, I get an ERR_CONNECTION_REFUSED

Please help!

UPDATE

If I replace the django key with watchtower it works. However, I want to put all Django logs into Cloudwatch and I followed the documentation which has the logger key as django.

Cyzanfar
  • 6,720
  • 9
  • 37
  • 71
  • Does the aws user has permissions to write logs on cloudwatch? Also, why did you comment out `root` in logging config? Include root config with both handlers and test it out. – Chillar Anand Nov 09 '21 at 02:45
  • Yes it has the correct permission to write to CloudWatch. I tested with and without root and still doesn't work (as in localhost is still not accessible) – Cyzanfar Nov 09 '21 at 13:57
  • 1
    Based from [here](https://github.com/kislyuk/watchtower/blob/develop/watchtower/__init__.py#L115), watchtower does not expect `boto3_session` but instead requires `boto3_client`. – Brian Destura Nov 23 '21 at 22:57
  • That doesn't seem to be the issue, I have other handlers that have `boto3_session`. The issue seems to happen in development only for the `django` logger. – Cyzanfar Nov 23 '21 at 23:56
  • You're setting up just a Session not a Client. Documentation specifies using a client: boto3.client(region_name=AWS_REGION_NAME). cf: https://stackoverflow.com/questions/42809096/difference-in-boto3-between-resource-client-and-session – gregory Nov 24 '21 at 18:09
  • @gregory I get `Unable to configure handler 'watchtower'` when using boto3 client. – Cyzanfar Nov 28 '21 at 13:21
  • 1
    Having the same issue. – abhanan93 Dec 08 '21 at 23:19

1 Answers1

0

You can pass another value beside the array ['watchtower'], which may resolve the issue. You can check the below code as a sample.

'loggers': { 'django': { 'level': 'INFO', 'handlers': ['watchtower','django'], 'propagate': True, }, },

Dharman
  • 26,923
  • 21
  • 73
  • 125
Vishal
  • 153
  • 11