Skip to content

deliveryhero/at-gcp-logging

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

at-gcp-logging

This package consists of 3 parts:

How to use:

MIDDLEWARE = [
    ...,
    'at_gcp_logging.middleware.CaptureRequestData',
    'at_gcp_logging.middleware.LogRequestsGCP',
]

LOGGING = {
    'formatters': {
        'gcp': {
            '()': 'at_gcp_logging.formatter.GCPJSONFormatter'
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'gcp'
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'INFO'
        }
    }
}

Optionally you can add additional data to json either in settings:

LOGGING = {
    'formatters': {
        'gcp': {
            '()': 'at_gcp_logging.formatter.GCPJSONFormatter',
            'format_dict': {
                'my-field': 'filename' #  map custom field to any valid attribute https://docs.python.org/3/library/logging.html#logrecord-attributes
            }   
        }
    }
}

or format log message as json:

import logging, json
logger = logging.getLogger(__name__)
payload = {
    'message': 'Request finished',
    'duration_ms': 1000,
    'status': response.status_code
}
logger.info(json.dumps(payload))

The result will be like this:

{"asctime": "2021-01-04 11:52:19,828", "name": "LogRequestsGCP", "severity": "INFO", "pathname": "/code/at-gcp-logging/at_gcp_logging/middleware.py", "lineno": 53, "thread": 139782117534496, "pid": 7, "exc_info": "", "stack_info": "", "user": "username", "ip": "172.18.0.1", "requested_path": "/api/v1/vendors/3db1cd38-2b44-41e7-8354-d8ff1c853602/carts/active/", "method": "GET", "user_agent": "PostmanRuntime/7.26.8", "message": "Request finished", "duration_ms": 15.895, "status": 200}

duration_ms and status are custom fields here

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages