1 /**
2 Specification of ExtendedFileLogger
3 */
4 module logger.extendedfilelogger_spec;
5 
6 import logger;
7 
8 import pijamas;
9 
10 import std.array : array;
11 import std.stdio : File;
12 import std.file : tempDir;
13 import std.algorithm : map, findSkip;
14 import std.path : buildPath;
15 import std.experimental.logger;
16 
17 private enum string outputfile = "output.log";
18 
19 private auto outputFilePath()
20 {
21     return buildPath(tempDir, outputfile);
22 }
23 
24 @("extendedfilelogger")
25 unittest
26 {
27     // describe("logging with simple log pattern must result on the expected log pattern")
28     {
29         // given("We create a ExtendedFileLogger with a SimpleLogPattern")
30         auto file = File(outputFilePath(), "w"); // Overwrite file
31         auto logger = new ExtendedFileLogger(file, LogLevel.all, new SimpleLogPattern());
32 
33         // when("we output some entries to the logger")
34         logger.log("Log");
35         logger.trace("Trace");
36         logger.info("Info");
37         logger.warning("Warning");
38         logger.error("Error");
39         logger.critical("Critical");
40         //logger.fatal("fatal");
41 
42         // then("the output file must containt the same number of lines that log events")
43         file.close();
44         auto output = File(outputFilePath(), "r").byLineCopy.array;
45         output.should.have.length(6);
46 
47         // and("each line must have the expected format")
48         // On windows, the SimpleLogPattern, appends two levels of directory name. A bug ?
49         output[0].should.match(`[0-9T\-:.]+ \[all\] (tests[\\/]logger[\\/])?extendedfilelogger_spec.d:[0-9]+:[a-zA-Z0-9_]+ Log`);
50         output[1].should.match(`[0-9T\-:.]+ \[trace\] (tests[\\/]logger[\\/])?extendedfilelogger_spec.d:[0-9]+:[a-zA-Z0-9_]+ Trace`);
51         output[2].should.match(`[0-9T\-:.]+ \[info\] (tests[\\/]logger[\\/])?extendedfilelogger_spec.d:[0-9]+:[a-zA-Z0-9_]+ Info`);
52         output[3].should.match(`[0-9T\-:.]+ \[warning\] (tests[\\/]logger[\\/])?extendedfilelogger_spec.d:[0-9]+:[a-zA-Z0-9_]+ Warning`);
53         output[4].should.match(`[0-9T\-:.]+ \[error\] (tests[\\/]logger[\\/])?extendedfilelogger_spec.d:[0-9]+:[a-zA-Z0-9_]+ Error`);
54         output[5].should.match(`[0-9T\-:.]+ \[critical\] (tests[\\/]logger[\\/])?extendedfilelogger_spec.d:[0-9]+:[a-zA-Z0-9_]+ Critical`);
55     }
56 
57     // describe("logging with ConfigurableLogPattern and simplePattern must result on the expected log pattern")
58     {
59         // given("We create a ExtendedFileLogger with a ConfigurableLogPattern")
60         auto file = File(outputFilePath(), "w"); // Overwrite file
61         auto logger = new ExtendedFileLogger(file, LogLevel.all, new ConfigurableLogPattern(simplePattern));
62 
63         // when("we output some entries to the logger")
64         logger.log("Log");
65         logger.trace("Trace");
66         logger.info("Info");
67         logger.warning("Warning");
68         logger.error("Error");
69         logger.critical("Critical");
70         //logger.fatal("fatal");
71 
72         // then("the output file must containt the same number of lines that log events")
73         file.close();
74         auto output = File(outputFilePath(), "r").byLineCopy.array;
75         output.should.have.length(6);
76 
77         // and("each line must have the expected format")
78         output[0].should.match(`[0-9T\-:.]+ all     : Log`);
79         output[1].should.match(`[0-9T\-:.]+ trace   : Trace`);
80         output[2].should.match(`[0-9T\-:.]+ info    : Info`);
81         output[3].should.match(`[0-9T\-:.]+ warning : Warning`);
82         output[4].should.match(`[0-9T\-:.]+ error   : Error`);
83         output[5].should.match(`[0-9T\-:.]+ critical: Critical`);
84     }
85 
86     // describe("logging with ConfigurableLogPattern and custum pattern with all options must result on the expected log pattern")
87     {
88         // given("We create a ExtendedFileLogger with a ConfigurableLogPattern")
89         const string pattern = "%d %2.2p %m %r [%t] ";
90         auto file = File(outputFilePath(), "w"); // Overwrite file
91         auto logger = new ExtendedFileLogger(file, LogLevel.all, new ConfigurableLogPattern(pattern));
92 
93         // when("we output some entries to the logger")
94         logger.log("Log");
95         logger.trace("Trace");
96         logger.info("Info");
97         logger.warning("Warning");
98         logger.error("Error");
99         logger.critical("Critical");
100         //logger.fatal("fatal");
101 
102         // then("the output file must containt the same number of lines that log events")
103         file.close();
104         auto output = File(outputFilePath(), "r").byLineCopy.array;
105         output.should.have.length(6);
106 
107         // and("each line must have the expected format")
108         output[0].should.match(`[0-9T\-:.]+ al logger.extendedfilelogger_spec [0-9]+ \[Tid\([0-9a-f]+\)\] Log`);
109         output[1].should.match(`[0-9T\-:.]+ tr logger.extendedfilelogger_spec [0-9]+ \[Tid\([0-9a-f]+\)\] Trace`);
110         output[2].should.match(`[0-9T\-:.]+ in logger.extendedfilelogger_spec [0-9]+ \[Tid\([0-9a-f]+\)\] Info`);
111         output[3].should.match(`[0-9T\-:.]+ wa logger.extendedfilelogger_spec [0-9]+ \[Tid\([0-9a-f]+\)\] Warning`);
112         output[4].should.match(`[0-9T\-:.]+ er logger.extendedfilelogger_spec [0-9]+ \[Tid\([0-9a-f]+\)\] Error`);
113         output[5].should.match(`[0-9T\-:.]+ cr logger.extendedfilelogger_spec [0-9]+ \[Tid\([0-9a-f]+\)\] Critical`);
114     }
115 }
116