def write(self, message):
    if message != '\n':
        self.logger.log(self.level, message)

def main():
    logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('demo')
info_fp = LoggerWriter(logger, logging.INFO)
debug_fp = LoggerWriter(logger, logging.DEBUG)
print('An INFO message', file=info_fp)
print('A DEBUG message', file=debug_fp)
if __name__ == '__main__':
    main()
Redirect stdout to logger in Python