logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use sentry_core::protocol::{Breadcrumb, Event, Level, Map, Value};
use slog::{Key, OwnedKVList, Record, Serializer, KV};
use std::fmt;

/// Converts a [`slog::Level`] to a Sentry [`Level`]
pub fn convert_log_level(level: slog::Level) -> Level {
    match level {
        slog::Level::Trace | slog::Level::Debug => Level::Debug,
        slog::Level::Info => Level::Info,
        slog::Level::Warning => Level::Warning,
        slog::Level::Error | slog::Level::Critical => Level::Error,
    }
}

struct MapSerializer<'a>(&'a mut Map<String, Value>);

macro_rules! impl_into {
    ($t:ty => $f:ident) => {
        fn $f(&mut self, key: Key, val: $t) -> slog::Result {
            self.0.insert(key.into(), val.into());
            Ok(())
        }
    };
}
impl<'a> Serializer for MapSerializer<'a> {
    fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments) -> slog::Result {
        self.0.insert(key.into(), format!("{}", val).into());
        Ok(())
    }

    fn emit_serde(&mut self, key: Key, val: &dyn slog::SerdeValue) -> slog::Result {
        let value = serde_json::to_value(val.as_serde()).map_err(|_e| slog::Error::Other)?;
        self.0.insert(key.into(), value);
        Ok(())
    }

    impl_into! { usize => emit_usize }
    impl_into! { isize => emit_isize }
    impl_into! { bool  => emit_bool  }
    impl_into! { u8    => emit_u8    }
    impl_into! { i8    => emit_i8    }
    impl_into! { u16   => emit_u16   }
    impl_into! { i16   => emit_i16   }
    impl_into! { u32   => emit_u32   }
    impl_into! { i32   => emit_i32   }
    impl_into! { f32   => emit_f32   }
    impl_into! { u64   => emit_u64   }
    impl_into! { i64   => emit_i64   }
    impl_into! { f64   => emit_f64   }
    impl_into! { &str  => emit_str   }
}

/// Adds the data from a [`slog::KV`] into a Sentry [`Map`].
fn add_kv_to_map(map: &mut Map<String, Value>, record: &Record, kv: &impl KV) {
    // TODO: Do something with these errors?
    let _ = record.kv().serialize(record, &mut MapSerializer(map));
    let _ = kv.serialize(record, &mut MapSerializer(map));
}

/// Creates a Sentry [`Breadcrumb`] from the [`Record`].
pub fn breadcrumb_from_record(record: &Record, values: &OwnedKVList) -> Breadcrumb {
    let mut data = Map::new();
    add_kv_to_map(&mut data, record, values);

    Breadcrumb {
        ty: "log".into(),
        message: Some(record.msg().to_string()),
        level: convert_log_level(record.level()),
        data,
        ..Default::default()
    }
}

/// Creates a simple message [`Event`] from the [`Record`].
pub fn event_from_record(record: &Record, values: &OwnedKVList) -> Event<'static> {
    let mut extra = Map::new();
    add_kv_to_map(&mut extra, record, values);
    Event {
        message: Some(record.msg().to_string()),
        level: convert_log_level(record.level()),
        extra,
        ..Default::default()
    }
}

/// Creates an exception [`Event`] from the [`Record`].
///
/// # Examples
///
/// ```
/// let args = format_args!("");
/// let record = slog::record!(slog::Level::Error, "", &args, slog::b!());
/// let kv = slog::o!().into();
/// let event = sentry_slog::exception_from_record(&record, &kv);
/// ```
pub fn exception_from_record(record: &Record, values: &OwnedKVList) -> Event<'static> {
    // TODO: Exception records in Sentry need a valid type, value and full stack trace to support
    // proper grouping and issue metadata generation. log::Record does not contain sufficient
    // information for this. However, it may contain a serialized error which we can parse to emit
    // an exception record.
    event_from_record(record, values)
}

#[cfg(test)]
mod test {
    use super::*;
    use serde::Serialize;

    use slog::{b, o, record, Level};

    #[derive(Serialize, Clone)]
    struct Something {
        msg: String,
        count: usize,
    }

    impl slog::Value for Something {
        fn serialize(
            &self,
            _record: &Record,
            key: Key,
            serializer: &mut dyn slog::Serializer,
        ) -> slog::Result {
            serializer.emit_serde(key, self)
        }
    }

    impl slog::SerdeValue for Something {
        fn as_serde(&self) -> &dyn erased_serde::Serialize {
            self
        }

        fn to_sendable(&self) -> Box<dyn slog::SerdeValue + Send + 'static> {
            Box::new(self.clone())
        }
    }

    #[test]
    fn test_slog_kvs() {
        let extras = o!("lib" => "sentry", "version" => 1, "test" => true);

        let mut map: Map<String, Value> = Map::new();

        add_kv_to_map(
            &mut map,
            &record!(
                Level::Debug,
                "test",
                &format_args!("Hello, world!"),
                b!("something" => &Something {
                    msg: "message!".into(),
                    count: 42,
                })
            ),
            &extras,
        );

        assert_eq!(map.get("lib"), Some(&"sentry".into()));
        assert_eq!(map.get("version"), Some(&1.into()));
        assert_eq!(map.get("test"), Some(&true.into()));
        assert_eq!(
            map.get("something"),
            Some(&Value::Object(
                vec![
                    ("msg".to_string(), Value::from("message!")),
                    ("count".to_string(), Value::from(42))
                ]
                .into_iter()
                .collect()
            ))
        )
    }
}