# -*- coding: utf-8 -*-

"""Context manager that enables temporary changes in logging level.
Note: Not created by DataONE.
Source: https://docs.python.org/2/howto/logging-cookbook.html
"""
import logging
import sys


class LoggingContext(object):
  def __init__(self, logger, level=None, handler=None, close=True):
    self.logger = logger
    self.level = level
    self.handler = handler
    self.close = close

  def __enter__(self):
    if self.level is not None:
      self.old_level = self.logger.level
      self.logger.setLevel(self.level)
    if self.handler:
      self.logger.addHandler(self.handler)

  def __exit__(self, et, ev, tb):
    if self.level is not None:
      self.logger.setLevel(self.old_level)
    if self.handler:
      self.logger.removeHandler(self.handler)
    if self.handler and self.close:
      self.handler.close()
      # implicit return of None => don't swallow exceptions

Add context manager for logging