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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#![warn(missing_debug_implementations, missing_docs)]

//! Provides a [`tracing`](https://docs.rs/tracing) [`Layer`] for Graylog structured logging.
//!
//! # Usage
//!
//! ```rust
//! use std::net::SocketAddr;
//! use tracing_gelf::Logger;
//!
//! #[tokio::main]
//! async fn main() {
//!    // Graylog address
//!    let address = "127.0.0.1:12201";
//!
//!    // Initialize subscriber
//!    let mut conn_handle = Logger::builder().init_tcp(address).unwrap();
//!
//!    // Spawn background task
//!    // Any futures executor can be used
//!    tokio::spawn(async move { conn_handle.connect().await });
//!
//!    // Send a log to Graylog
//!    tracing::info!(message = "oooh, what's in here?");
//!
//!    // Create a span
//!    let span = tracing::info_span!("cave");
//!    span.in_scope(|| {
//!        let test = tracing::info_span!("deeper in cave", smell = "damp");
//!        test.in_scope(|| {
//!            // Send a log to Graylog, inside a nested span
//!            tracing::warn!(message = "oh god, it's dark in here");
//!        })
//!    });
//!
//!    // Send a log to Graylog
//!    tracing::error!(message = "i'm glad to be out", spook_lvl = 3, ruck_sack = ?["glasses", "inhaler", "large bat"]);
//! }
//! ```
//!
//! # GELF Encoding
//!
//! [`Events`](tracing_core::Event) are encoded into [GELF format] as follows:
//! * [Event] fields are inserted as [GELF] additional fields, `_field_name`.
//! * [Event] field named `message` is renamed to `short_message`.
//! * If `short_message` (or `message`) [Event] field is missing then `short_message` is
//! set to the empty string.
//! * [Event] fields whose names collide with [GELF] required fields are coerced
//! into the required types and overrides defaults given in the builder.
//! * The hierarchy of spans is concatenated and inserted as `span_a:span_b:span_c` and
//! inserted as an additional field `_span`.
//!
//! [GELF]: https://docs.graylog.org/en/3.1/pages/gelf.html
//! [GELF format]: https://docs.graylog.org/en/3.1/pages/gelf.html

mod connection;
mod visitor;

use std::{borrow::Cow, collections::HashMap, fmt::Display};

use bytes::Bytes;
use futures_channel::mpsc;
use serde_json::{map::Map, Value};
use tokio::net::ToSocketAddrs;
use tracing_core::{
    dispatcher::SetGlobalDefaultError,
    span::{Attributes, Id, Record},
    Event, Subscriber,
};
use tracing_subscriber::{
    layer::{Context, Layer},
    registry::LookupSpan,
    Registry,
};

pub use connection::*;

const DEFAULT_BUFFER: usize = 512;
const DEFAULT_VERSION: &str = "1.1";

/// A [`Layer`] responsible for sending structured logs to Graylog.
#[derive(Debug)]
pub struct Logger {
    base_object: HashMap<Cow<'static, str>, Value>,
    line_numbers: bool,
    file_names: bool,
    module_paths: bool,
    spans: bool,
    sender: mpsc::Sender<Bytes>,
}

impl Logger {
    /// Creates a default [`Logger`] configuration, which can then be customized.
    pub fn builder() -> Builder {
        Builder::default()
    }
}

/// The error type for [`Logger`] building.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum BuilderError {
    /// Could not resolve the hostname.
    #[error("hostname resolution failed")]
    HostnameResolution(#[source] std::io::Error),
    /// Could not coerce the [`OsString`](std::ffi::OsString) into a string.
    #[error("hostname could not be parsed as an OsString: {}", .0.to_string_lossy().as_ref())]
    OsString(std::ffi::OsString),
    /// Global dispatcher failed.
    #[error("global dispatcher failed to initialize")]
    Global(#[source] SetGlobalDefaultError),
}

/// A builder for [`Logger`].
#[derive(Debug)]
pub struct Builder {
    additional_fields: HashMap<Cow<'static, str>, Value>,
    version: Option<String>,
    host: Option<String>,
    file_names: bool,
    line_numbers: bool,
    module_paths: bool,
    spans: bool,
    buffer: Option<usize>,
}

impl Default for Builder {
    fn default() -> Self {
        Builder {
            additional_fields: HashMap::with_capacity(32),
            version: None,
            host: None,
            file_names: true,
            line_numbers: true,
            module_paths: true,
            spans: true,
            buffer: None,
        }
    }
}

impl Builder {
    /// Adds a persistent additional field to the GELF messages.
    pub fn additional_field<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Display,
        V: Into<Value>,
    {
        let coerced_value: Value = match value.into() {
            Value::Number(n) => Value::Number(n),
            Value::String(x) => Value::String(x),
            x => Value::String(x.to_string()),
        };
        self.additional_fields
            .insert(format!("_{}", key).into(), coerced_value);
        self
    }

    /// Sets the GELF version number. Defaults to "1.1".
    pub fn version<V>(mut self, version: V) -> Self
    where
        V: ToString,
    {
        self.version = Some(version.to_string());
        self
    }

    /// Sets the `host` field. Defaults to the system's host name.
    pub fn host<V>(mut self, host: V) -> Self
    where
        V: ToString,
    {
        self.host = Some(host.to_string());
        self
    }

    /// Sets whether line numbers should be logged. Defaults to true.
    pub fn line_numbers(mut self, value: bool) -> Self {
        self.line_numbers = value;
        self
    }

    /// Sets whether file names should be logged. Defaults to true.
    pub fn file_names(mut self, value: bool) -> Self {
        self.file_names = value;
        self
    }

    /// Sets whether module paths should be logged. Defaults to true.
    pub fn module_paths(mut self, value: bool) -> Self {
        self.module_paths = value;
        self
    }

    /// Sets the buffer length. Defaults to 512.
    pub fn buffer(mut self, length: usize) -> Self {
        self.buffer = Some(length);
        self
    }

    fn connect<A, Conn>(
        self,
        addr: A,
        conn: Conn,
    ) -> Result<(Logger, ConnectionHandle<A, Conn>), BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        // Persistent fields
        let mut base_object = self.additional_fields;

        // Get hostname
        let hostname = if let Some(host) = self.host {
            host
        } else {
            hostname::get()
                .map_err(BuilderError::HostnameResolution)?
                .into_string()
                .map_err(BuilderError::OsString)?
        };
        base_object.insert("host".into(), hostname.into());

        // Add version
        let version = self.version.unwrap_or_else(|| DEFAULT_VERSION.to_string());
        base_object.insert("version".into(), version.into());

        // Set buffer
        let buffer = self.buffer.unwrap_or(DEFAULT_BUFFER);

        // Construct background task
        let (sender, receiver) = mpsc::channel::<Bytes>(buffer);
        let handle = ConnectionHandle {
            addr,
            receiver,
            conn,
        };
        let logger = Logger {
            base_object,
            file_names: self.file_names,
            line_numbers: self.line_numbers,
            module_paths: self.module_paths,
            spans: self.spans,
            sender,
        };

        Ok((logger, handle))
    }

    /// Returns a [`Logger`] and its UDP [`ConnectionHandle`].
    pub fn connect_udp<A>(
        self,
        addr: A,
    ) -> Result<(Logger, ConnectionHandle<A, UdpConnection>), BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        self.connect(addr, UdpConnection)
    }

    /// Returns a [`Logger`] and its TCP [`ConnectionHandle`].
    pub fn connect_tcp<A>(
        self,
        addr: A,
    ) -> Result<(Logger, ConnectionHandle<A, TcpConnection>), BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        self.connect(addr, TcpConnection)
    }

    /// Returns a [`Logger`] and its TLS [`ConnectionHandle`].
    #[cfg(feature = "rustls-tls")]
    pub fn connect_tls<A>(
        self,
        addr: A,
        server_name: rustls_pki_types::ServerName<'static>,
        client_config: std::sync::Arc<tokio_rustls::rustls::ClientConfig>,
    ) -> Result<(Logger, ConnectionHandle<A, TlsConnection>), BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        self.connect(
            addr,
            TlsConnection {
                server_name,
                client_config,
            },
        )
    }

    /// Initialize logging with a given [`Subscriber`] and returns its UDP [`ConnectionHandle`].
    pub fn init_udp_with_subscriber<S, A>(
        self,
        addr: A,
        subscriber: S,
    ) -> Result<ConnectionHandle<A, UdpConnection>, BuilderError>
    where
        S: Subscriber + for<'a> LookupSpan<'a>,
        S: Send + Sync + 'static,
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        let (logger, bg_task) = self.connect_udp(addr)?;
        let subscriber = Layer::with_subscriber(logger, subscriber);
        tracing_core::dispatcher::set_global_default(tracing_core::dispatcher::Dispatch::new(
            subscriber,
        ))
        .map_err(BuilderError::Global)?;

        Ok(bg_task)
    }

    /// Initializes logging with a given [`Subscriber`] and returns its TCP [`ConnectionHandle`].
    pub fn init_tcp_with_subscriber<A, S>(
        self,
        addr: A,
        subscriber: S,
    ) -> Result<ConnectionHandle<A, TcpConnection>, BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,

        S: Subscriber + for<'a> LookupSpan<'a>,
        S: Send + Sync + 'static,
    {
        let (logger, bg_task) = self.connect_tcp(addr)?;

        // If a subscriber was set then use it as the inner subscriber.
        let subscriber = Layer::with_subscriber(logger, subscriber);
        tracing_core::dispatcher::set_global_default(tracing_core::dispatcher::Dispatch::new(
            subscriber,
        ))
        .map_err(BuilderError::Global)?;

        Ok(bg_task)
    }

    /// Initialize logging with a given [`Subscriber`] and returns its [`ConnectionHandle`].
    #[cfg(feature = "rustls-tls")]
    pub fn init_tls_with_subscriber<A, S>(
        self,
        addr: A,
        server_name: rustls_pki_types::ServerName<'static>,
        client_config: std::sync::Arc<tokio_rustls::rustls::ClientConfig>,
        subscriber: S,
    ) -> Result<ConnectionHandle<A, TlsConnection>, BuilderError>
    where
        A: ToSocketAddrs + Send + Sync + 'static,
        S: Subscriber + for<'a> LookupSpan<'a>,
        S: Send + Sync + 'static,
    {
        let (logger, bg_task) = self.connect_tls(addr, server_name, client_config)?;

        // If a subscriber was set then use it as the inner subscriber.
        let subscriber = Layer::with_subscriber(logger, subscriber);
        tracing_core::dispatcher::set_global_default(tracing_core::dispatcher::Dispatch::new(
            subscriber,
        ))
        .map_err(BuilderError::Global)?;

        Ok(bg_task)
    }

    /// Initializes TCP logging and returns its [`ConnectionHandle`].
    pub fn init_tcp<A>(self, addr: A) -> Result<ConnectionHandle<A, TcpConnection>, BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        self.init_tcp_with_subscriber(addr, Registry::default())
    }

    /// Initializes TLS logging and returns its [`ConnectionHandle`].
    #[cfg(feature = "rustls-tls")]
    pub fn init_tls<A>(
        self,
        addr: A,
        server_name: rustls_pki_types::ServerName<'static>,
        client_config: std::sync::Arc<tokio_rustls::rustls::ClientConfig>,
    ) -> Result<ConnectionHandle<A, TlsConnection>, BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        self.init_tls_with_subscriber(addr, server_name, client_config, Registry::default())
    }

    /// Initialize UDP logging and returns its [`ConnectionHandle`].
    pub fn init_udp<A>(self, addr: A) -> Result<ConnectionHandle<A, UdpConnection>, BuilderError>
    where
        A: ToSocketAddrs,
        A: Send + Sync + 'static,
    {
        self.init_udp_with_subscriber(addr, Registry::default())
    }
}

impl<S> Layer<S> for Logger
where
    S: Subscriber + for<'a> LookupSpan<'a>,
{
    fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
        let span = ctx.span(id).expect("span not found, this is a bug");

        let mut extensions = span.extensions_mut();

        if extensions.get_mut::<Map<String, Value>>().is_none() {
            let mut object = HashMap::with_capacity(16);
            let mut visitor = visitor::AdditionalFieldVisitor::new(&mut object);
            attrs.record(&mut visitor);
            extensions.insert(object);
        }
    }

    fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
        let span = ctx.span(id).expect("span not found, this is a bug");
        let mut extensions = span.extensions_mut();
        if let Some(object) = extensions.get_mut::<HashMap<Cow<'static, str>, Value>>() {
            let mut add_field_visitor = visitor::AdditionalFieldVisitor::new(object);
            values.record(&mut add_field_visitor);
        } else {
            let mut object = HashMap::with_capacity(16);
            let mut add_field_visitor = visitor::AdditionalFieldVisitor::new(&mut object);
            values.record(&mut add_field_visitor);
            extensions.insert(object)
        }
    }

    fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
        // GELF object
        let mut object = self.base_object.clone();

        // Get span name
        if self.spans {
            let span = ctx.current_span().id().and_then(|id| {
                ctx.span_scope(id).map(|scope| {
                    scope.from_root().fold(String::new(), |mut spans, span| {
                        // Add span fields to the base object
                        if let Some(span_object) =
                            span.extensions().get::<HashMap<Cow<'static, str>, Value>>()
                        {
                            object.extend(span_object.clone());
                        }
                        if !spans.is_empty() {
                            spans = format!("{}:{}", spans, span.name());
                        } else {
                            spans = span.name().to_string();
                        }

                        spans
                    })
                })
            });

            if let Some(span) = span {
                object.insert("_span".into(), span.into());
            }
        }

        // Extract metadata
        // Insert level
        let metadata = event.metadata();
        let level_num = match *metadata.level() {
            tracing_core::Level::ERROR => 3,
            tracing_core::Level::WARN => 4,
            tracing_core::Level::INFO => 5,
            tracing_core::Level::DEBUG => 6,
            tracing_core::Level::TRACE => 7,
        };
        object.insert("level".into(), level_num.into());

        // Insert file
        if self.file_names {
            if let Some(file) = metadata.file() {
                object.insert("_file".into(), file.into());
            }
        }

        // Insert line
        if self.line_numbers {
            if let Some(line) = metadata.line() {
                object.insert("_line".into(), line.into());
            }
        }

        // Insert module path
        if self.module_paths {
            if let Some(module_path) = metadata.module_path() {
                object.insert("_module_path".into(), module_path.into());
            }
        }

        // Append additional fields
        let mut add_field_visitor = visitor::AdditionalFieldVisitor::new(&mut object);
        event.record(&mut add_field_visitor);

        if !object.contains_key("short_message") {
            object.insert("short_message".into(), "".into());
        }

        // Serialize
        let object = object
            .into_iter()
            .map(|(key, value)| (key.to_string(), value))
            .collect();
        let final_object = Value::Object(object);
        let mut raw = serde_json::to_vec(&final_object).unwrap(); // This is safe
        raw.push(0);

        // Send
        if let Err(_err) = self.sender.clone().try_send(Bytes::from(raw)) {
            // TODO: Add handler
        };
    }
}