def test_simple_log_processor_custom_level(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() log_emitter = log_emitter_provider.get_log_emitter(__name__) log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("custom_level") logger.setLevel(logging.ERROR) logger.addHandler(OTLPHandler(log_emitter=log_emitter)) logger.warning("Warning message") logger.debug("Debug message") logger.error("Error message") logger.critical("Critical message") finished_logs = exporter.get_finished_logs() # Make sure only level >= logging.CRITICAL logs are recorded self.assertEqual(len(finished_logs), 2) critical_log_record = finished_logs[0].log_record fatal_log_record = finished_logs[1].log_record self.assertEqual(critical_log_record.body, "Error message") self.assertEqual(critical_log_record.severity_text, "ERROR") self.assertEqual( critical_log_record.severity_number, SeverityNumber.ERROR ) self.assertEqual(fatal_log_record.body, "Critical message") self.assertEqual(fatal_log_record.severity_text, "CRITICAL") self.assertEqual( fatal_log_record.severity_number, SeverityNumber.FATAL )
def test_simple_log_processor_default_level(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() log_emitter = log_emitter_provider.get_log_emitter(__name__) log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("default_level") logger.addHandler(OTLPHandler(log_emitter=log_emitter)) logger.warning("Something is wrong") finished_logs = exporter.get_finished_logs() self.assertEqual(len(finished_logs), 1) warning_log_record = finished_logs[0].log_record self.assertEqual(warning_log_record.body, "Something is wrong") self.assertEqual(warning_log_record.severity_text, "WARNING") self.assertEqual( warning_log_record.severity_number, SeverityNumber.WARN )
def test_simple_log_processor_trace_correlation(self): exporter = InMemoryLogExporter() log_emitter_provider = LogEmitterProvider() log_emitter = log_emitter_provider.get_log_emitter("name", "version") log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) logger = logging.getLogger("trace_correlation") logger.addHandler(OTLPHandler(log_emitter=log_emitter)) logger.warning("Warning message") finished_logs = exporter.get_finished_logs() self.assertEqual(len(finished_logs), 1) log_record = finished_logs[0].log_record self.assertEqual(log_record.body, "Warning message") self.assertEqual(log_record.severity_text, "WARNING") self.assertEqual(log_record.severity_number, SeverityNumber.WARN) self.assertEqual(log_record.trace_id, INVALID_SPAN_CONTEXT.trace_id) self.assertEqual(log_record.span_id, INVALID_SPAN_CONTEXT.span_id) self.assertEqual( log_record.trace_flags, INVALID_SPAN_CONTEXT.trace_flags ) exporter.clear() tracer = trace.TracerProvider().get_tracer(__name__) with tracer.start_as_current_span("test") as span: logger.critical("Critical message within span") finished_logs = exporter.get_finished_logs() log_record = finished_logs[0].log_record self.assertEqual(log_record.body, "Critical message within span") self.assertEqual(log_record.severity_text, "CRITICAL") self.assertEqual(log_record.severity_number, SeverityNumber.FATAL) span_context = span.get_span_context() self.assertEqual(log_record.trace_id, span_context.trace_id) self.assertEqual(log_record.span_id, span_context.span_id) self.assertEqual(log_record.trace_flags, span_context.trace_flags)