def config_logging(use_locking): """ """ logfilename = ('mproc2_LOCKING.log' if use_locking else 'mproc2_NOLOCKING.log') lcd = LCDict(log_path=LOG_PATH, locking=use_locking, attach_handlers_to_root=True, root_level='DEBUG') # add main file handler, which will write to LOG_PATH + '/' + logfilename lcd.add_stderr_handler( 'console', formatter='msg', level='DEBUG' ).add_formatter( 'my_file_formatter', format= '%(processName)-12s: %(name)-14s: %(levelname)-8s: %(asctime)24s: %(message)s' ).add_file_handler( 'app_file', filename=logfilename, mode='w', # level='DEBUG', formatter='my_file_formatter', ) lcd.config()
def test_init_trailing_space(self): from prelogging import LCDict s = '''\ ' my formatter ' format: '%(name)s - %(levelname)s - %(message)s' style: % ''' update_formatter_presets(s) # Swap stderr _stderr = sys.stderr sio_out = io.StringIO() sys.stderr = sio_out lcd = LCDict(attach_handlers_to_root=True) lcd.add_stderr_handler('con', formatter=' my formatter ') lcd.config() import logging root = logging.getLogger() root.warning(u'Hello') # PY2: 'u' prefix self.assertEqual(sio_out.getvalue(), "root - WARNING - Hello\n") # unswap stderr, unnecessarily sys.stderr = _stderr
def logging_config(): lcd = LCDict(attach_handlers_to_root=True) # root level: WARNING lcd.add_stderr_handler('con-err', formatter='level_msg') # console handler level: NOTSET # Add TWO SMTPHandlers, one for each level ERROR and CRITICAL, # which will email technical staff with logged messages of levels >= ERROR. # We use a filter to make the first handler squelch CRITICAL messages: lcd.add_callable_filter("filter-error-only", filter_error_only) # TEST_TO_ADDRESS included just for testing/trying out the example basic_toaddrs = [TEST_TO_ADDRESS, '*****@*****.**'] # add error-only SMTP handler add_smtp_handler_to_lcd(lcd, 'email-error', level='ERROR', toaddrs=basic_toaddrs, subject='ERROR (Alert from SMTPHandler)', filters=['filter-error-only']) # add critical-only SMTP handler add_smtp_handler_to_lcd(lcd, 'email-critical', level='CRITICAL', toaddrs=basic_toaddrs + ['*****@*****.**'], subject='CRITICAL (Alert from SMTPHandler)') lcd.config()
def test_no_root_handlers_no_lock(self): """ DON'T add handlers to root, locking=False """ lcd = LCDict(root_level='DEBUG') self.assertEqual(lcd.locking, False) self.assertEqual(lcd.attach_handlers_to_root, False) # lcd.dump() # | DEBUG comment out expected = self.get_expected_starting_dict('DEBUG') self.assertEqual(lcd, expected) lcd.add_stderr_handler('console', level='WARNING', formatter='msg') # lcd.dump() # | DEBUG comment out self.assertEqual(lcd['formatters'], { 'msg': { 'format': '%(message)s' }, }) lcd.add_file_handler('default_file', filename='blather.log', formatter='msg') # lcd.dump() # | DEBUG comment out self.assertEqual( lcd['handlers'], { 'console': { 'class': 'logging.StreamHandler', 'formatter': 'msg', 'level': 'WARNING', 'stream': 'ext://sys.stderr' }, 'default_file': { 'class': 'logging.FileHandler', 'delay': False, 'filename': 'blather.log', 'formatter': 'msg', # 'level': 'NOTSET', 'mode': 'a' } }) lcd.add_logger('default', handlers=('console', 'default_file'), level='DEBUG') # lcd.dump() # | DEBUG comment out self.assertEqual(lcd['loggers'], { 'default': { 'handlers': ['console', 'default_file'], 'level': 'DEBUG' } })
def config_logging(use_locking): logfilename = 'logfile (%s).log' % ('LOCKING' if use_locking else 'NOLOCKING') lcd = LCDict(log_path='_log/mproc_deco/', root_level='DEBUG', attach_handlers_to_root=True, locking=use_locking) # Set up console handler to show process name, time, handler name lcd.add_stderr_handler('console', formatter='process_time_logger_level_msg', level='INFO') # Add main file handler, which will write to log_path + '/' + logfilename lcd.add_file_handler( 'app_file', filename=logfilename, mode='w', formatter='process_time_logger_level_msg', ) lcd.config()
def config_logging(use_locking): # NOTE: log_path dir should already exist log_path = os.path.join('_log/mproc_deco_rot_fh', 'LOCKING' if use_locking else 'NOLOCKING') lcd = LCDict(log_path=log_path, root_level='DEBUG', attach_handlers_to_root=True, locking=use_locking) # Set up console handler to show process name, time, handler name lcd.add_stderr_handler('console', formatter='process_level_msg', level='INFO') # Add main file handler, which will write to log_path + '/' + logfilename lcd.add_rotating_file_handler( 'rot_fh', filename=LOGFILENAME, # formatter='process_time_level_msg', max_bytes=1024, backup_count=10, # mode='w', ) lcd.config()
def config_logging(use_locking): if not sys.platform.startswith('darwin'): raise NotImplementedError( "This example is currently implemented only for OS X / macOS") print("%s locking" % ("Using" if use_locking else "NOT using")) lcd = LCDict(root_level='DEBUG', attach_handlers_to_root=True, locking=use_locking) # Set up console handler to show process name, time, handler name lcd.add_stderr_handler( 'console', level='WARNING', formatter='process_level_msg' ) # Add syslog handler with same formatter lcd.add_syslog_handler( 'h_syslog', formatter='process_level_msg', address='/var/run/syslog', # works for OS X; '/dev/log' for *nix ) lcd.check() lcd.config()
def test_style_no_quotes(self): from prelogging import LCDict s = '''\ myformatter format: '__%(message)s__' style: % ''' update_formatter_presets(s) # Swap stderr _stderr = sys.stderr sio_out = io.StringIO() sys.stderr = sio_out lcd = LCDict(attach_handlers_to_root=True) lcd.add_stderr_handler('con', formatter='myformatter') lcd.config() root = logging.getLogger() root.warning(u'Yo, muh man') # PY2: 'u' prefix self.assertEqual(sio_out.getvalue(), "__Yo, muh man__\n") # unswap stderr, unnecessarily sys.stderr = _stderr
def main(): # root, console handler levels: WARNING. lcd = LCDict(attach_handlers_to_root=True) lcd.add_stderr_handler('con-err', formatter='msg').add_email_handler( 'email-handler', level='ERROR', formatter='time_logger_level_msg', # SMTPHandler-specific kwargs: mailhost=SMTP_SERVER, fromaddr=FROM_ADDRESS, toaddrs=[TEST_TO_ADDRESS, '*****@*****.**'], # string or list of strings subject='Alert from SMTPHandler', username=SMTP_USERNAME, password=SMTP_PASSWORD) lcd.config() root = logging.getLogger() root.debug("1.") # not logged (loglevel too low) root.info("2.") # ditto root.warning("3.") # logged to console root.error("4.") # logged to console, emailed root.critical("5.") # ditto
def test_root_handlers_lock(self): """ DO add handlers to root, locking=True """ lcd = LCDict(attach_handlers_to_root=True, locking=True) self.assertEqual(lcd.locking, True) self.assertEqual(lcd.attach_handlers_to_root, True) # lcd.dump() # | DEBUG comment out expected = self.get_expected_starting_dict() self.assertEqual(lcd, expected) # No formatters, use default lcd.add_stderr_handler('console', level='WARNING').add_file_handler( 'default_file', filename='blather.log', formatter='process_time_logger_level_msg') # lcd.dump() # | DEBUG comment out self.assertEqual( lcd['handlers'], { 'console': { '()': 'ext://prelogging.LockingStreamHandler', 'create_lock': True, 'level': 'WARNING', 'stream': 'ext://sys.stderr' }, 'default_file': { '()': 'ext://prelogging.LockingFileHandler', 'create_lock': True, 'delay': False, 'filename': 'blather.log', 'formatter': 'process_time_logger_level_msg', # 'level': 'NOTSET', 'mode': 'a' } }) lcd.clone_handler(clone='con2', handler='console') # lcd.dump() # | DEBUG comment out self.assertEqual( lcd['handlers'], { 'con2': { '()': 'ext://prelogging.LockingStreamHandler', 'create_lock': True, 'level': 'WARNING', 'stream': 'ext://sys.stderr' }, 'console': { '()': 'ext://prelogging.LockingStreamHandler', 'create_lock': True, 'level': 'WARNING', 'stream': 'ext://sys.stderr' }, 'default_file': { '()': 'ext://prelogging.LockingFileHandler', 'create_lock': True, 'delay': False, 'filename': 'blather.log', 'formatter': 'process_time_logger_level_msg', # 'level': 'NOTSET', 'mode': 'a' } }) # For more coverage (locking_handlers.py from 46% to 60%) lcd.config(disable_existing_loggers=True)