@@ -35,61 +35,62 @@ fn get_filename(filepath: &str) -> &str {
35
35
filepath
36
36
}
37
37
38
- impl < ' a , LR : LogRecord > EventVisitor < ' a , LR > {
38
+ impl < ' a , LR : LogRecord > EventVisitor < ' a , LR > {
39
39
fn new ( log_record : & ' a mut LR ) -> Self {
40
- EventVisitor { log_record}
40
+ EventVisitor { log_record }
41
41
}
42
42
fn visit_metadata ( & mut self , meta : & Metadata ) {
43
- self . log_record . add_attribute ( Key :: new ( "name" ) , AnyValue :: from ( meta. name ( ) ) ) ;
43
+ self . log_record
44
+ . add_attribute ( Key :: new ( "name" ) , AnyValue :: from ( meta. name ( ) ) ) ;
44
45
45
46
#[ cfg( feature = "experimental_metadata_attributes" ) ]
46
47
self . visit_experimental_metadata ( meta) ;
47
48
}
48
49
49
50
#[ cfg( feature = "experimental_metadata_attributes" ) ]
50
51
fn visit_experimental_metadata ( & mut self , meta : & Metadata ) {
51
- self . log_record_attributes
52
- . push ( ( "log.target" . into ( ) , meta. target ( ) . to_owned ( ) . into ( ) ) ) ;
52
+ self . log_record . add_attribute (
53
+ Key :: new ( "log.target" ) ,
54
+ AnyValue :: from ( meta. target ( ) . to_owned ( ) ) ,
55
+ ) ;
53
56
54
57
if let Some ( module_path) = meta. module_path ( ) {
55
- self . log_record_attributes
56
- . push ( ( "code.namespace" . into ( ) , module_path. to_owned ( ) . into ( ) ) ) ;
58
+ self . log_record . add_attribute (
59
+ Key :: new ( "code.namespace" ) ,
60
+ AnyValue :: from ( module_path. to_owned ( ) ) ,
61
+ ) ;
62
+ s
57
63
}
58
64
59
65
if let Some ( filepath) = meta. file ( ) {
60
- self . log_record_attributes
61
- . push ( ( "code.filepath" . into ( ) , filepath. to_owned ( ) . into ( ) ) ) ;
62
- self . log_record_attributes . push ( (
63
- "code.filename" . into ( ) ,
64
- get_filename ( filepath) . to_owned ( ) . into ( ) ,
65
- ) ) ;
66
+ self . log_record . add_attribute (
67
+ Key :: new ( "code.filepath" ) ,
68
+ AnyValue :: from ( filepath. to_owned ( ) ) ,
69
+ ) ;
70
+ self . log_record . add_attribute (
71
+ Key :: new ( "code.filename" ) ,
72
+ AnyValue :: from ( get_filename ( filepath) . to_owned ( ) ) ,
73
+ ) ;
66
74
}
67
75
68
76
if let Some ( line) = meta. line ( ) {
69
- self . log_record_attributes
70
- . push ( ( "code.lineno" . into ( ) , line. into ( ) ) ) ;
71
- }
72
- }
73
-
74
- fn push_to_otel_log_record < LR : LogRecord > ( self , log_record : & mut LR ) {
75
- if let Some ( body) = self . log_record_body {
76
- log_record. set_body ( body) ;
77
+ self . log_record
78
+ . add_attribute ( Key :: new ( "code.lineno" ) , AnyValue :: from ( line) ) ;
77
79
}
78
- log_record. add_attributes ( self . log_record_attributes ) ;
79
80
}
80
81
}
81
82
82
- impl tracing:: field:: Visit for EventVisitor {
83
+ impl < ' a , LR : LogRecord > tracing:: field:: Visit for EventVisitor < ' a , LR > {
83
84
fn record_debug ( & mut self , field : & tracing:: field:: Field , value : & dyn std:: fmt:: Debug ) {
84
85
#[ cfg( feature = "experimental_metadata_attributes" ) ]
85
86
if is_duplicated_metadata ( field. name ( ) ) {
86
87
return ;
87
88
}
88
89
if field. name ( ) == "message" {
89
- self . log_record_body = Some ( format ! ( "{value :?}" ) . into ( ) ) ;
90
+ self . log_record . set_body ( format ! ( "{:?}" , value ) . into ( ) ) ;
90
91
} else {
91
- self . log_record_attributes
92
- . push ( ( field. name ( ) . into ( ) , format ! ( "{value:?}" ) . into ( ) ) ) ;
92
+ self . log_record
93
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( format ! ( "{value:?}" ) ) ) ;
93
94
}
94
95
}
95
96
@@ -98,27 +99,27 @@ impl tracing::field::Visit for EventVisitor {
98
99
if is_duplicated_metadata ( field. name ( ) ) {
99
100
return ;
100
101
}
101
- self . log_record_attributes
102
- . push ( ( field. name ( ) . into ( ) , value. to_owned ( ) . into ( ) ) ) ;
102
+ self . log_record
103
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value. to_owned ( ) ) ) ;
103
104
}
104
105
105
106
fn record_bool ( & mut self , field : & tracing_core:: Field , value : bool ) {
106
- self . log_record_attributes
107
- . push ( ( field. name ( ) . into ( ) , value . into ( ) ) ) ;
107
+ self . log_record
108
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value ) ) ;
108
109
}
109
110
110
111
fn record_f64 ( & mut self , field : & tracing:: field:: Field , value : f64 ) {
111
- self . log_record_attributes
112
- . push ( ( field. name ( ) . into ( ) , value . into ( ) ) ) ;
112
+ self . log_record
113
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value ) ) ;
113
114
}
114
115
115
116
fn record_i64 ( & mut self , field : & tracing:: field:: Field , value : i64 ) {
116
117
#[ cfg( feature = "experimental_metadata_attributes" ) ]
117
118
if is_duplicated_metadata ( field. name ( ) ) {
118
119
return ;
119
120
}
120
- self . log_record_attributes
121
- . push ( ( field. name ( ) . into ( ) , value . into ( ) ) ) ;
121
+ self . log_record
122
+ . add_attribute ( Key :: new ( field. name ( ) ) , AnyValue :: from ( value ) ) ;
122
123
}
123
124
124
125
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
@@ -173,11 +174,10 @@ where
173
174
log_record. set_severity_number ( severity_of_level ( meta. level ( ) ) ) ;
174
175
log_record. set_severity_text ( meta. level ( ) . to_string ( ) . into ( ) ) ;
175
176
176
- let mut visitor = EventVisitor :: default ( ) ;
177
+ let mut visitor = EventVisitor :: new ( & mut log_record ) ;
177
178
visitor. visit_metadata ( meta) ;
178
179
// Visit fields.
179
180
event. record ( & mut visitor) ;
180
- visitor. push_to_otel_log_record ( & mut log_record) ;
181
181
182
182
self . logger . emit ( log_record) ;
183
183
}
0 commit comments