@@ -17,6 +17,13 @@ class FakeConsoleTransport extends TransportStream {
17
17
}
18
18
}
19
19
20
+ let lastLogstashLog ;
21
+ class FakeLogstashTransport extends TransportStream {
22
+ log ( info ) {
23
+ lastLogstashLog = info ;
24
+ }
25
+ }
26
+
20
27
let logger ;
21
28
const symbolMessage = Symbol . for ( 'message' ) ;
22
29
const symbolLevel = Symbol . for ( 'level' ) ;
@@ -25,58 +32,95 @@ describe('gupy-logger', () => {
25
32
beforeEach ( ( ) => {
26
33
lastConsoleLog = null ;
27
34
lastSentryLog = null ;
35
+ lastLogstashLog = null ;
36
+ const loggerFactory = loggerFactoryGenerator ( {
37
+ winston,
38
+ consoleTransportClass : FakeConsoleTransport ,
39
+ sentryTransportClass : FakeSentryTransport ,
40
+ logstashTransportClass : FakeLogstashTransport ,
41
+ } ) ;
42
+ logger = loggerFactory ( {
43
+ config : { sentry : { enabled : true , dsn : 'any' , level : 'info' } ,
44
+ logstash : { enabled : true , host : 'logstashhost' , port : 12345 , level : 'info' } }
45
+ } ) ;
46
+ } ) ;
47
+
48
+ it ( 'should init without logstash by default' , ( ) => {
28
49
const loggerFactory = loggerFactoryGenerator ( {
29
50
winston,
30
51
consoleTransportClass : FakeConsoleTransport ,
31
- sentryTransportClass : FakeSentryTransport
52
+ sentryTransportClass : FakeSentryTransport ,
53
+ logstashTransportClass : undefined ,
32
54
} ) ;
55
+
33
56
logger = loggerFactory ( {
34
- config : { sentry : { enabled : true , dsn : 'any' , level : 'info' } }
57
+ config : { sentry : { enabled : true , dsn : 'any' , level : 'info' } } ,
35
58
} ) ;
59
+
60
+ expect ( logger ) . be . not . equal ( undefined ) ;
36
61
} ) ;
37
62
38
63
it ( 'should log debug nowhere' , ( ) => {
39
64
logger . debug ( 'any info' ) ;
40
65
expect ( lastSentryLog ) . to . equal ( null ) ;
41
66
expect ( lastConsoleLog ) . to . deep . equal ( null ) ;
42
67
expect ( lastSentryLog ) . to . deep . equal ( null ) ;
68
+ expect ( lastLogstashLog ) . to . deep . equal ( null ) ;
43
69
} ) ;
44
70
45
- it ( 'should log info only at console' , ( ) => {
46
- logger . info ( 'any info' ) ;
47
- expect ( lastSentryLog ) . to . equal ( null ) ;
48
- expect ( lastConsoleLog ) . to . deep . equal ( {
71
+ it ( 'should log info only at console and logstash' , ( ) => {
72
+ const expectedLog = {
49
73
level : 'info' ,
50
74
message : 'any info'
51
- } ) ;
75
+ } ;
76
+
77
+ logger . info ( 'any info' ) ;
78
+
79
+ expect ( lastSentryLog ) . to . equal ( null ) ;
80
+ expect ( lastConsoleLog ) . to . deep . equal ( expectedLog ) ;
81
+ expect ( lastLogstashLog . application ) . to . equal ( 'gupy' ) ;
82
+ expect ( lastLogstashLog . message ) . to . equal ( 'any info' ) ;
83
+ expect ( lastLogstashLog . level ) . to . equal ( 'info' ) ;
84
+
52
85
expect ( lastConsoleLog [ symbolMessage ] ) . to . match ( / \d { 4 } - \d { 2 } - \d { 2 } \d { 2 } : \d { 2 } \+ \d { 2 } : \d { 2 } \[ i n f o ] : a n y i n f o / ) ;
53
86
expect ( lastConsoleLog [ symbolLevel ] ) . to . equal ( 'info' ) ;
54
87
} ) ;
55
88
56
- it ( 'should log warn only at console' , ( ) => {
57
- logger . warn ( 'any warn' ) ;
58
- expect ( lastSentryLog ) . to . equal ( null ) ;
59
- expect ( lastConsoleLog ) . to . deep . equal ( {
89
+ it ( 'should log warn only at console and logstash' , ( ) => {
90
+ const expectedLog = {
60
91
level : 'warn' ,
61
92
message : 'any warn'
62
- } ) ;
93
+ } ;
94
+
95
+ logger . warn ( 'any warn' ) ;
96
+
97
+ expect ( lastSentryLog ) . to . equal ( null ) ;
98
+ expect ( lastConsoleLog ) . to . deep . equal ( expectedLog ) ;
99
+ expect ( lastLogstashLog . application ) . to . equal ( 'gupy' ) ;
100
+ expect ( lastLogstashLog . message ) . to . equal ( 'any warn' ) ;
101
+ expect ( lastLogstashLog . level ) . to . equal ( 'warn' ) ;
102
+
63
103
expect ( lastConsoleLog [ symbolMessage ] ) . to . match ( / \d { 4 } - \d { 2 } - \d { 2 } \d { 2 } : \d { 2 } \+ \d { 2 } : \d { 2 } \[ w a r n ] : a n y w a r n / ) ;
64
104
expect ( lastConsoleLog [ symbolLevel ] ) . to . equal ( 'warn' ) ;
65
105
} ) ;
66
106
67
- it ( 'should log error at sentry and console' , ( ) => {
68
- logger . error ( 'any error' ) ;
69
- expect ( lastSentryLog ) . to . deep . equal ( {
107
+ it ( 'should log error at all transport classes' , ( ) => {
108
+ const expectedLog = {
70
109
level : 'error' ,
71
110
message : 'any error'
72
- } ) ;
111
+ } ;
112
+
113
+ logger . error ( 'any error' ) ;
114
+
115
+ expect ( lastLogstashLog . application ) . to . equal ( 'gupy' ) ;
116
+ expect ( lastLogstashLog . level ) . to . equal ( 'error' ) ;
117
+ expect ( lastLogstashLog . message ) . to . equal ( 'any error' ) ;
118
+
119
+ expect ( lastSentryLog ) . to . deep . equal ( expectedLog ) ;
73
120
expect ( lastSentryLog [ symbolMessage ] ) . to . match ( / \d { 4 } - \d { 2 } - \d { 2 } \d { 2 } : \d { 2 } \+ \d { 2 } : \d { 2 } \[ e r r o r ] : a n y e r r o r / ) ;
74
121
expect ( lastSentryLog [ symbolLevel ] ) . to . equal ( 'error' ) ;
75
122
76
- expect ( lastConsoleLog ) . to . be . deep . equal ( {
77
- level : 'error' ,
78
- message : 'any error'
79
- } ) ;
123
+ expect ( lastConsoleLog ) . to . be . deep . equal ( expectedLog ) ;
80
124
expect ( lastConsoleLog [ symbolMessage ] ) . to . match ( / \d { 4 } - \d { 2 } - \d { 2 } \d { 2 } : \d { 2 } \+ \d { 2 } : \d { 2 } \[ e r r o r ] : a n y e r r o r / ) ;
81
125
expect ( lastConsoleLog [ symbolLevel ] ) . to . equal ( 'error' ) ;
82
126
} ) ;
0 commit comments