title: Logging to file in Python across modules/class (again!)
body: <p>After reviewing <a href="https://stackoverflow.com/questions/16947234/python-logging-across-multiple-modules">this thread</a> and <a href="https://stackoverflow.com/questions/47886582/logging-to-a-single-file-across-different-modules-in-python">this thread</a>, I still don't quite &quot;get&quot; the concept of using <code>logging</code> in Python 3</p>
  <p>Current Python is 3.9 with Flask, under Windows.</p>
  <p>Here is the flow of the program:</p>
  <p>in <code>wsgi.py</code></p>
  <pre><code>from api import app
  </code></pre>
  <p>Then in <code>api.py</code>, it has:</p>
  <pre><code>import logging
  from utils import setup_logger
  log = setup_logger()
  
  ...
  rp = ReportParser()
  ...
  
  @app.route('/invocations/pdf', methods=['POST'], endpoint=&quot;invocations_pdf&quot;)
  def invocations_pdf():
      result = rp.parse(sometext) # &lt;&lt;&lt; I have log.debug() in this function, but it did not save log to file
      log.debug(&quot;RUN call API.&quot;)  # The log.debug() here did not save log to file
  
      return jsonify({'result':result})
  
  if __name__ == '__main__':
      app.run()
  </code></pre>
  <p>In <code>utils.py</code> &gt; <code>setup_logger()</code>:</p>
  <pre><code>import logging
  
  from dotenv import load_dotenv, dotenv_values
  load_dotenv()
  
  def setup_logger():
      env = dotenv_values(&quot;.env&quot;)
  
      cust_log_level = logging.DEBUG
      if('LOG_LEVEL' in env.keys()):
          if(env['LOG_LEVEL'] == &quot;CRITICAL&quot;):
              cust_log_level = logging.CRITICAL
          elif(env['LOG_LEVEL'] == &quot;ERROR&quot;):
              cust_log_level = logging.ERROR
          elif(env['LOG_LEVEL'] == &quot;WARNING&quot;):
              cust_log_level = logging.WARNING
          elif(env['LOG_LEVEL'] == &quot;INFO&quot;):
              cust_log_level = logging.INFO
          elif(env['LOG_LEVEL'] == &quot;DEBUG&quot;):
              cust_log_level = logging.DEBUG
  
  
      # create logger
      log = logging.getLogger(&quot;med_rpt&quot;)
  
      if('SAVE_LOG' in env.keys() and env['SAVE_LOG'] == &quot;1&quot;):
          # create console handler and set level to debug
          ch = logging.FileHandler(&quot;api.log&quot;, encoding=&quot;utf8&quot;)
          ch.setLevel(cust_log_level)
  
          # add formatter to ch
          ch.setFormatter(logging.Formatter('%(asctime)s|%(name)s|%(levelname)s|%(message)s'))
          log.addHandler(ch)
      else:
          log.setLevel(cust_log_level)
  
      return log
  
  </code></pre>
  <p>Then the class <code>parse_report.py</code></p>
  <pre><code>import logging
  log = logging.getLogger(&quot;med_rpt&quot;)
  
  class ReportParser:
      def __init__(self, config_path):
          ...
  
      def parse(text):
          log.debug(&quot;Run [Parse]&quot;)
  ...
  
  </code></pre>
  <p><strong>Question #1</strong></p>
  <ol>
  <li>Why the <code>log.debug()</code> in <code>invocations_pdf()</code> cannot save log to file?</li>
  <li>Why the <code>log.debug()</code> in <code>ReportParser.parse(text)</code> cannot save log to file?</li>
  </ol>
  <p>Can you please help to clarify how to use this <code>logging</code> module in python?
  How do I make it work?</p>
  <p>Thank you very much.</p>