opentelemetry/logs/logger.rs
1use std::borrow::Cow;
2
3use crate::{logs::LogRecord, InstrumentationScope};
4
5use super::Severity;
6
7/// The interface for emitting [`LogRecord`]s.
8pub trait Logger {
9 /// Specifies the `LogRecord` type associated with this logger.
10 type LogRecord: LogRecord;
11
12 /// Creates a new log record builder.
13 fn create_log_record(&self) -> Self::LogRecord;
14
15 /// Emit a [`LogRecord`]. If there is active current thread's [`Context`],
16 /// the logger will set the record's `TraceContext` to the active trace context,
17 ///
18 /// [`Context`]: crate::Context
19 fn emit(&self, record: Self::LogRecord);
20
21 /// Check if the given log is enabled.
22 fn event_enabled(&self, level: Severity, target: &str, name: Option<&str>) -> bool;
23}
24
25/// Interfaces that can create [`Logger`] instances.
26pub trait LoggerProvider {
27 /// The [`Logger`] type that this provider will return.
28 type Logger: Logger;
29
30 /// Returns a new logger with the given instrumentation scope.
31 ///
32 /// # Examples
33 ///
34 /// ```
35 /// use opentelemetry::InstrumentationScope;
36 /// use opentelemetry::logs::LoggerProvider;
37 /// use opentelemetry_sdk::logs::SdkLoggerProvider;
38 ///
39 /// let provider = SdkLoggerProvider::builder().build();
40 ///
41 /// // logger used in applications/binaries
42 /// let logger = provider.logger("my_app");
43 ///
44 /// // logger used in libraries/crates that optionally includes version and schema url
45 /// let scope = InstrumentationScope::builder(env!("CARGO_PKG_NAME"))
46 /// .with_version(env!("CARGO_PKG_VERSION"))
47 /// .with_schema_url("https://opentelemetry.io/schemas/1.0.0")
48 /// .build();
49 ///
50 /// let logger = provider.logger_with_scope(scope);
51 /// ```
52 fn logger_with_scope(&self, scope: InstrumentationScope) -> Self::Logger;
53
54 /// Returns a new logger with the given name.
55 ///
56 /// The `name` should be the application name or the name of the library
57 /// providing instrumentation.
58 fn logger(&self, name: impl Into<Cow<'static, str>>) -> Self::Logger {
59 let scope = InstrumentationScope::builder(name).build();
60 self.logger_with_scope(scope)
61 }
62}