q

Function q 

Source
pub fn q<T: Display + ?Sized>(x: &T) -> String
Expand description

Make a quoted and escaped string for JSON.

use s_structured_log::q;

let quoted = q("abc");
assert_eq!(quoted, "\"abc\"");
use s_structured_log::q;

let x = "ab\ncdef\x02\x03猫\"bbb🐈";
let expected = r#""ab\ncdef\u0002\u0003猫\"bbb🐈""#;
assert_eq!(q(x), expected.to_owned());
Examples found in repository?
examples/basic.rs (line 41)
9fn main() {
10    JsonLogger::init(LoggerOutput::Stdout, log::LogLevelFilter::Info);
11
12    s_trace!(json_object! {
13        "trace_key1" => 1,
14        "trace_key2" => "value2"
15    });
16    s_debug!(json_object! {
17        "debug_key1" => 1,
18        "debug_key2" => "value2"
19    });
20    s_info!(json_object! {
21        "info_key1" => 1,
22        "info_key2" => "value2"
23    });
24    s_warn!(json_object! {
25        "warn_key1" => 1,
26        "warn_key2" => "value2"
27    });
28    s_error!(json_object! {
29        "error_key1" => 1,
30        "error_key2" => "value2"
31    });
32
33    trace!("{:?}",
34           json_object! {
35        "trace_key1" => 1,
36        "trace_key2" => "value2"
37    });
38    error!("{}",
39           json_format! {
40        "error_key1" => 1,
41        "error_key2" => q("value2"),
42        "error_key3" => json_format![q("value3"),4]
43    });
44}
More examples
Hide additional examples
examples/complicated_json.rs (line 43)
9fn main() {
10    JsonLogger::init(LoggerOutput::Stderr, log::LogLevelFilter::Info);
11
12    // use json_object!
13    s_info!(json_object! {
14        "Fruits" => json_object! {
15            "on_the_table" => json_object! {
16                "Apple" => 1,
17                "Orange" => "two",
18                "Grape" => 1.2
19            },
20            "in_the_basket" => ["Banana", "Strawberry"]
21        },
22        "Pets" => [
23            json_object! {
24                "name" => "Tama",
25                "kind" => "cat",
26                "age" => 3
27            },
28            json_object! {
29                "name" => "Pochi",
30                "kind" => "dog",
31                "age" => 5
32            }
33        ]
34    });
35
36    // use json_format! and target with `json:` prefix.
37    info!(target: &format!("json:{}", module_path!()),
38          "{}",
39          json_format! {
40        "Fruits" => json_format! {
41            "on_the_table" => json_format! {
42                "Apple" => 1,
43                "Orange" => q("two"),
44                "Grape" => 1.2
45            },
46            "in_the_basket" => json_format![q("Banana"), q("Strawberry")]
47        },
48        "Pets" => json_format![
49            json_format! {
50                "name" => q("Tama"),
51                "kind" => q("cat"),
52                "age" => 3
53            },
54            json_format! {
55                "name" => q("Pochi"),
56                "kind" => q("dog"),
57                "age" => 5
58            }
59        ]
60    });
61
62    // use json_format! and default target.
63    info!("{}",
64          json_format! {
65        "Fruits" => json_format! {
66            "on_the_table" => json_format! {
67                "Apple" => 1,
68                "Orange" => 2,
69                "Grape" => 1.2
70            },
71            "in_the_basket" => json_format![q("Banana"), q("Strawberry")]
72        },
73        "Pets" => json_format![
74            json_format! {
75                "name" => q("Tama"),
76                "kind" => q("cat")
77            },
78            json_format! {
79                "name" => q("Pochi"),
80                "kind" => q("dog")
81            }
82        ]
83    });
84}