title: Django rest framework logging different levels on different files
body: <p>I am working on <a href="http://www.django-rest-framework.org" rel="noreferrer">Django REST framework</a> and I want to have separate files for logging data.</p>
  
  <p>I want to have a file for the simple transactions e.g. GET, PUT, POST etc. and one file with the errors that I will collect in case of an error.</p>
  
  <p>I have been reading the <a href="https://docs.djangoproject.com/en/2.0/topics/logging/" rel="noreferrer">Django Logging Documentation</a> and I came up with some configurations on how to log the info data. Sample of configurations bellow:</p>
  
  <p><strong>settings.py</strong></p>
  
  <pre><code>STATIC_URL = '/static/'
  PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
  STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
  LOGGING_ROOT = os.path.join(STATIC_ROOT, 'logging')
  
  LOGGING = {
      'version': 1,
      'disable_existing_loggers': False,
      'handlers': {
          'file': {
              'level': 'INFO',
              'class': 'logging.FileHandler',
              'filename': LOGGING_ROOT + "/info.log",
          },
      },
      'loggers': {
          'django': {
              'handlers': ['file'],
              'level': 'INFO',
              'propagate': True,
          },
      },
  }
  </code></pre>
  
  <p>It works as expected sample of data in file:</p>
  
  <pre><code>"PUT /upload/dat.txt HTTP/1.1" 204 14
  "OPTIONS / HTTP/1.1" 200 10020
  "GET / HTTP/1.1" 200 9916
  </code></pre>
  
  <p>I tried to apply another handler in the settings.py file e.g.:</p>
  
  <pre><code>LOGGING = {
      'version': 1,
      'disable_existing_loggers': False,
      'handlers': {
          'fileInfo': {
              'level': 'INFO',
              'class': 'logging.FileHandler',
              'filename': LOGGING_ROOT + "/info.log",
          },
          'fileDebug': {
              'level': 'DEBUG',
              'class': 'logging.FileHandler',
              'filename': LOGGING_ROOT + "/debbug.log",
          },
      },
      'loggers': {
          'django': {
              'handlers': ['fileInfo'],
              'level': 'INFO',
              'propagate': True,
          },
          'django': {
              'handlers': ['fileDebug'],
              'level': 'DEBUG',
              'propagate': True,
          },
      },
  }
  </code></pre>
  
  <p>I does not work as expected, now I am getting all the data (INFO and DEBUG) on the same file debug.log. So I decided to have another approach through the logging library. What I can not figure out is how to pipe the output of these log errors into the directory e.g. (path/errors.log).</p>
  
  <p>From the documentation:</p>
  
  <pre><code># import the logging library
  import logging
  
  # Get an instance of a logger
  logger = logging.getLogger(__name__)
  
  def my_view(request, arg1, arg):
      ...
      if bad_mojo:
          # Log an error message
          logger.error('Something went wrong!')
  </code></pre>
  
  <p>I found this similar question <a href="https://stackoverflow.com/questions/44188270/no-handlers-could-be-found-for-logger">No handlers could be found for logger</a> and it works if I apply the following:</p>
  
  <pre><code>import logging
  logging.basicConfig()
  logger = logging.getLogger(__name__)
  logger.error('Something went wrong!')
  </code></pre>
  
  <p>Now everything is logged on the stdout (INFO and ERROR). Is there a way to combine those configurations?</p>
  
  <p>I want to log all requests into the info.log file, and log errors in the error.log file when I choose on my file.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Solution provided by <a href="https://stackoverflow.com/users/41316/bruno-desthuilliers">bruno desthuilliers</a> sample bellow:</p>
  
  <p><strong>settings.py</strong></p>
  
  <pre><code>LOGGING = {
      'version': 1,
      'disable_existing_loggers': False,
      'handlers': {
          'fileInfo': {
              'level': 'INFO',
              'class': 'logging.FileHandler',
              'filename': os.path.join(LOGGING_ROOT, "info.log"),
          },
          'fileDebug': {
              'level': 'DEBUG',
              'class': 'logging.FileHandler',
              'filename': os.path.join(LOGGING_ROOT, "debug.log")
          },
          'console': {
              'class': 'logging.StreamHandler',
          },
      },
      'loggers': {
          'django': {
              'handlers': ['console', 'fileInfo', 'fileDebug'],
              'level': 'DEBUG',
              'propagate': True,
          },
      },
  }
  </code></pre>
  
  <p>I also found <a href="https://stackoverflow.com/questions/14058453/making-python-loggers-output-all-messages-to-stdout-in-addition-to-log">Making Python loggers output all messages to stdout in addition to log</a>. Sample of my current modifications:</p>
  
  <pre><code>import logging
  from thanosTest import settings
  logging.basicConfig(filename=os.path.join(settings.LOGGING_ROOT, "error.log"))
  stderrLogger = logging.StreamHandler()
  stderrLogger.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
  logging.getLogger().addHandler(stderrLogger)
  logging.error('Something went wrong!')
  </code></pre>
  
  <p>Now everything gets logged on the error.log. I guess I need to apply some filtering or something. I will try to figure it out.</p>