def main(): logdir = os.path.splitext(__file__)[0] + '_output' pathlib.Path(logdir).mkdir(exist_ok=True) logger = lo99ing.get_logger('LOGGER1') try: dict()[6] except Exception as e: logger.error( '1 this prints exception type and value=6 (no traceback): %s', e) try: dict()[6] except Exception: logger.exception('2 this prints traceback') try: dict()[6] except Exception as e: e.ATTR1 = 'VALUE1' e.ATTR2 = 'VALUE2' logger.exception('3 this prints traceback and exc attributes') logger.info( '4 next message fails formatting, and should cause a "Logged from" line' ) logger.info('does not print %s %s %s %s', 0, 1, 2) logger.info('5 done')
def getChild(self, suffix): from lo99ing import get_logger if self.root is not self: suffix = '.'.join((self.name, suffix)) return get_logger( suffix) # using get_logger() instead of self.manager.getLogger()
def test_lo99ing(): import lo99ing # late import from lo99ing.logger import Lo99er from lo99ing.level import log_level_manager ######################################## # TEST STRANGERS # not set, no explicit level -- should have gotten WARNING by _bootstrap L = lo99ing.get_logger('stranger11') assert not isinstance(L, Lo99er), L assert L.level == logging.WARNING, L.level assert log_level_manager.get_effective('stranger11') == logging.WARNING # set, no explicit level -- set existing level as initial L = lo99ing.get_logger('stranger21') assert not isinstance(L, Lo99er), L assert L.level == logging.ERROR, L.level assert log_level_manager.get_effective('stranger21') == logging.ERROR # set, explicit level -- ignore explicit level, set existing level as initial L = lo99ing.get_logger('stranger22', level='info') assert not isinstance(L, Lo99er), L assert L.level == logging.ERROR, L.level assert log_level_manager.get_effective('stranger22') == logging.ERROR ######################################## # TEST LO99ERS # no explicit level, set default level as initial L = lo99ing.get_logger('mine11') assert isinstance(L, Lo99er), L assert L.level == logging.INFO, L.level assert log_level_manager.get_effective('mine11') == logging.INFO assert log_level_manager.get_initial('mine11') == logging.INFO # with explicit level, set it as initial L = lo99ing.get_logger('mine12', level='error') assert isinstance(L, Lo99er), L assert L.level == logging.ERROR, L.level assert log_level_manager.get_effective('mine12') == logging.ERROR assert log_level_manager.get_initial('mine12') == logging.ERROR # no explicit level, but with pre-existing override lo99ing.set_log_level_override('mine21', 'debug') L = lo99ing.get_logger('mine21') assert isinstance(L, Lo99er), L assert L.level == logging.DEBUG, L.level assert log_level_manager.get_effective('mine21') == logging.DEBUG assert log_level_manager.get_initial('mine21') == logging.INFO # with explicit level, but with pre-existing override lo99ing.set_log_level_override('mine22', 'debug') L = lo99ing.get_logger('mine22', level='error') assert isinstance(L, Lo99er), L assert L.level == logging.DEBUG, L.level assert log_level_manager.get_effective('mine22') == logging.DEBUG assert log_level_manager.get_initial('mine22') == logging.ERROR
def main(): logdir = os.path.splitext(__file__)[0] + '_output' pathlib.Path(logdir).mkdir(exist_ok=True) logger = lo99ing.get_logger('xxx') flogger = lo99ing.get_file_logger('flogger', logdir + '/f.log') logger.info('1 this prints to stderr only') flogger.info('1 this prints to file only') logger.info('2 done') flogger.info('2 done')
def main(): logger = lo99ing.get_logger('LOGGER1') logger.info('<-- 1 local time') lo99ing.use_utc() logger.info('<-- 2 utc time') manual_clock = ManualClock() lo99ing.use_clock(manual_clock.now) logger.info('<-- 3 still utc time') manual_clock.t = datetime.datetime(1990, 1, 1, 2, 2, 2, 888000) logger.info('<-- 4 made up time in 1990') manual_clock.t = datetime.datetime(2222, 3, 3, 4, 4, 4, 999000) logger.info('<-- 5 made up time in 2222') logger.info('done')
def main(): logdir = os.path.splitext(__file__)[0] + '_output' pathlib.Path(logdir).mkdir(exist_ok=True) logger = lo99ing.get_logger('LOGGER1') logger.info('1 this prints to stderr') logger.error('2 this prints to stderr') logger.debug('NOPRINT') logger.set_log_level_override('error') logger.info('NOPRINT') logger.error('3 this prints to stderr') lo99ing.enable_file(os.path.join(logdir, 'a.log')) logger.info('NOPRINT') logger.set_log_level_override(None) logger.info('4 this prints to stderr and file (first line in file)') logger.error( '5 this prints to stderr and file, next message in stderr is 7') lo99ing.disable_stderr() logger.info('6 this prints to file') lo99ing.enable_stderr() logger.info('7 this prints to stderr and file') # test trace logger.info('8 next line is a trace, with filename and linenum') logger.TRACE() logger.info( '9 next line is a trace, with filename and linenum, and extra args') logger.TRACE(os, foo='bar') # test prefixed logger2 = logger.prefixed('AAA') logger2.info('10 this line includes AAA prefix') logger3 = logger2.prefixed('BBB') logger3.info('11 this line includes AAA BBB prefix') logger.info('12 done')
def main(): logdir = os.path.splitext(__file__)[0] + '_output' pathlib.Path(logdir).mkdir(exist_ok=True) logger = lo99ing.get_logger('LOGGER1') lo99ing.enable_file(pathlib.Path(logdir) / 'a_*_b.log', rotate=True) if UTC: lo99ing.use_utc() pprint(datetime.datetime.utcnow()) pprint(logging.root.handlers[-1].stream) while True: logger.info('x') pprint(datetime.datetime.utcnow()) pprint(logging.root.handlers[-1].stream) time.sleep(1 * 60)
def __init__(self, *, logger=None, logger_name=None, clock=None, profile=False, profile_kwargs=None, **kwargs): """ :param profile: If True, the thread will run with profiling enabled, using ProfileContext_. :param profile_kwargs: extra kwargs to pass to the ``ProfileContext``. """ super().__init__(**kwargs) if logger is None: if logger_name is None: logger_name = '%s' % self.name logger = lo99ing.get_logger(logger_name) self.logger = logger if clock is None: clock = datetime.datetime.now self._clock = clock self._stopping_event = threading.Event() self._stop_reason = None if profile_kwargs is None: profile_kwargs = {} self._profiler_ctx = self._get_profiler_ctxmgr(profile, **profile_kwargs) # The following are private. subclasses should not set them: self.__result = None self.__exception = None self.__runtime = self.Runtime(clock=self._clock) self.__future = self.Future(self)
def _post_process_log_levels(cli_levels, **kwargs): logger = lo99ing.get_logger('lo99ing') for logger_name, level in cli_levels.items(): logger.info('LOG LEVEL OVERRIDE: %s = %s', logger_name, level) lo99ing.set_log_level_override(logger_name, level) return cli_levels