def test_file_logging_rotation(logger, tmpdir): """Test Depot ID TBD. Test that log content ends up in a file when set a logdir. """ # Rotate log file every 20 bytes log_config = LoggingConfig(ignore_env=True, **{ 'log_directory': str(tmpdir), 'file_rotation_max_bytes': 20, 'file_rotation_backup_count': 2 }) setup_file_logging(log_config, 'log_file') # These two lines are greater than file_rotation_max_bytes, so first message should get rotated message1 = 'test_file_logging_part1' message2 = 'test_file_logging_part2' # Log error message, should go into log_file.log logger.error(message1) logger.error(message2) expected_filename1 = os.path.join(str(tmpdir), 'log_file.log') with open(expected_filename1, 'r') as f: assert message2 in f.read() assert message1 not in f.read() expected_filename2 = os.path.join(str(tmpdir), 'log_file.log.1') with open(expected_filename2, 'r') as f: assert message1 in f.read() assert message2 not in f.read()
def test_file_logging_prefix(logger, tmpdir): """Test Depot ID TBD. Test that file_prefix works in logging config """ log_config = LoggingConfig(ignore_env=True, **{'log_directory': str(tmpdir), 'file_prefix': 'foo'}) setup_file_logging(log_config, 'log_file') message = 'test_file_logging' logger.error(message) expected_filename = os.path.join(str(tmpdir), 'foo_log_file.log') with open(expected_filename, 'r') as f: assert message in f.read()
def test_file_logging(logger, tmpdir): """Test Depot ID TBD. Test that log content ends up in a file when set a logdir. """ log_config = LoggingConfig(ignore_env=True, **{'log_directory': str(tmpdir)}) setup_file_logging(log_config, 'log_file') message = 'test_file_logging' logger.error(message) expected_filename = os.path.join(str(tmpdir), 'log_file.log') with open(expected_filename, 'r') as f: assert message in f.read()
# Parse config try: cp = ConfigParser() app.config['DIRBS_CONFIG'] = cp.parse_config(ignore_env=False) except ConfigParseException: logger = logging.getLogger('dirbs.config') logger.critical( 'Exception encountered during parsing of config (.yml file). ' 'See console above for details...') sys.exit(1) # Update the logging based on the parsed config configure_logging(app.config['DIRBS_CONFIG'].log_config) # Initialize file logging setup_file_logging(app.config['DIRBS_CONFIG'].log_config, 'dirbs-api') # Init statsd client statsd = StatsClient(app.config['DIRBS_CONFIG'].statsd_config) # Init custom JSONEncoder (handles dates, etc.) app.json_encoder = utils.JSONEncoder def _metrics_type_from_req_ctxt(req: callable) -> str: """ Utility method to get a metrics path for the API type. Arguments: req: intercepted http request object Returns: