title: How to log to file per task if execution is within a Celery task context
body: <p>I want to modify my current logging setup without having to pass logger objects into functions as a parameter. Currently at the top of each module I define a LOG object</p>
  
  <pre><code>LOG = logging.getLogger(__name__)
  </code></pre>
  
  <p>I want to change the logging so that:</p>
  
  <ul>
  <li>the logs log to file, but only if the execution context is within a Celery task</li>
  <li>there is a file per task</li>
  <li>without having to modify the code of each task</li>
  </ul>
  
  <p>Thus if you have two tasks:</p>
  
  <pre><code>@task
  def taskOne():
    LOG.log("in task one")
    foo.foo()
  
  @task
  def taskTwo():
    LOG.log("in task two")
    foo.foo
  </code></pre>
  
  <p>and in module foo:</p>
  
  <pre><code>def foo():
    LOG.log("in foo")
  </code></pre>
  
  <p>Then:</p>
  
  <ul>
  <li>executing <code>taskOne</code> should send "in task one\nin foo" into a file named "taskOne.log"</li>
  <li>executing <code>taskTwo</code> should send "in task two\nin foo" into a file named "taskTwo.log"</li>
  <li>executing <code>foo</code> outside of a celery task should not send logs to a file</li>
  </ul>
  
  <p>I'm thinking of coming up with a handler that inspects the call stack to see if it is currently executing in the context of a celery task, and if so, log to a file. However I'm not sure whether the solution would be performant, or whether it's implementing bad practice.</p>