opentelemetry/logs/
mod.rs

1//! # OpenTelemetry Logs Bridge API
2///  This API is not intended to be called by application developers directly.
3///  It is provided for logging library authors to build log appenders, that
4///  bridges existing logging systems with OpenTelemetry.
5use crate::ExportError;
6
7use std::{sync::PoisonError, time::Duration};
8use thiserror::Error;
9
10mod logger;
11mod noop;
12mod record;
13
14pub use logger::{Logger, LoggerProvider};
15pub use noop::NoopLoggerProvider;
16pub use record::{AnyValue, LogRecord, Severity};
17
18/// Describe the result of operations in log SDK.
19pub type LogResult<T> = Result<T, LogError>;
20
21#[derive(Error, Debug)]
22#[non_exhaustive]
23/// Errors returned by the log SDK.
24pub enum LogError {
25    /// Export failed with the error returned by the exporter.
26    #[error("Exporter {} encountered the following errors: {0}", .0.exporter_name())]
27    ExportFailed(Box<dyn ExportError>),
28
29    /// Export failed to finish after certain period and processor stopped the export.
30    #[error("Exporter timed out after {} seconds", .0.as_secs())]
31    ExportTimedOut(Duration),
32
33    /// Other errors propagated from log SDK that weren't covered above.
34    #[error(transparent)]
35    Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
36}
37
38impl<T> From<T> for LogError
39where
40    T: ExportError,
41{
42    fn from(err: T) -> Self {
43        LogError::ExportFailed(Box::new(err))
44    }
45}
46
47impl From<String> for LogError {
48    fn from(err_msg: String) -> Self {
49        LogError::Other(Box::new(Custom(err_msg)))
50    }
51}
52
53impl From<&'static str> for LogError {
54    fn from(err_msg: &'static str) -> Self {
55        LogError::Other(Box::new(Custom(err_msg.into())))
56    }
57}
58
59impl<T> From<PoisonError<T>> for LogError {
60    fn from(err: PoisonError<T>) -> Self {
61        LogError::Other(err.to_string().into())
62    }
63}
64/// Wrap type for string
65#[derive(Error, Debug)]
66#[error("{0}")]
67struct Custom(String);