@@ -144,6 +144,8 @@ use crate::encode::{
144144#[ cfg( feature = "config_parsing" ) ]
145145use crate :: config:: { Deserialize , Deserializers } ;
146146
147+ use self :: parser:: Formatter ;
148+
147149mod parser;
148150
149151thread_local ! (
@@ -505,87 +507,25 @@ impl<'a> From<Piece<'a>> for Chunk {
505507 "P" | "pid" => no_args ( & formatter. args , parameters, FormattedChunk :: ProcessId ) ,
506508 "i" | "tid" => no_args ( & formatter. args , parameters, FormattedChunk :: SystemThreadId ) ,
507509 "t" | "target" => no_args ( & formatter. args , parameters, FormattedChunk :: Target ) ,
508- "X" | "mdc" => {
509- if formatter. args . len ( ) > 2 {
510- return Chunk :: Error ( "expected at most two arguments" . to_owned ( ) ) ;
511- }
512-
513- let key = match formatter. args . first ( ) {
514- Some ( arg) => {
515- if let Some ( arg) = arg. first ( ) {
516- match arg {
517- Piece :: Text ( key) => key. to_owned ( ) ,
518- Piece :: Error ( ref e) => return Chunk :: Error ( e. clone ( ) ) ,
519- _ => return Chunk :: Error ( "invalid MDC key" . to_owned ( ) ) ,
520- }
521- } else {
522- return Chunk :: Error ( "invalid MDC key" . to_owned ( ) ) ;
523- }
524- }
525- None => return Chunk :: Error ( "missing MDC key" . to_owned ( ) ) ,
526- } ;
527-
528- let default = match formatter. args . get ( 1 ) {
529- Some ( arg) => {
530- if let Some ( arg) = arg. first ( ) {
531- match arg {
532- Piece :: Text ( key) => key. to_owned ( ) ,
533- Piece :: Error ( ref e) => return Chunk :: Error ( e. clone ( ) ) ,
534- _ => return Chunk :: Error ( "invalid MDC default" . to_owned ( ) ) ,
535- }
536- } else {
537- return Chunk :: Error ( "invalid MDC default" . to_owned ( ) ) ;
538- }
539- }
540- None => "" ,
541- } ;
542-
543- Chunk :: Formatted {
544- chunk : FormattedChunk :: Mdc ( key. into ( ) , default. into ( ) ) ,
510+ "X" | "mdc" => match kv_parsing ( & formatter) {
511+ Err ( e) => Chunk :: Error ( format ! ( "MDC: {e}" ) ) ,
512+ Ok ( ( key, default) ) => Chunk :: Formatted {
513+ chunk : FormattedChunk :: Mdc ( key, default) ,
545514 params : parameters,
546- }
547- }
515+ } ,
516+ } ,
548517 #[ cfg( feature = "log_kv" ) ]
549- "K" | "key_value" => {
550- if formatter. args . len ( ) > 2 {
551- return Chunk :: Error ( "expected at most two arguments" . to_owned ( ) ) ;
552- }
553-
554- let key = match formatter. args . first ( ) {
555- Some ( arg) => {
556- if let Some ( arg) = arg. first ( ) {
557- match arg {
558- Piece :: Text ( key) => key. to_owned ( ) ,
559- Piece :: Error ( ref e) => return Chunk :: Error ( e. clone ( ) ) ,
560- _ => return Chunk :: Error ( "invalid log::kv key" . to_owned ( ) ) ,
561- }
562- } else {
563- return Chunk :: Error ( "invalid log::kv key" . to_owned ( ) ) ;
564- }
565- }
566- None => return Chunk :: Error ( "missing log::kv key" . to_owned ( ) ) ,
567- } ;
568-
569- let default = match formatter. args . get ( 1 ) {
570- Some ( arg) => {
571- if let Some ( arg) = arg. first ( ) {
572- match arg {
573- Piece :: Text ( value) => value. to_owned ( ) ,
574- Piece :: Error ( ref e) => return Chunk :: Error ( e. clone ( ) ) ,
575- _ => return Chunk :: Error ( "invalid log::kv default" . to_owned ( ) ) ,
576- }
577- } else {
578- return Chunk :: Error ( "invalid log::kv default" . to_owned ( ) ) ;
579- }
580- }
581- None => "" ,
582- } ;
583-
584- Chunk :: Formatted {
585- chunk : FormattedChunk :: Kv ( key. into ( ) , default. into ( ) ) ,
518+ "K" | "key_value" => match kv_parsing ( & formatter) {
519+ Err ( e) => Chunk :: Error ( format ! ( "key_value: {e}" ) ) ,
520+ Ok ( ( key, default) ) => Chunk :: Formatted {
521+ chunk : FormattedChunk :: Kv ( key, default) ,
586522 params : parameters,
587- }
588- }
523+ } ,
524+ } ,
525+ #[ cfg( not( feature = "log_kv" ) ) ]
526+ "K" | "key_value" => Chunk :: Error (
527+ "The log_kv feature is required to parse the key_value argument" . to_owned ( ) ,
528+ ) ,
589529 "" => {
590530 if formatter. args . len ( ) != 1 {
591531 return Chunk :: Error ( "expected exactly one argument" . to_owned ( ) ) ;
@@ -618,6 +558,43 @@ fn no_args(arg: &[Vec<Piece>], params: Parameters, chunk: FormattedChunk) -> Chu
618558 }
619559}
620560
561+ fn kv_parsing < ' a > ( formatter : & ' a Formatter ) -> Result < ( String , String ) , & ' a str > {
562+ if formatter. args . len ( ) > 2 {
563+ return Err ( "expected at most two arguments" ) ;
564+ }
565+
566+ let key = match formatter. args . first ( ) {
567+ Some ( arg) => {
568+ if let Some ( arg) = arg. first ( ) {
569+ match arg {
570+ Piece :: Text ( key) => key. to_owned ( ) ,
571+ Piece :: Error ( ref e) => return Err ( e) ,
572+ _ => return Err ( "invalid key" ) ,
573+ }
574+ } else {
575+ return Err ( "invalid key" ) ;
576+ }
577+ }
578+ None => return Err ( "missing key" ) ,
579+ } ;
580+
581+ let default = match formatter. args . get ( 1 ) {
582+ Some ( arg) => {
583+ if let Some ( arg) = arg. first ( ) {
584+ match arg {
585+ Piece :: Text ( key) => key. to_owned ( ) ,
586+ Piece :: Error ( ref e) => return Err ( e) ,
587+ _ => return Err ( "invalid default" ) ,
588+ }
589+ } else {
590+ return Err ( "invalid default" ) ;
591+ }
592+ }
593+ None => "" ,
594+ } ;
595+ Ok ( ( key. into ( ) , default. into ( ) ) )
596+ }
597+
621598#[ derive( Clone , Eq , PartialEq , Hash , Debug ) ]
622599enum Timezone {
623600 Utc ,
@@ -812,7 +789,9 @@ mod tests {
812789 #[ cfg( feature = "simple_writer" ) ]
813790 use std:: thread;
814791
815- use super :: * ;
792+ #[ cfg( feature = "log_kv" ) ]
793+ use super :: Parser ;
794+ use super :: { Chunk , PatternEncoder } ;
816795 #[ cfg( feature = "simple_writer" ) ]
817796 use crate :: encode:: writer:: simple:: SimpleWriter ;
818797 #[ cfg( feature = "simple_writer" ) ]
@@ -1141,11 +1120,11 @@ mod tests {
11411120 "expected at most two arguments" ,
11421121 ) ,
11431122 ( "[{K({l user_id):<5.5}]" , "expected '}'" ) ,
1144- ( "[{K({l} user_id):<5.5}]" , "invalid log::kv key" ) ,
1145- ( "[{K:<5.5}]" , "missing log::kv key" ) ,
1123+ ( "[{K({l} user_id):<5.5}]" , "key_value: invalid key" ) ,
1124+ ( "[{K:<5.5}]" , "key_value: missing key" ) ,
11461125 ( "[{K(user_id)({l):<5.5}]" , "expected '}'" ) ,
1147- ( "[{K(user_id)({l}):<5.5}]" , "invalid log::kv default" ) ,
1148- ( "[{K(user_id)():<5.5} {M}]" , "invalid log::kv default" ) ,
1126+ ( "[{K(user_id)({l}):<5.5}]" , "key_value: invalid default" ) ,
1127+ ( "[{K(user_id)():<5.5} {M}]" , "key_value: invalid default" ) ,
11491128 ] ;
11501129
11511130 for ( pattern, error_msg) in tests {
0 commit comments