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
use log::LogRecord;
use LogConfig;
use LogSpecification;
use flexi_error::FlexiLoggerError;
use FlexiLogger;
use ReconfigurationHandle;

/// Function type for Format functions.
pub type FormatFunction = fn(&LogRecord) -> String;


/// The standard entry-point for using flexi_logger.
///
/// Create a Logger with your desired (initial) loglevel-specification
///
/// * by specifying a String programmatically,
///   using [Logger::with_str()](struct.Logger.html#method.with_str),
/// * or by expecting a String in the environment,
///   using [Logger::with_env()](struct.Logger.html#method.with_env),
/// * or by providing an explicitly built LogSpecification,
///   using [Logger::with()](struct.Logger.html#method.with),
///
/// use its configuration methods, and finally call [start()](struct.Logger.html#method.start)
/// (or [start_reconfigurable()](struct.Logger.html#method.start_reconfigurable)).
///
/// ## Examples
///
/// ### Use defaults only
///
/// If you initialize flexi_logger like this, it behaves like env_logger:
///
/// ```
/// use flexi_logger::Logger;
///
/// Logger::with_env().start().unwrap();
/// ```
///
/// ### Write to files, use a detailed log-line format that contains the module and line number
///
/// Here we configure flexi_logger to write log entries with
/// time and location info into a log file in folder "log_files",
/// and we provide the loglevel-specification programmatically, as String:
///
/// ```
/// use flexi_logger::{Logger,opt_format};
///
/// Logger::with_str("myprog=debug, mylib=warn")
///             .log_to_file()
///             .directory("log_files")
///             .format(opt_format)
///             .start()
///             .unwrap_or_else(|e|{panic!("Logger initialization failed with {}",e)});
/// ```
///
pub struct Logger {
    spec: LogSpecification,
    config: LogConfig,
}

/// Simple methods for influencing the behavior of the Logger.
impl Logger {
    /// Create a Logger that you provide with an explicit LogSpecification.
    pub fn with(logspec: LogSpecification) -> Logger {
        Logger {
            spec: logspec,
            config: LogConfig::default_config_for_logger(),
        }
    }

    /// Create a Logger that reads the LogSpecification from a String or &str.
    /// [See LogSpecification](struct.LogSpecification.html) for the syntax.
    pub fn with_str<S: AsRef<str>>(s: S) -> Logger {
        let logspec = LogSpecification::parse(s.as_ref());
        Logger {
            spec: logspec,
            config: LogConfig::default_config_for_logger(),
        }
    }

    /// Create a Logger that reads the LogSpecification from the environment variable RUST_LOG.
    pub fn with_env() -> Logger {
        Logger {
            spec: LogSpecification::env(),
            config: LogConfig::default_config_for_logger(),
        }
    }

    /// Makes the logger write all logs to a file, rather than to stderr.
    pub fn log_to_file(mut self) -> Logger {
        self.config.log_to_file = true;
        self
    }

    /// Makes the logger print an info message to stdout when a new file is used for log-output.
    pub fn print_message(mut self) -> Logger {
        self.config.print_message = true;
        self
    }

    /// Makes the logger write all logged error messages additionally to stdout.
    pub fn duplicate_error(mut self) -> Logger {
        self.config.duplicate_error = true;
        self
    }

    /// Makes the logger write all logged error, warning, and info messages additionally to stdout.
    pub fn duplicate_info(mut self) -> Logger {
        self.config.duplicate_info = true;
        self
    }

    /// Makes the logger use the provided format function for the log entries,
    /// rather than the default ([formats::default_format](fn.default_format.html)).
    pub fn format(mut self, format: FormatFunction) -> Logger {
        self.config.format = format;
        self
    }

    /// Specifies a folder for the log files.
    ///
    /// This parameter only has an effect if log_to_file is set to true.
    /// If the specified folder does not exist, the initialization will fail.
    /// By default, the log files are created in the folder where the program was started.
    pub fn directory<S: Into<String>>(mut self, directory: S) -> Logger {
        self.config.directory = Some(directory.into());
        self
    }

    /// Specifies a suffix for the log files.
    ///
    /// This parameter only has an effect if log_to_file is set to true.
    pub fn suffix<S: Into<String>>(mut self, suffix: S) -> Logger {
        self.config.suffix = Some(suffix.into());
        self
    }

    /// Makes the logger not include a timestamp into the names of the log files
    /// (log_to_file must be chosen, too).
    pub fn suppress_timestamp(mut self) -> Logger {
        self.config.timestamp = false;
        self
    }

    /// This option only has an effect if log_to_file is used, too.
    ///
    /// By default, the log file will grow indefinitely.
    /// With this option, when the log file reaches or exceeds the specified file size,
    /// the file will be closed and a new file will be opened.
    /// Also he filename pattern changes - instead of the timestamp a serial number is included into the filename.
    pub fn rotate_over_size(mut self, rotate_over_size: usize) -> Logger {
        self.config.rotate_over_size = Some(rotate_over_size);
        self
    }

    /// This option only has an effect if log_to_file is used, too.
    ///
    /// The specified String is added to the log file name.
    pub fn discriminant<S: Into<String>>(mut self, discriminant: S) -> Logger {
        self.config.discriminant = Some(discriminant.into());
        self
    }

    /// This option only has an effect if log_to_file is used, too.
    ///
    /// The specified String will be used on linux systems to create in the current folder
    /// a symbolic link to the current log file.
    pub fn create_symlink<S: Into<String>>(mut self, symlink: S) -> Logger {
        self.config.create_symlink = Some(symlink.into());
        self
    }

    /// Consumes the Logger object and initializes the flexi_logger.
    pub fn start(self) -> Result<(), FlexiLoggerError> {
        FlexiLogger::start(self.config, self.spec)
    }

    /// Consumes the Logger object and initializes the flexi_logger in a way that
    /// subsequently the log specification can be exchanged dynamically.
    ///
    /// The resulting logger is still fast, but measurable slower for those log-calls (trace!() etc)
    /// that are on a deeper level than the deepest level in the LogSpecification.
    /// This is because the Log crate has an optimization for returning very fast from deep-level
    /// log calls, but the deepest level needs be given at initialization and cannot be updated
    /// later.
    ///
    /// Here is the output from a benchmark test, runnning on a windows laptop:
    ///
    ///  ```text
    ///   1   PS C:\dev\rust\projects\flexi_logger> cargo bench --test bench_standard -- --nocapture
    ///   2       Finished release [optimized] target(s) in 0.0 secs
    ///   3        Running target\release\deps\bench_standard-158f621674f85c86.exe
    ///   4
    ///   5   running 4 tests
    ///   6   test b10_no_logger_active  ... bench:         136 ns/iter (+/- 30)
    ///   7   test b20_initialize_logger ... bench:           0 ns/iter (+/- 0)
    ///   8   test b30_relevant_logs     ... bench:   1,676,793 ns/iter (+/- 342,747)
    ///   9   test b40_suppressed_logs   ... bench:         134 ns/iter (+/- 5)
    ///  10
    ///  11   test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out
    ///  12
    ///  13   PS C:\dev\rust\projects\flexi_logger> cargo bench --test bench_reconfigurable -- --nocapture
    ///  14       Finished release [optimized] target(s) in 0.0 secs
    ///  15        Running target\release\deps\bench_reconfigurable-bc6bb7d69906fc2f.exe
    ///  16
    ///  17   running 4 tests
    ///  18   test b10_no_logger_active  ... bench:         134 ns/iter (+/- 19)
    ///  19   test b20_initialize_logger ... bench:           0 ns/iter (+/- 0)
    ///  20   test b30_relevant_logs     ... bench:   1,665,871 ns/iter (+/- 306,734)
    ///  21   test b40_suppressed_logs   ... bench:       5,208 ns/iter (+/- 564)
    ///  22
    ///  23   test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out
    ///  ```
    ///
    /// It shows that logging is fastest when no logger is active (lines 6 and 18).
    /// And it is just as fast when the above-mentioned optimization kicks in (line 9).
    ///
    /// Logging is expensive when logs are really written (line 8 and 20), independent of the
    /// reconfigurability feature of the flexi_logger.
    ///
    /// The measurable, but still in most cases not important price for reconfigurability
    /// can be seen by comparing lines 9 and 21.
    ///
    pub fn start_reconfigurable(self) -> Result<ReconfigurationHandle, FlexiLoggerError> {
        FlexiLogger::start_reconfigurable(self.config, self.spec)
    }

    // used in tests only
    #[doc(hidden)]
    #[allow(dead_code)]
    fn get_config(&self) -> &LogConfig {
        &self.config
    }
}

/// Alternative set of methods to control the behavior of the Logger.
/// use these methods when you want to control the settings dynamically, e.g. with doc_opts.
impl Logger {
    /// With true, makes the logger write all logs to a file, otherwise to stderr.
    pub fn o_log_to_file(mut self, log_to_file: bool) -> Logger {
        self.config.log_to_file = log_to_file;
        self
    }

    /// With true, makes the logger print an info message to stdout when a new file is used for log-output.
    pub fn o_print_message(mut self, print_message: bool) -> Logger {
        self.config.print_message = print_message;
        self
    }

    /// With true, makes the logger write all logged error messages additionally to stdout.
    pub fn o_duplicate_error(mut self, duplicate_error: bool) -> Logger {
        self.config.duplicate_error = duplicate_error;
        self
    }

    /// With true, makes the logger write all logged error, warning, and info messages additionally to stdout.
    pub fn o_duplicate_info(mut self, duplicate_info: bool) -> Logger {
        self.config.duplicate_info = duplicate_info;
        self
    }

    /// Specifies a folder for the log files.
    ///
    /// This parameter only has an effect if log_to_file is set to true.
    /// If the specified folder does not exist, the initialization will fail.
    /// With None, the log files are created in the folder where the program was started.
    pub fn o_directory<S: Into<String>>(mut self, directory: Option<S>) -> Logger {
        self.config.directory = directory.map(|d| d.into());
        self
    }

    /// Specifies a suffix for the log files.
    ///
    /// This parameter only has an effect if log_to_file is set to true.
    /// By default, the suffix 'log' is used. With None, no suffix is used.
    pub fn o_suffix<S: Into<String>>(mut self, suffix: Option<S>) -> Logger {
        self.config.suffix = suffix.map(|s| s.into());
        self
    }

    /// With true, makes the logger include a timestamp into the names of the log files.
    /// (log_to_file must be chosen, too).
    pub fn o_timestamp(mut self, timestamp: bool) -> Logger {
        self.config.timestamp = timestamp;
        self
    }

    /// This option only has an effect if log_to_file is used, too.
    ///
    /// By default, and with None, the log file will grow indefinitely.
    /// If a size is set, when the log file reaches or exceeds the specified size,
    /// the file will be closed and a new file will be opened.
    /// Also the filename pattern changes - instead of the timestamp a serial number
    /// is included into the filename.
    pub fn o_rotate_over_size(mut self, rotate_over_size: Option<usize>) -> Logger {
        self.config.rotate_over_size = rotate_over_size;
        self
    }

    /// This option only has an effect if log_to_file is used, too.
    ///
    /// The specified String is added to the log file name.
    pub fn o_discriminant<S: Into<String>>(mut self, discriminant: Option<S>) -> Logger {
        self.config.discriminant = discriminant.map(|d| d.into());
        self
    }

    /// This option only has an effect if log_to_file is used, too.
    ///
    /// If a String is specified, it will be used on linux systems to create in the current folder
    /// a symbolic link with this name to the current log file.
    pub fn o_create_symlink<S: Into<String>>(mut self, symlink: Option<S>) -> Logger {
        self.config.create_symlink = symlink.map(|s| s.into());
        self
    }
}


#[cfg(test)]
mod tests {
    extern crate log;
    use Logger;

    #[test]
    fn verify_defaults() {
        let logger = Logger::with_str("");
        let config = logger.get_config();

        assert!(config.log_to_file == false);
        assert!(config.print_message == false);
        assert!(config.duplicate_error == false);
        assert!(config.duplicate_info == false);
        assert!(config.directory == None);
        assert!(config.suffix == Some("log".to_string()));
        assert!(config.timestamp == true);
        assert!(config.rotate_over_size == None);
        assert!(config.discriminant == None);
        assert!(config.create_symlink == None);
    }

    #[test]
    fn verify_non_defaults() {
        let logger = Logger::with_str("")
            .log_to_file()
            .print_message()
            .duplicate_error()
            .duplicate_info()
            .directory("logdir")
            .suffix("trc")
            .suppress_timestamp()
            .rotate_over_size(10_000_000)
            .discriminant("TEST")
            .create_symlink("current_log_file");
        let config = logger.get_config();

        assert!(config.log_to_file == true);
        assert!(config.print_message == true);
        assert!(config.duplicate_error == true);
        assert!(config.duplicate_info == true);
        assert!(config.directory == Some("logdir".to_string()));
        assert!(config.suffix == Some("trc".to_string()));
        assert!(config.timestamp == false);
        assert!(config.rotate_over_size == Some(10_000_000));
        assert!(config.discriminant == Some("TEST".to_string()));
        assert!(config.create_symlink == Some("current_log_file".to_string()));
    }
}