1 //! Tests!
2 use std::{fs, io, io::prelude::*};
3 
4 use log::Level::*;
5 
6 mod support;
7 
8 use support::manual_log;
9 
10 #[test]
test_basic_logging_file_logging()11 fn test_basic_logging_file_logging() {
12     // Create a temporary directory to put a log file into for testing
13     let temp_log_dir = tempdir::TempDir::new("fern").expect("Failed to set up temporary directory");
14     let log_file = temp_log_dir.path().join("test.log");
15 
16     {
17         // Create a basic logger configuration
18         let (_max_level, logger) = fern::Dispatch::new()
19             .format(|out, msg, record| out.finish(format_args!("[{}] {}", record.level(), msg)))
20             .level(log::LevelFilter::Info)
21             .chain(io::stdout())
22             .chain(fern::log_file(log_file).expect("Failed to open log file"))
23             .into_log();
24 
25         let l = &*logger;
26         manual_log(l, Trace, "SHOULD NOT DISPLAY");
27         manual_log(l, Debug, "SHOULD NOT DISPLAY");
28         manual_log(l, Info, "Test information message");
29         manual_log(l, Warn, "Test warning message");
30         manual_log(l, Error, "Test error message");
31 
32         // ensure all File objects are dropped and OS buffers are flushed.
33         log::logger().flush();
34 
35         {
36             let result = {
37                 let mut log_read = fs::File::open(&temp_log_dir.path().join("test.log")).unwrap();
38                 let mut buf = String::new();
39                 log_read.read_to_string(&mut buf).unwrap();
40                 buf
41             };
42             assert!(
43                 !result.contains("SHOULD NOT DISPLAY"),
44                 "expected result not including \"SHOULD_NOT_DISPLAY\", found:\n```\n{}\n```\n",
45                 result
46             );
47             assert!(
48                 result.contains("[INFO] Test information message"),
49                 "expected result including \"[INFO] Test information message\", found:\n```\n{}\n```\n",
50                 result
51             );
52             assert!(
53                 result.contains("[WARN] Test warning message"),
54                 "expected result including \"[WARN] Test warning message\", found:\n```\n{}\n```\n",
55                 result
56             );
57             assert!(
58                 result.contains("[ERROR] Test error message"),
59                 "expected result to not include \"[ERROR] Test error message\", found:\n```\n{}\n```\n",
60                 result
61             );
62         }
63     } // ensure logger is dropped before temp dir
64 
65     temp_log_dir
66         .close()
67         .expect("Failed to clean up temporary directory");
68 }
69 
70 #[test]
test_custom_line_separators()71 fn test_custom_line_separators() {
72     // Create a temporary directory to put a log file into for testing
73     let temp_log_dir = tempdir::TempDir::new("fern").expect("Failed to set up temporary directory");
74     let log_file = temp_log_dir.path().join("test_custom_line_sep.log");
75 
76     {
77         // Create a basic logger configuration
78         let (_max_level, logger) = fern::Dispatch::new()
79             // default format is just the message if not specified
80             // default log level is 'trace' if not specified (logs all messages)
81             // output to the log file with the "\r\n" line separator.
82             .chain(fern::Output::file(
83                 fern::log_file(&log_file).expect("Failed to open log file"),
84                 "\r\n",
85             ))
86             .into_log();
87 
88         let l = &*logger;
89         manual_log(l, Info, "message1");
90         manual_log(l, Info, "message2");
91 
92         // ensure all File objects are dropped and OS buffers are flushed.
93         logger.flush();
94 
95         {
96             let result = {
97                 let mut log_read =
98                     fs::File::open(&temp_log_dir.path().join("test_custom_line_sep.log")).unwrap();
99                 let mut buf = String::new();
100                 log_read.read_to_string(&mut buf).unwrap();
101                 buf
102             };
103             assert_eq!(&result, "message1\r\nmessage2\r\n");
104         }
105     } // ensure logger is dropped before temp dir
106 
107     temp_log_dir
108         .close()
109         .expect("Failed to clean up temporary directory");
110 }
111