@@ -2,13 +2,47 @@ import { createLogger, transports, format } from 'winston';
22import DailyRotateFile from 'winston-daily-rotate-file' ;
33import { ElasticsearchTransport } from 'winston-elasticsearch' ;
44
5- const esTransport = new ElasticsearchTransport ( {
6- level : 'info' ,
7- indexPrefix : 'backend-javascript-logs' ,
8- clientOpts : {
9- node : process . env . ELASTICSEARCH_NODE || 'http://localhost:9200' ,
10- } ,
11- } ) ;
5+ const transportsArray = [ ] ;
6+
7+ if ( process . env . LOGSTASH_ENABLED === 'true' ) {
8+ transportsArray . push (
9+ new DailyRotateFile ( {
10+ filename : 'logs/logstash-%DATE%.log' ,
11+ datePattern : 'YYYY-MM-DD' ,
12+ level : 'info' ,
13+ zippedArchive : true ,
14+ maxSize : '10m' ,
15+ maxFiles : '30d' ,
16+ } ) ,
17+ ) ;
18+ } else if ( process . env . ELASTIC_ENABLED === 'true' ) {
19+ const esTransport = new ElasticsearchTransport ( {
20+ level : 'info' ,
21+ indexPrefix : 'backend-javascript-logs' ,
22+ clientOpts : {
23+ node : process . env . ELASTICSEARCH_NODE || 'http://localhost:9200' ,
24+ } ,
25+ } ) ;
26+ transportsArray . push ( esTransport ) ;
27+ }
28+
29+ transportsArray . push (
30+ new DailyRotateFile ( {
31+ filename : 'logs/error-%DATE%.log' ,
32+ datePattern : 'YYYY-MM-DD' ,
33+ level : 'error' ,
34+ zippedArchive : true ,
35+ maxSize : '5m' ,
36+ maxFiles : '120d' ,
37+ } ) ,
38+ new DailyRotateFile ( {
39+ filename : 'logs/combined-%DATE%.log' ,
40+ datePattern : 'YYYY-MM-DD' ,
41+ zippedArchive : true ,
42+ maxSize : '5m' ,
43+ maxFiles : '120d' ,
44+ } ) ,
45+ ) ;
1246
1347const logger = createLogger ( {
1448 level : process . env . LOG_LEVEL || 'warn' ,
@@ -18,24 +52,7 @@ const logger = createLogger({
1852 format . splat ( ) ,
1953 format . json ( ) ,
2054 ) ,
21- transports : [
22- esTransport ,
23- new DailyRotateFile ( {
24- filename : 'logs/error-%DATE%.log' ,
25- datePattern : 'YYYY-MM-DD' ,
26- level : 'error' ,
27- zippedArchive : true ,
28- maxSize : '5m' ,
29- maxFiles : '120d' ,
30- } ) ,
31- new DailyRotateFile ( {
32- filename : 'logs/combined-%DATE%.log' ,
33- datePattern : 'YYYY-MM-DD' ,
34- zippedArchive : true ,
35- maxSize : '5m' ,
36- maxFiles : '120d' ,
37- } ) ,
38- ] ,
55+ transports : transportsArray ,
3956} ) ;
4057
4158if ( process . env . NODE_ENV !== 'production' ) {
@@ -47,3 +64,4 @@ if (process.env.NODE_ENV !== 'production') {
4764}
4865
4966export default logger ;
67+
0 commit comments