title: How to make separate log file for error, warning in python?
body: <p>I want to make a separate log for errors, warning, and combined log for errors and warning in python. If I used python own logging mechanism to make log, I only can have one log file which can have errors, warning. I want to be more specific for that, I want to make a separate log only for errors, only for warnings, and combined log for errors and warning. I came across some logging function but I still can't make separate log files that what I expected. How can I fetch the errors, warning, and make separate log files for them? Is there any way of doing this? any possible thoughts? can anyone put me a possible approach to make this happen?</p>
  
  <p><strong>my current attempt</strong>:</p>
  
  <p>here is what I tried so far, and I couldn't make a separate log for errors and warning, I mean warning message from python terminal didn't make log still.</p>
  
  <pre><code>import logging
  
  def getLogger(LOG_NAME = '', LOG_FILE_INFO  = 'file.log', LOG_FILE_ERROR = 'file.err'):
      LOG_FORMAT = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
      log = logging.getLogger(LOG_NAME)
      log_formatter = logging.Formatter(LOG_FORMAT)
      # comment this to suppress console output
      stream_handler = logging.StreamHandler()
      stream_handler.setFormatter(log_formatter)
      log.addHandler(stream_handler)
  
      file_handler_info = logging.FileHandler(LOG_FILE_INFO, mode='w')
      file_handler_info.setFormatter(log_formatter)
      file_handler_info.setLevel(logging.INFO)
      log.addHandler(file_handler_info)
  
      file_handler_error = logging.FileHandler(LOG_FILE_ERROR, mode='w')
      file_handler_error.setFormatter(log_formatter)
      file_handler_error.setLevel(logging.ERROR)
      log.addHandler(file_handler_error)
      log.setLevel(logging.INFO)
      return log
  </code></pre>
  
  <p><strong>coding logic</strong></p>
  
  <p>here is the coding logic in my python function where I am gonna use log function to make separate log with <code>ifelse</code> condition:</p>
  
  <pre><code>if val == "combined":
      print("printing out combined log info including error, warning")
  elif val == "error":
       print("printing out error log info only")
  else:
      print("print out rlog info from running R functions")
  </code></pre>
  
  <p>how do I make this implementation better? how do I achieve the task of making a separate log for errors, warning, and combined logs of errors and warnings? any better idea or thoughts? thanks</p>
  
  <p><strong>update</strong>:</p>
  
  <p>this is app running session from python console, and I want to make log of this:</p>
  
  <pre><code>&gt;  * Environment: production    WARNING: This is a development server.
  &gt; Do not use it in a production deployment.    Use a production WSGI
  &gt; server instead.  * Debug mode: off  * Running on http://0.0.0.0:8080/
  &gt; (Press CTRL+C to quit)
  &gt; 127.0.0.1 - - [20/May/2020 10:31:37] "[37mGET /api/v1/ui/ HTTP/1.1[0m" 200 -
  &gt; 127.0.0.1 - - [20/May/2020 10:31:38] "[37mGET /api/v1/openapi.json HTTP/1.1[0m" 200 - 2020-05-20
  &gt; 10:31:52,545-immunomatch_ed-INFO-getting ready to retrieve immunoMatch
  &gt; System version... 2020-05-20 10:31:52,547-immunomatch_ed-WARNING-This
  &gt; is a WARNING message 2020-05-20 10:31:52,548-immunomatch_ed-ERROR-This
  &gt; is an ERROR message
  &gt; 127.0.0.1 - - [20/May/2020 10:31:52] "[37mGET /api/v1/about HTTP/1.1[0m" 200 - 2020-05-20
  &gt; 10:32:18,288-immunomatch_ed-INFO-getting ready to retrieve immunoMatch
  &gt; score...
  &gt; 127.0.0.1 - - [20/May/2020 10:32:19] "[37mPOST /api/v1/immunomatch_ed HTTP/1.1[0m" 200 - 2020-05-20
  &gt; 10:40:49,746-immunomatch_ed-INFO-getting ready to retrieve R session
  &gt; info... 2020-05-20 10:40:49,746-immunomatch_ed-WARNING-This is a
  &gt; WARNING message 2020-05-20 10:40:49,749-immunomatch_ed-ERROR-This is
  &gt; an ERROR message
  &gt; C:\Users\sam\AppData\Local\Programs\Python\Python37\lib\site-packages\connexion\apis\flask_api.py:199:
  &gt; FutureWarning: Implicit (flask) JSON serialization will change in the
  &gt; next major version. This is triggered because a response body is being
  &gt; serialized as JSON even though the mimetype is not a JSON type. This
  &gt; will be replaced by something that is mimetype-specific and may raise
  &gt; an error instead of silently converting everything to JSON. Please
  &gt; make sure to specify media/mime types in your specs.   FutureWarning 
  &gt; # a Deprecation targeted at application users.
  &gt; 127.0.0.1 - - [20/May/2020 10:40:50] "[37mGET /api/v1/rsession HTTP/1.1[0m" 200 - printing out combined log info including error,
  &gt; warning
  &gt; 127.0.0.1 - - [20/May/2020 10:41:38] "[37mGET /api/v1/log?type=combined HTTP/1.1[0m" 200 -
  </code></pre>
  
  <p>but above log function that I coded is not getting those to log file. How can I make a log of this? any idea?</p>