getslash / slash

The Slash testing infrastructure
https://getslash.github.io/slash
Other
75 stars 38 forks source link

Make logging handlers application bound by default #866

Open leugenel opened 6 years ago

leugenel commented 6 years ago
from slash import logger, Test
from threading import Timer

class LogInThread(Test):
    def test_general(self):
        logger.info("Start the program")
        Timer(1, self.execute).start()
    def execute(self):
        logger.info("Execute the thread")

The log doesn't fire from the execute method and works properly from test_general

vmalloc commented 6 years ago

Hmmm... that's correct. Slash handlers are thread bound and not application bound. I guess we should change that. Thanks for reporting!

leugenel commented 6 years ago

Thanks! Can I do something to fix it temporarily inside my code?

vmalloc commented 6 years ago

On second glance it looks like applicationbound is indeed used. I'll take a closer look at your example to see what's going on there...

Just to be clear - I think that in your example the thread outlives the test. Where do you expect the log to appear?

leugenel commented 6 years ago

I would expect to see it even at the console using -vvv flag

vmalloc commented 6 years ago

I think in your case it can actually outlive the session, meaning the global handler (including console) gets popped out of the stack...

leugenel commented 6 years ago

So what you recommend in this case? It looks like a simple use case...

vmalloc commented 6 years ago

The question is what you expect this example to do... Do you expect the thread to outlive the session?

If this is unintentional I would add a workaround that would wait on that thread on session cleanup (through add_cleanup(..., scope="session"))

Otherwise, you could initialize your own logbook handler (thread-bound) inside the thread, and take the current log path from context.session.log somehow...

leugenel commented 6 years ago

I see - thanks!