logging a coroutine aware variable Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Are static class variables possible?Using global variables in a functionHow do I pass a variable by reference?How to have git log show filenames like svn log -vHow to access environment variable values?uWSGI async functions don't know about Django's logging settingsHide strange unwanted Xcode logsHow to read all message from queue using stomp library in Python?python logging __name__ and packagingHow to configure logger in decorator class in python

Using audio cues to encourage good posture

Is it fair for a professor to grade us on the possession of past papers?

What does an IRS interview request entail when called in to verify expenses for a sole proprietor small business?

Why is my conclusion inconsistent with the van't Hoff equation?

What does this icon in iOS Stardew Valley mean?

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Why are there no cargo aircraft with "flying wing" design?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Why did the IBM 650 use bi-quinary?

The logistics of corpse disposal

Why light coming from distant stars is not discreet?

How to align text above triangle figure

What would be the ideal power source for a cybernetic eye?

What's the purpose of writing one's academic biography in the third person?

Error "illegal generic type for instanceof" when using local classes

How do I keep my slimes from escaping their pens?

How does debian/ubuntu knows a package has a updated version

Dating a Former Employee

Echoing a tail command produces unexpected output?

Fundamental Solution of the Pell Equation

Using et al. for a last / senior author rather than for a first author

Resolving to minmaj7

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

How discoverable are IPv6 addresses and AAAA names by potential attackers?



logging a coroutine aware variable



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Are static class variables possible?Using global variables in a functionHow do I pass a variable by reference?How to have git log show filenames like svn log -vHow to access environment variable values?uWSGI async functions don't know about Django's logging settingsHide strange unwanted Xcode logsHow to read all message from queue using stomp library in Python?python logging __name__ and packagingHow to configure logger in decorator class in python



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm following the worker example given in asyncio.Queue. In functions called by worker_task() I'd like to log the current worker name without having to pass it everywhere.



I suspect through some combination of LoggerAdapter and ContextVar it can do what I want.



Something along the following...



import logging
logging.basicConfig(format="%(worker_name)s %(message)s")
logger = logging.getLogger(__name__)

async def sub_function():
logger.info("a message") # worker-1: a message

async def worker_task(queue, worker_name):
logger.do_something(worker_name) # HELP HERE PLEASE
await sub_function()

queue = asyncio.Queue()

workers = [
loop.create_task(worker_task(queue, "worker-1"),
loop.create_task(worker_task(queue, "worker-2"),
]









share|improve this question



















  • 1





    Please always use a generic [python] tag for Python questions

    – juanpa.arrivillaga
    Mar 8 at 17:45

















1















I'm following the worker example given in asyncio.Queue. In functions called by worker_task() I'd like to log the current worker name without having to pass it everywhere.



I suspect through some combination of LoggerAdapter and ContextVar it can do what I want.



Something along the following...



import logging
logging.basicConfig(format="%(worker_name)s %(message)s")
logger = logging.getLogger(__name__)

async def sub_function():
logger.info("a message") # worker-1: a message

async def worker_task(queue, worker_name):
logger.do_something(worker_name) # HELP HERE PLEASE
await sub_function()

queue = asyncio.Queue()

workers = [
loop.create_task(worker_task(queue, "worker-1"),
loop.create_task(worker_task(queue, "worker-2"),
]









share|improve this question



















  • 1





    Please always use a generic [python] tag for Python questions

    – juanpa.arrivillaga
    Mar 8 at 17:45













1












1








1








I'm following the worker example given in asyncio.Queue. In functions called by worker_task() I'd like to log the current worker name without having to pass it everywhere.



I suspect through some combination of LoggerAdapter and ContextVar it can do what I want.



Something along the following...



import logging
logging.basicConfig(format="%(worker_name)s %(message)s")
logger = logging.getLogger(__name__)

async def sub_function():
logger.info("a message") # worker-1: a message

async def worker_task(queue, worker_name):
logger.do_something(worker_name) # HELP HERE PLEASE
await sub_function()

queue = asyncio.Queue()

workers = [
loop.create_task(worker_task(queue, "worker-1"),
loop.create_task(worker_task(queue, "worker-2"),
]









share|improve this question
















I'm following the worker example given in asyncio.Queue. In functions called by worker_task() I'd like to log the current worker name without having to pass it everywhere.



I suspect through some combination of LoggerAdapter and ContextVar it can do what I want.



Something along the following...



import logging
logging.basicConfig(format="%(worker_name)s %(message)s")
logger = logging.getLogger(__name__)

async def sub_function():
logger.info("a message") # worker-1: a message

async def worker_task(queue, worker_name):
logger.do_something(worker_name) # HELP HERE PLEASE
await sub_function()

queue = asyncio.Queue()

workers = [
loop.create_task(worker_task(queue, "worker-1"),
loop.create_task(worker_task(queue, "worker-2"),
]






python python-3.x logging python-asyncio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 17:45









juanpa.arrivillaga

39.4k33977




39.4k33977










asked Mar 8 at 17:43









KurtKurt

1,37021720




1,37021720







  • 1





    Please always use a generic [python] tag for Python questions

    – juanpa.arrivillaga
    Mar 8 at 17:45












  • 1





    Please always use a generic [python] tag for Python questions

    – juanpa.arrivillaga
    Mar 8 at 17:45







1




1





Please always use a generic [python] tag for Python questions

– juanpa.arrivillaga
Mar 8 at 17:45





Please always use a generic [python] tag for Python questions

– juanpa.arrivillaga
Mar 8 at 17:45












1 Answer
1






active

oldest

votes


















3














You could use something like this:



import logging
import asyncio
from contextvars import ContextVar

WorkerName = ContextVar('worker_name')

logging.basicConfig(format='%(worker_name)s %(message)s', level=logging.INFO)

class WorkerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
kwargs.setdefault('extra', )['worker_name'] = WorkerName.get()
return msg, kwargs

logger = WorkerAdapter(logging.getLogger(__name__), None)


async def sub_function():
logger.info('a message')

async def worker_task(worker_name):
WorkerName.set(worker_name)
await sub_function()

loop = asyncio.get_event_loop()

workers = [
loop.create_task(worker_task('worker-1')),
loop.create_task(worker_task('worker-2')),
]

loop.run_until_complete(asyncio.gather(*workers))





share|improve this answer























  • Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

    – Kurt
    Mar 8 at 23:23











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068373%2flogging-a-coroutine-aware-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You could use something like this:



import logging
import asyncio
from contextvars import ContextVar

WorkerName = ContextVar('worker_name')

logging.basicConfig(format='%(worker_name)s %(message)s', level=logging.INFO)

class WorkerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
kwargs.setdefault('extra', )['worker_name'] = WorkerName.get()
return msg, kwargs

logger = WorkerAdapter(logging.getLogger(__name__), None)


async def sub_function():
logger.info('a message')

async def worker_task(worker_name):
WorkerName.set(worker_name)
await sub_function()

loop = asyncio.get_event_loop()

workers = [
loop.create_task(worker_task('worker-1')),
loop.create_task(worker_task('worker-2')),
]

loop.run_until_complete(asyncio.gather(*workers))





share|improve this answer























  • Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

    – Kurt
    Mar 8 at 23:23















3














You could use something like this:



import logging
import asyncio
from contextvars import ContextVar

WorkerName = ContextVar('worker_name')

logging.basicConfig(format='%(worker_name)s %(message)s', level=logging.INFO)

class WorkerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
kwargs.setdefault('extra', )['worker_name'] = WorkerName.get()
return msg, kwargs

logger = WorkerAdapter(logging.getLogger(__name__), None)


async def sub_function():
logger.info('a message')

async def worker_task(worker_name):
WorkerName.set(worker_name)
await sub_function()

loop = asyncio.get_event_loop()

workers = [
loop.create_task(worker_task('worker-1')),
loop.create_task(worker_task('worker-2')),
]

loop.run_until_complete(asyncio.gather(*workers))





share|improve this answer























  • Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

    – Kurt
    Mar 8 at 23:23













3












3








3







You could use something like this:



import logging
import asyncio
from contextvars import ContextVar

WorkerName = ContextVar('worker_name')

logging.basicConfig(format='%(worker_name)s %(message)s', level=logging.INFO)

class WorkerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
kwargs.setdefault('extra', )['worker_name'] = WorkerName.get()
return msg, kwargs

logger = WorkerAdapter(logging.getLogger(__name__), None)


async def sub_function():
logger.info('a message')

async def worker_task(worker_name):
WorkerName.set(worker_name)
await sub_function()

loop = asyncio.get_event_loop()

workers = [
loop.create_task(worker_task('worker-1')),
loop.create_task(worker_task('worker-2')),
]

loop.run_until_complete(asyncio.gather(*workers))





share|improve this answer













You could use something like this:



import logging
import asyncio
from contextvars import ContextVar

WorkerName = ContextVar('worker_name')

logging.basicConfig(format='%(worker_name)s %(message)s', level=logging.INFO)

class WorkerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
kwargs.setdefault('extra', )['worker_name'] = WorkerName.get()
return msg, kwargs

logger = WorkerAdapter(logging.getLogger(__name__), None)


async def sub_function():
logger.info('a message')

async def worker_task(worker_name):
WorkerName.set(worker_name)
await sub_function()

loop = asyncio.get_event_loop()

workers = [
loop.create_task(worker_task('worker-1')),
loop.create_task(worker_task('worker-2')),
]

loop.run_until_complete(asyncio.gather(*workers))






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 8 at 19:04









panda-34panda-34

3,2591219




3,2591219












  • Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

    – Kurt
    Mar 8 at 23:23

















  • Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

    – Kurt
    Mar 8 at 23:23
















Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

– Kurt
Mar 8 at 23:23





Nice! A bit fussy since I had things in multiple files but this works a treat. Thanks.

– Kurt
Mar 8 at 23:23



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068373%2flogging-a-coroutine-aware-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко