1

I'm trying to implement django logging with AWS CloudWatch, after creating the user and entering the correct fields, following the guides scattered on the web, I still have an error:

ValueError: Unable to configure handler 'watchtower': __init__() got an unexpected keyword argument 'boto3_session'

This is my setting file (logging config):

boto3_connect_session = Session(
    aws_access_key_id=AWS_ACCESS_KEY_ID,
    aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
    region_name=AWS_REGION_NAME
)


LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'aws': {
            'format': u"%(asctime)s [%(levelname)-8s] %(message)s [%(pathname)s:%(lineno)d]",
            'datefmt': "%Y-%m-%d %H:%M:%S"
        },
        'simple': {
            'format': '[%(asctime)s %(module)s] %(levelname)s: %(message)s'
        },

    },
    'handlers': {
        'watchtower': {
            'level': 'DEBUG',
            'class': 'watchtower.CloudWatchLogHandler',
            'boto3_session': boto3_connect_session,
#                     'log_group': AWS_LOG_GROUP,
#                     'stream_name': AWS_LOG_STREAM,
            'formatter': 'aws',
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/tmp/logger.log',
            'formatter':'simple'
        },

    },
    'loggers': {
        AWS_LOGGER_NAME: {
            'level': 'DEBUG',
            'handlers': ['watchtower'],
            'propagate': False,
        },
        'local': {
            'level': 'DEBUG',
            'handlers': ['file'],
            'propagate': False,
        },

    },
}

full error message:

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib64/python3.6/logging/config.py", line 565, in configure
    handler = self.configure_handler(handlers[name])
  File "/usr/lib64/python3.6/logging/config.py", line 738, in configure_handler
    result = factory(**kwargs)
  File "/usr/local/lib/python3.6/site-packages/watchtower/__init__.py", line 203, in __init__
    super().__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'boto3_session'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib64/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.6/site-packages/django/utils/log.py", line 76, in configure_logging
    logging_config_func(logging_settings)
  File "/usr/lib64/python3.6/logging/config.py", line 802, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib64/python3.6/logging/config.py", line 573, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'watchtower': __init__() got an unexpected keyword argument 'boto3_session'

watchtower verions:

Name: watchtower
Version: 3.0.0

boto3 version:

Name: boto3
Version: 1.13.3

I can't figure out where the problem is, most guides use this system.

Thanks in advance.

Plaoo
  • 417
  • 3
  • 19

1 Answers1

2

In the official doc for watchtower with Django, It does not mention boto3_session in

'watchtower': {
            'level': 'DEBUG',
            'class': 'watchtower.CloudWatchLogHandler',
            'boto3_session': boto3_connect_session,
#                     'log_group': AWS_LOG_GROUP,
#                     'stream_name': AWS_LOG_STREAM,
            'formatter': 'aws',
        },

The document has boto3_client instead of boto3_session

Dharman
  • 30,962
  • 25
  • 85
  • 135
Arun T
  • 1,114
  • 6
  • 17
  • 1
    Yes, correct I managed to make the logs appear on Cloudwatch, but it seems strange to me that there are so many guides, even recent ones that explain in another way. Thanks. – Plaoo Feb 02 '22 at 14:16
  • True that, Even I saw a SO post which used boto3_session and people pointed out to him that he has to use boto3_client, but the OP was mentioning that boto3_session does work for him. But I always try to follow the official doc, since we can be sure that would be kept uptodate. :) – Arun T Feb 02 '22 at 14:22
  • 2
    It changed in v2.0.0. From the [version history](https://github.com/kislyuk/watchtower/blob/develop/Changes.rst) "Introduce the ability to pass a Boto3 logs client and remove the ability to pass Boto3 sessions" – David Buck Nov 11 '22 at 11:41