Sunday, 1 December 2019

How to use request_id while logging in asynchronous functions?

In asynchronous functions, every logger statement is getting their own request_id.

import logging
log = logging.getLogger('test_logger')

def sync_fun():
    log.info("test 1")
    log.info("test 2")
    log.info("test 3")

@after_response.enable
def async_fun():
    log.info("test 1")
    log.info("test 2")
    log.info("test 3")    

output of sync_fun:
[06/Nov/2019 10:42:00.234] [None] [130C6C47F1E24164AAC0440C719630] [INFO] Test 1
[06/Nov/2019 10:42:00.234] [None] [130C6C47F1E24164AAC0440C719630] [INFO] Test 2
[06/Nov/2019 10:42:00.234] [None] [130C6C47F1E24164AAC0440C719630] [INFO] Test 3

130C6C47F1E24164AAC0440C719630 is a request_id and its common for all logger statements.

output of async_fun:
[06/Nov/2019 10:42:00.234] [None] [AB352B8F2DF9459ABDD2FBF51EB05F] [INFO] Test 1
[06/Nov/2019 10:42:00.234] [None] [V9E9B6DF5F9C442195EA7C1379FBFA] [INFO] Test 2
[06/Nov/2019 10:42:00.234] [None] [DCA311A92724443C9AD7E951288917] [INFO] Test 3

async_fun is an asynchronous function and request ids are different for all logger statements.

How do i get the same request_id for each logger statements in asynchronous function.



from How to use request_id while logging in asynchronous functions?

No comments:

Post a Comment