question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stream Not Created / Logs not written DJango 2

See original GitHub issue

I have the following configuration:

boto3_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,
    'root': {
        'level': LOG_LEVEL,
        'handlers': ['console'],
    },
    'formatters': {
        'simple': {
            'format': u"%(asctime)s [%(levelname)-8s] %(message)s",
            'datefmt': "%Y-%m-%d %H:%M:%S"
        },
        'aws': {
            # you can add specific format for aws here
            'format': u"%(asctime)s [%(levelname)-8s] %(message)s",
            'datefmt': "%Y-%m-%d %H:%M:%S"
        },
    },

    'handlers': {
        'watchtower': {
            'level': LOG_LEVEL,
            'class': 'watchtower.CloudWatchLogHandler',
            'boto3_session': boto3_session,
            'log_group': 'MyLogGroup',
            'create_log_group': False,
            'stream_name': 'ops',
            'formatter': 'aws',
        },
        'console': {
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django': {
            'level': LOG_LEVEL,
            'handlers': ['watchtower'],
            'propagate': False,
        },
        # add your other loggers here...
    },
}

When I send my logs, I see that the “MyLogGroup” is created, but not the stream and therefore no logs either. When i turn on debugging for botocore, I see the following:

Loading variable region from instance vars with value 'us-west-2'.
Loading variable profile from defaults.
Loading variable ca_bundle from defaults.
Loading variable profile from defaults.
Loading variable api_versions from defaults.
Loading variable profile from defaults.
Event choose-service-name: calling handler <function handle_service_name_alias at 0x10ca50048>
Event creating-client-class.logs: calling handler <function add_generate_presigned_url at 0x10ca1a9d8>
The s3 config key is not a dictionary type, ignoring its value of: None
Setting logs timeout as (60, 60)
Registering retry handlers for service: logs

I confirm when I run the configuration through this set of commands everything works as expected: import watchtower, logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(name) logger.addHandler(watchtower.CloudWatchLogHandler()) logger.info(“Hi”)

So it seems to me that the handler is not being registered correctly

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
kojimacommented, Oct 15, 2019

I’ve resolved this issue by setting use_queues: False for watchtower handler like below:

'watchtower': {
        'level': os.getenv('DJANGO_LOG_LEVEL', 'ERROR'),
        'class': 'watchtower.CloudWatchLogHandler',
        'boto3_session': boto3_session,
        'log_group': '<log group>',
        'stream_name': '<stream name>',
        'formatter': 'verbose',
        'use_queues': False,   # <--- This setting resolves the issue
},

Hope this setting also resolves your problem.

1reaction
kislyukcommented, Jan 30, 2021

@terencehonles thanks, this is a good point; I should probably just go ahead and use per-thread boto3 clients unless a custom one was passed in at init time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Log messages from Django application not uploaded in AWS ...
When I run the application it creates log group in AWS CloudWatch but log stream is not created and log messages also not...
Read more >
Logging - Django documentation
A logger is the entry point into the logging system. Each logger is a named bucket to which messages can be written for...
Read more >
Install and configure the CloudWatch Logs agent on a running ...
Step 1: Configure your IAM role or user for CloudWatch Logs · In the navigation pane, choose Roles. · Choose the role by...
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
Line 2: create a basicConf function and pass some arguments to create ... 0 and not equal to pre-defined logging levels as logging...
Read more >
How to Collect, Customize, and Centralize Python Logs
Python's built -in logging module is designed to give you critical ... In the example above, an error message was logged, but it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found