Reference

Duplication problem

problem

#!/usr/bin/env python

import time
import logging

class test:
    def __init__(self):
        self.init_logger()

    def init_logger(self):
        logger = logging.getLogger(__name__)
        logging.Formatter.converter = time.gmtime
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%Y-%m-%dT%H:%M:%SZ')
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        logger.addHandler(ch)
        self.logger = logger

    def print_info(self, comment):
        self.logger.info(comment)
                 
def main():
    for i in range(5):
        logger = test()
        logger.print_info('test %d' % i)

if __name__ == '__main__':
    main()

2013-01-19T03:41:16Z INFO test 0
2013-01-19T03:41:16Z INFO test 1
2013-01-19T03:41:16Z INFO test 1
2013-01-19T03:41:16Z INFO test 2
2013-01-19T03:41:16Z INFO test 2
2013-01-19T03:41:16Z INFO test 2
2013-01-19T03:41:16Z INFO test 3
2013-01-19T03:41:16Z INFO test 3
2013-01-19T03:41:16Z INFO test 3
2013-01-19T03:41:16Z INFO test 3
2013-01-19T03:41:16Z INFO test 4
2013-01-19T03:41:16Z INFO test 4
2013-01-19T03:41:16Z INFO test 4
2013-01-19T03:41:16Z INFO test 4
2013-01-19T03:41:16Z INFO test 4

solution?

#!/usr/bin/env python

import time
import logging

class test:
    def __init__(self, name):
        self.init_logger(name)

    def init_logger(self, name):
        logger = logging.getLogger(name)
        logging.Formatter.converter = time.gmtime
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%Y-%m-%dT%H:%M:%SZ')
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        logger.addHandler(ch)
        self.logger = logger

    def print_info(self, comment):
        self.logger.info(comment)
                
def main():
    for i in range(5):
        logger = test(str(i))
        logger.print_info('test %d' % i)

if __name__ == '__main__':
    main()

2013-01-19T03:44:57Z INFO test 0
2013-01-19T03:44:57Z INFO test 1
2013-01-19T03:44:57Z INFO test 2
2013-01-19T03:44:57Z INFO test 3
2013-01-19T03:44:57Z INFO test 4

solution??

#!/usr/bin/env python

import time
import logging

_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
logging.Formatter.converter = time.gmtime
_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%Y-%m-%dT%H:%M:%SZ')
__ch = logging.StreamHandler()
__ch.setLevel(logging.DEBUG)
__ch.setFormatter(_formatter)
_logger.addHandler(__ch)

class test:
    def __init__(self, name):
        fh = logging.FileHandler(name)
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(_formatter)
        _logger.addHandler(fh)
        self.__fh = fh
        
    def print_info(self, comment):
        _logger.info(comment)

    def finalize(self):
        _logger.removeHandler(self.__fh)

def main():
    for i in range(4):
        p = test(str(i)+'.log')
        p.print_info('test %d' % i)
        p.finalize()

if __name__ == '__main__':
    main()

2013-01-19T04:03:38Z INFO test 0
2013-01-19T04:03:38Z INFO test 1
2013-01-19T04:03:38Z INFO test 2
2013-01-19T04:03:38Z INFO test 3