This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vinay.sajip
Recipients BreamoreBoy, Saimadhav.Heblikar, pitrou, rhettinger, vinay.sajip
Date 2014-08-15.14:07:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1408111635.21.0.0262453871996.issue22195@psf.upfronthosting.co.za>
In-reply-to
Content
Here's a tentative suggestion. Does it meet your needs?

import logging
import sys

class LoggerWriter(object):
    def __init__(self, logger='', level=logging.DEBUG):
        if isinstance(logger, str):
            logger = logging.getLogger(logger)
        self.logger = logger
        self.level = level
        self.buffer = ''

    def _output(self, force):
        lines = self.buffer.split('\n')
        if force:
            self.buffer = ''
        else:
            self.buffer = lines.pop()
        for line in lines:
            self.logger.log(self.level, line)

    def flush(self):
        self._output(True)

    def write(self, text):
        self.buffer += text
        self._output(False)


def main():
    stream = LoggerWriter()
    with open('lwtest.txt', 'w') as f:
        print('foo', 1, 'bar\n\n', 2.0, 'baz', file=stream)
        print(file=stream)
        print('foo', 1, 'bar\n\n', 2.0, 'baz', file=f)
        print(file=f)
        print('frob', end=' ', file=stream)
        print('frob', end=' ', file=f)
        f.flush()
    stream.flush()

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s',
                        filename='lwtest.log')
    sys.exit(main())
History
Date User Action Args
2014-08-15 14:07:15vinay.sajipsetrecipients: + vinay.sajip, rhettinger, pitrou, BreamoreBoy, Saimadhav.Heblikar
2014-08-15 14:07:15vinay.sajipsetmessageid: <1408111635.21.0.0262453871996.issue22195@psf.upfronthosting.co.za>
2014-08-15 14:07:15vinay.sajiplinkissue22195 messages
2014-08-15 14:07:15vinay.sajipcreate