1
1
use sentry_core:: protocol:: Event ;
2
+ #[ cfg( feature = "logs" ) ]
3
+ use sentry_core:: protocol:: { Log , LogAttribute , LogLevel } ;
2
4
use sentry_core:: { Breadcrumb , Level } ;
5
+ #[ cfg( feature = "logs" ) ]
6
+ use std:: { collections:: BTreeMap , time:: SystemTime } ;
3
7
4
- /// Converts a [`log::Level`] to a Sentry [`Level`]
8
+ /// Converts a [`log::Level`] to a Sentry [`Level`], used for [`Event`] and [`Breadcrumb`].
5
9
pub fn convert_log_level ( level : log:: Level ) -> Level {
6
10
match level {
7
11
log:: Level :: Error => Level :: Error ,
@@ -11,6 +15,18 @@ pub fn convert_log_level(level: log::Level) -> Level {
11
15
}
12
16
}
13
17
18
+ /// Converts a [`log::Level`] to a Sentry [`LogLevel`], used for [`Log`].
19
+ #[ cfg( feature = "logs" ) ]
20
+ pub fn convert_log_level_to_sentry_log_level ( level : log:: Level ) -> LogLevel {
21
+ match level {
22
+ log:: Level :: Error => LogLevel :: Error ,
23
+ log:: Level :: Warn => LogLevel :: Warn ,
24
+ log:: Level :: Info => LogLevel :: Info ,
25
+ log:: Level :: Debug => LogLevel :: Debug ,
26
+ log:: Level :: Trace => LogLevel :: Trace ,
27
+ }
28
+ }
29
+
14
30
/// Creates a [`Breadcrumb`] from a given [`log::Record`].
15
31
pub fn breadcrumb_from_record ( record : & log:: Record < ' _ > ) -> Breadcrumb {
16
32
Breadcrumb {
@@ -40,3 +56,33 @@ pub fn exception_from_record(record: &log::Record<'_>) -> Event<'static> {
40
56
// an exception record.
41
57
event_from_record ( record)
42
58
}
59
+
60
+ /// Creates a [`Log`] from a given [`log::Record`].
61
+ #[ cfg( feature = "logs" ) ]
62
+ pub fn log_from_record ( record : & log:: Record < ' _ > ) -> Log {
63
+ let mut attributes: BTreeMap < String , LogAttribute > = BTreeMap :: new ( ) ;
64
+
65
+ attributes. insert ( "logger.target" . into ( ) , record. target ( ) . into ( ) ) ;
66
+ if let Some ( module_path) = record. module_path ( ) {
67
+ attributes. insert ( "logger.module_path" . into ( ) , module_path. into ( ) ) ;
68
+ }
69
+ if let Some ( file) = record. file ( ) {
70
+ attributes. insert ( "logger.file" . into ( ) , file. into ( ) ) ;
71
+ }
72
+ if let Some ( line) = record. line ( ) {
73
+ attributes. insert ( "logger.line" . into ( ) , line. into ( ) ) ;
74
+ }
75
+
76
+ attributes. insert ( "sentry.origin" . into ( ) , "auto.logger.log" . into ( ) ) ;
77
+
78
+ // TODO: support the `kv` feature and store key value pairs as attributes
79
+
80
+ Log {
81
+ level : convert_log_level_to_sentry_log_level ( record. level ( ) ) ,
82
+ body : format ! ( "{}" , record. args( ) ) ,
83
+ trace_id : None ,
84
+ timestamp : SystemTime :: now ( ) ,
85
+ severity_number : None ,
86
+ attributes,
87
+ }
88
+ }
0 commit comments