def setUp(self): rm_file(this_base + '/t.out') # root logger k3log.make_logger(base_dir=this_base, log_fn='t.out', level=logging.DEBUG, fmt='message')
def test_set_logger_level(self): cases = ( (None, 'debug1\ndebug2'), ('1_prefix', 'debug1\ndebug2\ndebug2'), (('1_prefix', '2_prefix'), 'debug1\ndebug2'), (('not_exist', ), 'debug1\ndebug2\ndebug1\ndebug2'), (('not_exist', '1_prefix'), 'debug1\ndebug2\ndebug2'), ) for inp, expected in cases: rm_file(this_base + '/ss') logger1 = k3log.make_logger(base_dir=this_base, log_name='1_prefix_1', log_fn='ss', level='DEBUG', fmt='%(message)s', datefmt='%H%M%S') logger2 = k3log.make_logger(base_dir=this_base, log_name='2_prefix_1', log_fn='ss', level='DEBUG', fmt='%(message)s', datefmt='%H%M%S') logger1.debug('debug1') logger2.debug('debug2') k3log.set_logger_level(level='INFO', name_prefixes=inp) logger1.debug('debug1') logger2.debug('debug2') content = read_file(this_base + '/ss') self.assertEqual(expected, content.strip())
def test_make_logger(self): rm_file(this_base + '/tt') l = k3log.make_logger(base_dir=this_base, log_name='m', log_fn='tt', level='INFO', fmt='%(message)s', datefmt='%H%M%S') l.debug('debug') l.info('info') cont = read_file(this_base + '/tt').strip() self.assertEqual(cont, 'info')
def test_make_file_handler(self): rm_file(this_base + '/handler_change') l = k3log.make_logger(base_dir=this_base, log_name='h', log_fn='dd', level='INFO', fmt='%(message)s', datefmt='%H%M%S') l.handlers = [] handler = k3log.make_file_handler(base_dir=this_base, log_fn='handler_change', fmt='%(message)s', datefmt='%H%M%S') l.addHandler(handler) l.debug('debug') l.info('info') cont = read_file(this_base + '/handler_change').strip() self.assertEqual(cont, 'info')
def test_concurrent_write_and_remove(self): l = k3log.make_logger(base_dir=this_base, log_name='rolling', log_fn='rolling.out', level=logging.DEBUG, fmt='message') n = 10240 sess = {'running': True} def _remove(): while sess['running']: rm_file(this_base + '/rolling.out') th = threading.Thread(target=_remove) th.daemon = True th.start() for ii in range(n): l.debug('123') sess['running'] = False th.join()
import k3log # make a file logger in one line logger = k3log.make_logger('/tmp', level='INFO', fmt='%(message)s', datefmt="%H:%M:%S") logger.info('foo') logger.stack_str(fmt="{fn}:{ln} in {func}\n {statement}", sep="\n") # runpy.py:174 in _run_module_as_main # "__main__", fname, loader, pkg_name) # runpy.py:72 in _run_code # exec code in run_globals # ... # test_logutil.py:82 in test_deprecate # k3log.deprecate() # 'foo', fmt='{fn}:{ln} in {func}\n {statement}', sep='\n')
def read_file(fn): with open(fn, 'r') as f: return f.read() def rm_file(fn): try: os.unlink(fn) except OSError as e: if e.errno == errno.ENOENT: pass else: raise rm_file('/tmp/tt') l = k3log.make_logger(base_dir=None, log_name='m', log_fn='tt', level='INFO', fmt='%(message)s', datefmt='%H%M%S' ) l.debug('debug') l.info('info') cont = read_file('/tmp/tt').strip() print(cont)
def rm_file(fn): try: os.unlink(fn) except OSError as e: if e.errno == errno.ENOENT: pass else: raise rm_file('/tmp/handler_change') l = k3log.make_logger(base_dir='/tmp', log_name='h', log_fn='dd', level='INFO', fmt='%(message)s', datefmt='%H%M%S') l.handlers = [] handler = k3log.make_file_handler(log_fn='handler_change', fmt='%(message)s', datefmt='%H%M%S') l.addHandler(handler) l.debug('debug') l.info('info') cont = read_file('/tmp/handler_change').strip() print(cont)
import logging import k3log logger = k3log.make_logger('/tmp', log_fn='stdlog', level='INFO', fmt='message') k3log.add_std_handler(logger, 'stdout', fmt='message', level=logging.ERROR) logger.debug('debug') logger.info('stdlog') logger.error('error')