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
//! A log4rs appender which routes logging events to dynamically created sub-appenders.
//!
//! For example, you may want to direct output to different directories based on a "job ID" stored
//! in the MDC:
//!
//! ```yaml
//! appenders:
//!   job:
//!     kind: routing
//!     router:
//!       kind: pattern
//!       pattern:
//!         kind: file
//!         path: "log/jobs/${mdc(job_id)}/output.log"
//!     cache:
//!       idle_timeout: 30 seconds
//! loggers:
//!   server::job_runner:
//!     appenders:
//!     - job
//! ```
//!
//! ```ignore
//! #[macro_use]
//! extern crate log;
//! extern crate log_mdc;
//!
//! # fn generate_job_id() -> String { "foobar".to_owned() }
//! # fn main() {
//! let job_id = generate_job_id();
//! log_mdc::insert("job_id", job_id);
//!
//! info!("Starting job");
//! # }
//! ```
#![doc(html_root_url = "https://docs.rs/log4rs-routing-appender/0.4.0")]
#![warn(missing_docs)]
extern crate antidote;
extern crate linked_hash_map;
extern crate log;
extern crate log4rs;

#[cfg(feature = "humantime")]
extern crate humantime;
#[cfg(feature = "log-mdc")]
extern crate log_mdc;
#[cfg(feature = "ordered-float")]
extern crate ordered_float;
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "serde-value")]
extern crate serde_value;

#[cfg(feature = "serde_derive")]
#[macro_use]
extern crate serde_derive;

use antidote::Mutex;
use log::Record;
use log4rs::append::Append;
use std::error::Error;
use std::fmt;
use std::time::Duration;

#[cfg(feature = "file")]
use log4rs::file::{Deserialize, Deserializers};
#[cfg(feature = "file")]
use serde::de::{self, Deserialize as SerdeDeserialize};
#[cfg(feature = "file")]
use serde_value::Value;
#[cfg(feature = "file")]
use std::collections::BTreeMap;

use route::{Cache, Route};

pub mod route;

/// Configuration for the `RoutingAppender`.
#[cfg(feature = "file")]
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RoutingAppenderConfig {
    router: RouterConfig,
    #[serde(default)]
    cache: CacheConfig,
}

#[cfg(feature = "file")]
#[derive(Deserialize, Default)]
#[serde(deny_unknown_fields)]
struct CacheConfig {
    #[serde(deserialize_with = "de_duration", default)]
    idle_timeout: Option<Duration>,
}

/// Registers the following mappings:
///
/// * Appenders
///     * "routing" -> `RoutingAppenderDeserializer`
/// * Routers
///     * "pattern" -> `PatternAppenderDeserializer`
///         * Requires the `pattern-router` feature (enabled by default).
#[cfg(feature = "file")]
pub fn register(d: &mut Deserializers) {
    d.insert("routing", RoutingAppenderDeserializer);

    #[cfg(feature = "pattern-router")]
    d.insert("pattern", route::pattern::PatternRouterDeserializer);
}

/// An appender which routes log events to dynamically constructed sub-appenders.
pub struct RoutingAppender {
    router: Box<Route>,
    cache: Mutex<Cache>,
}

impl fmt::Debug for RoutingAppender {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("RoutingAppender")
            .field("router", &self.router)
            .finish()
    }
}

impl Append for RoutingAppender {
    fn append(&self, record: &Record) -> Result<(), Box<Error + Sync + Send>> {
        let appender = self.router.route(record, &mut self.cache.lock())?;
        appender.appender().append(record)
    }

    fn flush(&self) {}
}

impl RoutingAppender {
    /// Creates a new `RoutingAppender` builder.
    pub fn builder() -> RoutingAppenderBuilder {
        RoutingAppenderBuilder {
            idle_timeout: Duration::from_secs(2 * 60),
        }
    }
}

/// A builder for `RoutingAppender`s.
pub struct RoutingAppenderBuilder {
    idle_timeout: Duration,
}

impl RoutingAppenderBuilder {
    /// Sets the duration after which an appender that has not been used will be removed from the
    /// cache.
    ///
    /// Defaults to 2 minutes.
    pub fn idle_timeout(mut self, idle_timeout: Duration) -> RoutingAppenderBuilder {
        self.idle_timeout = idle_timeout;
        self
    }

    /// Consumes the builder, producing a `RoutingAppender`.
    pub fn build(self, router: Box<Route>) -> RoutingAppender {
        RoutingAppender {
            router: router,
            cache: Mutex::new(Cache::new(self.idle_timeout)),
        }
    }
}

/// A deserializer for the `RoutingAppender`.
///
/// # Configuration
///
/// ```yaml
/// kind: routing
///
/// # The router used to determine the appender to use for a log event.
/// # Required.
/// router:
///   kind: pattern
///   pattern:
///     kind: file
///     path: "log/${mdc(job_id)}.log"
///
/// # Configuration of the cache of appenders generated by the router.
/// cache:
///
///   # The duration that a cached appender has been unused after which it
///   # will be disposed of. Defaults to 2 minutes.
///   idle_timeout: 2 minutes
/// ```
#[cfg(feature = "file")]
pub struct RoutingAppenderDeserializer;

#[cfg(feature = "file")]
impl Deserialize for RoutingAppenderDeserializer {
    type Trait = Append;
    type Config = RoutingAppenderConfig;

    fn deserialize(
        &self,
        config: RoutingAppenderConfig,
        deserializers: &Deserializers,
    ) -> Result<Box<Append>, Box<Error + Sync + Send>> {
        let mut builder = RoutingAppender::builder();
        if let Some(idle_timeout) = config.cache.idle_timeout {
            builder = builder.idle_timeout(idle_timeout);
        }
        let router = deserializers.deserialize(&config.router.kind, config.router.config)?;
        Ok(Box::new(builder.build(router)))
    }
}

#[derive(PartialEq, Eq, Debug)]
#[cfg(feature = "file")]
struct RouterConfig {
    kind: String,
    config: Value,
}

#[cfg(feature = "file")]
impl<'de> de::Deserialize<'de> for RouterConfig {
    fn deserialize<D>(d: D) -> Result<RouterConfig, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let mut map = BTreeMap::<Value, Value>::deserialize(d)?;

        let kind = match map.remove(&Value::String("kind".to_owned())) {
            Some(kind) => kind.deserialize_into().map_err(|e| e.to_error())?,
            None => return Err(de::Error::missing_field("kind")),
        };

        Ok(RouterConfig {
            kind: kind,
            config: Value::Map(map),
        })
    }
}

#[cfg(feature = "file")]
fn de_duration<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
where
    D: de::Deserializer<'de>,
{
    struct S(Duration);

    impl<'de2> de::Deserialize<'de2> for S {
        fn deserialize<D>(d: D) -> Result<S, D::Error>
        where
            D: de::Deserializer<'de2>,
        {
            struct V;

            impl<'de3> de::Visitor<'de3> for V {
                type Value = S;

                fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
                    fmt.write_str("a duration")
                }

                fn visit_str<E>(self, v: &str) -> Result<S, E>
                where
                    E: de::Error,
                {
                    humantime::parse_duration(v)
                        .map(S)
                        .map_err(|e| E::custom(&e.to_string()))
                }
            }

            d.deserialize_str(V)
        }
    }

    Option::<S>::deserialize(d).map(|d| d.map(|d| d.0))
}

trait CacheInner {
    fn new(expiration: Duration) -> Cache;
}

trait AppenderInner {
    fn appender(&self) -> &Append;
}