1
1
use tracing_appender:: rolling:: { RollingFileAppender , Rotation } ;
2
2
use tracing_core:: Subscriber ;
3
3
use tracing_subscriber:: {
4
- fmt:: { self , time:: FormatTime , FormatEvent , FormatFields , FormattedFields } ,
4
+ field:: RecordFields ,
5
+ fmt:: {
6
+ self ,
7
+ format:: { DefaultFields , Writer } ,
8
+ time:: FormatTime ,
9
+ FormatEvent , FormatFields , FormattedFields ,
10
+ } ,
5
11
layer:: SubscriberExt ,
6
12
registry:: LookupSpan ,
7
13
util:: SubscriberInitExt ,
@@ -135,7 +141,25 @@ where
135
141
136
142
let writer = builder. build ( & c. path ) . expect ( "Failed to create a rolling file appender." ) ;
137
143
144
+ // Another fields formatter is necessary because of this bug
145
+ // https://github.com/tokio-rs/tracing/issues/1372. Using a new
146
+ // formatter for the fields forces to record them in different span
147
+ // extensions, and thus remove the duplicated fields in the span.
148
+ #[ derive( Default ) ]
149
+ struct FieldsFormatterForFiles ( DefaultFields ) ;
150
+
151
+ impl < ' writer > FormatFields < ' writer > for FieldsFormatterForFiles {
152
+ fn format_fields < R : RecordFields > (
153
+ & self ,
154
+ writer : Writer < ' writer > ,
155
+ fields : R ,
156
+ ) -> std:: fmt:: Result {
157
+ self . 0 . format_fields ( writer, fields)
158
+ }
159
+ }
160
+
138
161
fmt:: layer ( )
162
+ . fmt_fields ( FieldsFormatterForFiles :: default ( ) )
139
163
. event_format ( EventFormatter :: new ( ) )
140
164
// EventFormatter doesn't support ANSI colors anyways, but the
141
165
// default field formatter does, which is unhelpful for iOS +
@@ -147,15 +171,34 @@ where
147
171
Layer :: and_then (
148
172
file_layer,
149
173
config. write_to_stdout_or_system . then ( || {
174
+ // Another fields formatter is necessary because of this bug
175
+ // https://github.com/tokio-rs/tracing/issues/1372. Using a new
176
+ // formatter for the fields forces to record them in different span
177
+ // extensions, and thus remove the duplicated fields in the span.
178
+ #[ derive( Default ) ]
179
+ struct FieldsFormatterFormStdoutOrSystem ( DefaultFields ) ;
180
+
181
+ impl < ' writer > FormatFields < ' writer > for FieldsFormatterFormStdoutOrSystem {
182
+ fn format_fields < R : RecordFields > (
183
+ & self ,
184
+ writer : Writer < ' writer > ,
185
+ fields : R ,
186
+ ) -> std:: fmt:: Result {
187
+ self . 0 . format_fields ( writer, fields)
188
+ }
189
+ }
190
+
150
191
#[ cfg( not( target_os = "android" ) ) ]
151
192
return fmt:: layer ( )
193
+ . fmt_fields ( FieldsFormatterFormStdoutOrSystem :: default ( ) )
152
194
. event_format ( EventFormatter :: new ( ) )
153
195
// See comment above.
154
196
. with_ansi ( false )
155
197
. with_writer ( std:: io:: stderr) ;
156
198
157
199
#[ cfg( target_os = "android" ) ]
158
200
return fmt:: layer ( )
201
+ . fmt_fields ( FieldsFormatterFormStdoutOrSystem :: default ( ) )
159
202
. event_format ( EventFormatter :: for_logcat ( ) )
160
203
// See comment above.
161
204
. with_ansi ( false )
0 commit comments