def test_logger_init(self): """ When creating a new DDLogger Has the same interface as logging.Logger Configures a defaultdict for buckets Properly configures the rate limit """ # Create a logger log = DDLogger('test.logger') # Ensure we set the name and use default log level self.assertEqual(log.name, 'test.logger') self.assertEqual(log.level, logging.NOTSET) # Assert DDLogger default properties self.assertIsInstance(log.buckets, dict) self.assertEqual(log.rate_limit, 60) # Assert manager and parent # DEV: Parent is `None` because `manager._findParents()` doesn't get called # unless we use `get_logger` (this is the same behavior as `logging.getLogger` and `Logger('name')`) self.assertEqual(log.manager, self.manager) self.assertIsNone(log.parent) # Override rate limit from environment variable with self.override_env(dict(DD_LOGGING_RATE_LIMIT='10')): log = DDLogger('test.logger') self.assertEqual(log.rate_limit, 10) # Set specific log level log = DDLogger('test.logger', level=logging.DEBUG) self.assertEqual(log.level, logging.DEBUG)
def test_logger_deprecated_rate_limit(self): with self.override_env(dict(DD_LOGGING_RATE_LIMIT="10")), warns(RemovedInDDTrace10Warning): log = DDLogger("test.logger") self.assertEqual(log.rate_limit, 10) # Ensure correct precedence with self.override_env(dict(DD_LOGGING_RATE_LIMIT="10", DD_TRACE_LOGGING_RATE="20")): log = DDLogger("test.logger") self.assertEqual(log.rate_limit, 20)
def test_logger_handle_bucket_skipped_msg(self, base_handle): """ When calling `DDLogger.handle` When a bucket exists for a previous time frame We pass only the record to the base `Logger.handle` We update the record message to include the number of skipped messages """ log = get_logger('test.logger') # Create log record to handle original_msg = 'hello %s' original_args = (1, ) record = self._make_record(log, msg=original_msg, args=(1, )) # Create a bucket entry for this record key = (record.name, record.levelno, record.pathname, record.lineno) bucket = int(record.created / log.rate_limit) # We want the time bucket to be for an older bucket log.buckets[key] = DDLogger.LoggingBucket(bucket=bucket - 1, skipped=20) # Handle our record log.handle(record) # We passed to base Logger.handle base_handle.assert_called_once_with(record) self.assertEqual(record.msg, original_msg + ', %s additional messages skipped') self.assertEqual(record.args, original_args + (20, )) self.assertEqual(record.getMessage(), 'hello 1, 20 additional messages skipped')