7

I am using Django watchtower to log events to AWS Cloudwatch and added logging configs in the 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,
    '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,997
  • 9
  • 43
  • 81
  • 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
  • I managed to resolve this error by adding ```logger.addHandler(CloudWatchLogHandler())``` – Glyn Jackson Nov 16 '22 at 17:41

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
  • 30,962
  • 25
  • 85
  • 135
Vishal
  • 128
  • 13