@@ -16,60 +16,91 @@ class FakeConsoleTransport extends TransportStream {
16
16
lastConsoleLog = info ;
17
17
}
18
18
}
19
+ let lastLogstashLog ;
20
+ class FakeLogstashTransport extends TransportStream {
21
+ log ( info ) {
22
+ lastLogstashLog = info ;
23
+ }
24
+ }
19
25
let logger ;
20
26
const symbolMessage = Symbol . for ( 'message' ) ;
21
27
const symbolLevel = Symbol . for ( 'level' ) ;
22
28
describe ( 'gupy-logger' , ( ) => {
23
29
beforeEach ( ( ) => {
24
30
lastConsoleLog = null ;
25
31
lastSentryLog = null ;
32
+ lastLogstashLog = null ;
26
33
const loggerFactory = logger_factory_generator_1 . loggerFactoryGenerator ( {
27
34
winston,
28
35
consoleTransportClass : FakeConsoleTransport ,
29
- sentryTransportClass : FakeSentryTransport
36
+ sentryTransportClass : FakeSentryTransport ,
37
+ logstashTransportClass : FakeLogstashTransport ,
30
38
} ) ;
31
39
logger = loggerFactory ( {
32
- config : { sentry : { enabled : true , dsn : 'any' , level : 'info' } }
40
+ config : { sentry : { enabled : true , dsn : 'any' , level : 'info' } ,
41
+ logstash : { enabled : true , host : 'logstashhost' , port : 12345 , level : 'info' } }
33
42
} ) ;
34
43
} ) ;
44
+ it ( 'should init without logstash by default' , ( ) => {
45
+ const loggerFactory = logger_factory_generator_1 . loggerFactoryGenerator ( {
46
+ winston,
47
+ consoleTransportClass : FakeConsoleTransport ,
48
+ sentryTransportClass : FakeSentryTransport ,
49
+ logstashTransportClass : undefined ,
50
+ } ) ;
51
+ logger = loggerFactory ( {
52
+ config : { sentry : { enabled : true , dsn : 'any' , level : 'info' } } ,
53
+ } ) ;
54
+ chai_1 . expect ( logger ) . be . not . equal ( undefined ) ;
55
+ } ) ;
35
56
it ( 'should log debug nowhere' , ( ) => {
36
57
logger . debug ( 'any info' ) ;
37
58
chai_1 . expect ( lastSentryLog ) . to . equal ( null ) ;
38
59
chai_1 . expect ( lastConsoleLog ) . to . deep . equal ( null ) ;
39
60
chai_1 . expect ( lastSentryLog ) . to . deep . equal ( null ) ;
61
+ chai_1 . expect ( lastLogstashLog ) . to . deep . equal ( null ) ;
40
62
} ) ;
41
- it ( 'should log info only at console' , ( ) => {
42
- logger . info ( 'any info' ) ;
43
- chai_1 . expect ( lastSentryLog ) . to . equal ( null ) ;
44
- chai_1 . expect ( lastConsoleLog ) . to . deep . equal ( {
63
+ it ( 'should log info only at console and logstash' , ( ) => {
64
+ const expectedLog = {
45
65
level : 'info' ,
46
66
message : 'any info'
47
- } ) ;
67
+ } ;
68
+ logger . info ( 'any info' ) ;
69
+ chai_1 . expect ( lastSentryLog ) . to . equal ( null ) ;
70
+ chai_1 . expect ( lastConsoleLog ) . to . deep . equal ( expectedLog ) ;
71
+ chai_1 . expect ( lastLogstashLog . application ) . to . equal ( 'gupy' ) ;
72
+ chai_1 . expect ( lastLogstashLog . message ) . to . equal ( 'any info' ) ;
73
+ chai_1 . expect ( lastLogstashLog . level ) . to . equal ( 'info' ) ;
48
74
chai_1 . 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 / ) ;
49
75
chai_1 . expect ( lastConsoleLog [ symbolLevel ] ) . to . equal ( 'info' ) ;
50
76
} ) ;
51
- it ( 'should log warn only at console' , ( ) => {
52
- logger . warn ( 'any warn' ) ;
53
- chai_1 . expect ( lastSentryLog ) . to . equal ( null ) ;
54
- chai_1 . expect ( lastConsoleLog ) . to . deep . equal ( {
77
+ it ( 'should log warn only at console and logstash' , ( ) => {
78
+ const expectedLog = {
55
79
level : 'warn' ,
56
80
message : 'any warn'
57
- } ) ;
81
+ } ;
82
+ logger . warn ( 'any warn' ) ;
83
+ chai_1 . expect ( lastSentryLog ) . to . equal ( null ) ;
84
+ chai_1 . expect ( lastConsoleLog ) . to . deep . equal ( expectedLog ) ;
85
+ chai_1 . expect ( lastLogstashLog . application ) . to . equal ( 'gupy' ) ;
86
+ chai_1 . expect ( lastLogstashLog . message ) . to . equal ( 'any warn' ) ;
87
+ chai_1 . expect ( lastLogstashLog . level ) . to . equal ( 'warn' ) ;
58
88
chai_1 . 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 / ) ;
59
89
chai_1 . expect ( lastConsoleLog [ symbolLevel ] ) . to . equal ( 'warn' ) ;
60
90
} ) ;
61
- it ( 'should log error at sentry and console' , ( ) => {
62
- logger . error ( 'any error' ) ;
63
- chai_1 . expect ( lastSentryLog ) . to . deep . equal ( {
91
+ it ( 'should log error at all transport classes' , ( ) => {
92
+ const expectedLog = {
64
93
level : 'error' ,
65
94
message : 'any error'
66
- } ) ;
95
+ } ;
96
+ logger . error ( 'any error' ) ;
97
+ chai_1 . expect ( lastLogstashLog . application ) . to . equal ( 'gupy' ) ;
98
+ chai_1 . expect ( lastLogstashLog . level ) . to . equal ( 'error' ) ;
99
+ chai_1 . expect ( lastLogstashLog . message ) . to . equal ( 'any error' ) ;
100
+ chai_1 . expect ( lastSentryLog ) . to . deep . equal ( expectedLog ) ;
67
101
chai_1 . 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 / ) ;
68
102
chai_1 . expect ( lastSentryLog [ symbolLevel ] ) . to . equal ( 'error' ) ;
69
- chai_1 . expect ( lastConsoleLog ) . to . be . deep . equal ( {
70
- level : 'error' ,
71
- message : 'any error'
72
- } ) ;
103
+ chai_1 . expect ( lastConsoleLog ) . to . be . deep . equal ( expectedLog ) ;
73
104
chai_1 . 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 / ) ;
74
105
chai_1 . expect ( lastConsoleLog [ symbolLevel ] ) . to . equal ( 'error' ) ;
75
106
} ) ;
0 commit comments