apple_quant_core/log/
ext.rs1use colored::{Color, ColoredString, Colorize};
2
3use tracing::Level;
4
5pub trait AnyhowErrorExt {
6 fn now(
7 self,
8 );
9}
10
11impl AnyhowErrorExt for anyhow::Error {
12 fn now(
13 self,
14 ) {
15 let error: &(dyn std::error::Error + 'static) = &*self;
16 tracing::error!(error = error);
17 }
18}
19
20pub trait LogLevelExt {
21 fn colored(
22 self,
23 ) -> ColoredString;
24
25 fn color(
26 &self,
27 ) -> Color;
28}
29
30impl LogLevelExt for Level {
31 fn colored(
32 self,
33 ) -> ColoredString {
34 self.as_str().color(self.color())
35 }
36
37 fn color(
38 &self,
39 ) -> Color {
40 match *self {
41 Self::ERROR => Color::Red,
42 Self::WARN => Color::Yellow,
43 Self::INFO => Color::White,
44 Self::DEBUG => Color::Green,
45 Self::TRACE => Color::Magenta,
46 }
47 }
48}