@@ -6,164 +6,175 @@ const os = require("os");
6
6
7
7
const CUSTOMER_UUID = "customer-uuid" ;
8
8
const AUTH_INFO_USER_ID = "auth-info-user-id" ;
9
- const AUTH_REMOTE_CLIENT_ID = "remote-client-id" ;
10
9
11
10
class ExpressMiddlewares {
12
-
13
- static accessLogMiddleware ( serviceName , dockerMode , customTokens = { } , accessLogOptions = { } ) {
14
-
15
- serviceName = serviceName || "unknown" ;
16
- const hostName = os . hostname ( ) ;
17
- const serviceColor = process . env . SERVICE_COLOR || "unknown" ;
18
- const errorHandler = ( request , response ) => "error" ;
19
- const optKeys = [ ] ;
20
-
21
- // Check for additional access logs
22
- if ( customTokens && typeof customTokens === "object" ) {
23
- for ( const key in customTokens ) {
24
- try {
25
- morgan . token ( key , typeof customTokens [ key ] === "function" ? customTokens [ key ] : errorHandler ) ;
26
- }
27
- catch ( err ) {
28
- morgan . token ( key , errorHandler ) ;
29
- }
30
- optKeys . push ( `\"${ key } \": \":${ key } \"` ) ;
31
- }
11
+ static accessLogMiddleware (
12
+ serviceName ,
13
+ dockerMode ,
14
+ customTokens = { } ,
15
+ accessLogOptions = { }
16
+ ) {
17
+ serviceName = serviceName || "unknown" ;
18
+ const hostName = os . hostname ( ) ;
19
+ const serviceColor = process . env . SERVICE_COLOR || "unknown" ;
20
+ const errorHandler = ( request , response ) => "error" ;
21
+ const optKeys = [ ] ;
22
+
23
+ // Check for additional access logs
24
+ if ( customTokens && typeof customTokens === "object" ) {
25
+ for ( const key in customTokens ) {
26
+ try {
27
+ morgan . token (
28
+ key ,
29
+ typeof customTokens [ key ] === "function"
30
+ ? customTokens [ key ]
31
+ : errorHandler
32
+ ) ;
33
+ } catch ( err ) {
34
+ morgan . token ( key , errorHandler ) ;
32
35
}
33
-
34
- morgan . token ( "host_name" , function getHostName ( request , response ) {
35
- return hostName ;
36
- } ) ;
37
-
38
- morgan . token ( "service_name" , function getHostName ( request , response ) {
39
- return serviceName ;
40
- } ) ;
41
-
42
- morgan . token ( "uri" , function getUri ( request , response ) {
43
- try {
44
- return request . _parsedUrl . pathname ;
45
- } catch ( e ) {
46
- return "error" ;
47
- }
48
- } ) ;
49
-
50
- morgan . token ( "query_string" , function getQueryString ( request ) {
51
- try {
52
- return request . _parsedUrl . query ;
53
- } catch ( e ) {
54
- return "error" ;
55
- }
56
- } ) ;
57
-
58
- morgan . token ( "protocol" , function getProtocol ( request , response ) {
59
- try {
60
- return request . secure ? "HTTPS" : "HTTP" ;
61
- } catch ( e ) {
62
- return "error" ;
63
- }
64
- } ) ;
65
-
66
- morgan . token ( "server_name" , function getServerName ( request , response ) {
67
- try {
68
- return request . headers . host ? request . headers . host : "unknown" ;
69
- } catch ( e ) {
70
- return "error" ;
71
- }
72
- } ) ;
73
-
74
- morgan . token ( "service_color" , function getServiceColor ( request , response ) {
75
- return serviceColor ;
76
- } ) ;
77
-
78
- morgan . token ( "remote_client_id" , function getRemoteClientId ( request , response ) {
79
-
80
- let rcId = "" ;
81
-
82
- try {
83
- rcId = request . headers . customeruuid ;
84
-
85
- if ( request . headers [ CUSTOMER_UUID ] ) {
86
- rcId = request . headers [ CUSTOMER_UUID ] ;
87
- }
88
-
89
- if ( request . headers [ AUTH_INFO_USER_ID ] ) {
90
- rcId = request . headers [ AUTH_INFO_USER_ID ] ;
91
- }
92
-
93
- if ( request . headers [ AUTH_REMOTE_CLIENT_ID ] )
94
- {
95
- rcId = request . headers [ AUTH_REMOTE_CLIENT_ID ] ;
96
- }
97
-
98
- if ( ! rcId ) {
99
- rcId = "unknown" ;
100
- }
101
-
102
- } catch ( e ) {
103
- rcId = "error" ;
104
- }
105
-
106
- return rcId ;
107
- } ) ;
108
-
109
- morgan . token ( "bytes_received" , function getBytesReceived ( request , response ) {
110
- try {
111
-
112
- if ( typeof request . body === "string" ) {
113
- return Buffer . byteLength ( request . body , "utf-8" ) . toString ( ) ;
114
- }
115
-
116
- return "0" ;
117
-
118
- } catch ( e ) {
119
- return "error" ;
120
- }
121
- } ) ;
122
-
123
- morgan . token ( "user_agent" , function getUserAgent ( request , response ) {
124
- try {
125
- const userAgent = request . headers [ "user-agent" ] ;
126
- if ( typeof userAgent === "string" ) {
127
- return userAgent ;
128
- }
129
- if ( Array . isArray ( userAgent ) && userAgent . length > 0 ) {
130
- return userAgent [ 0 ] ;
131
- }
132
- return "" ;
133
- } catch ( e ) {
134
- return "error" ;
135
- }
136
- } ) ;
137
-
138
- return morgan (
139
- "{ \"@timestamp\": \":date[iso]\", \"host\": \":host_name\", \"loglevel\": \"INFO\"," +
140
- " \"correlation-id\": \":req[correlation-id]\", \"application_type\": \"service\", \"log_type\": \"access\"," +
141
- " \"service\": \":service_name\", \"remote_address\": \":remote-addr\", \"status\": :status," +
142
- " \"request_method\": \":method\", \"uri\": \":uri\", \"query_string\": \":query_string\"," +
143
- " \"response_time\": :response-time, \"protocol\": \":protocol\", \"server_name\": \":server_name\"," +
144
- " \"current_color\": \":service_color\", \"remote_client_id\": \":remote_client_id\"," +
145
- " \"user_agent\": \":user_agent\", " +
146
- `${ optKeys . length ? optKeys . join ( ", " ) + "," : "" } ` + " \"bytes_received\": \":bytes_received\" }" ,
147
- accessLogOptions ) ;
36
+ optKeys . push ( `\"${ key } \": \":${ key } \"` ) ;
37
+ }
148
38
}
149
39
150
- accessLogMiddlewareFile ( filePath , dockerMode ) {
40
+ morgan . token ( "host_name" , function getHostName ( request , response ) {
41
+ return hostName ;
42
+ } ) ;
43
+
44
+ morgan . token ( "service_name" , function getHostName ( request , response ) {
45
+ return serviceName ;
46
+ } ) ;
47
+
48
+ morgan . token ( "uri" , function getUri ( request , response ) {
49
+ try {
50
+ return request . _parsedUrl . pathname ;
51
+ } catch ( e ) {
52
+ return "error" ;
53
+ }
54
+ } ) ;
55
+
56
+ morgan . token ( "query_string" , function getQueryString ( request ) {
57
+ try {
58
+ return request . _parsedUrl . query ;
59
+ } catch ( e ) {
60
+ return "error" ;
61
+ }
62
+ } ) ;
63
+
64
+ morgan . token ( "protocol" , function getProtocol ( request , response ) {
65
+ try {
66
+ return request . secure ? "HTTPS" : "HTTP" ;
67
+ } catch ( e ) {
68
+ return "error" ;
69
+ }
70
+ } ) ;
71
+
72
+ morgan . token ( "server_name" , function getServerName ( request , response ) {
73
+ try {
74
+ return request . headers . host ? request . headers . host : "unknown" ;
75
+ } catch ( e ) {
76
+ return "error" ;
77
+ }
78
+ } ) ;
79
+
80
+ morgan . token ( "service_color" , function getServiceColor ( request , response ) {
81
+ return serviceColor ;
82
+ } ) ;
83
+
84
+ morgan . token ( "remote_client_id" , function getRemoteClientId (
85
+ request ,
86
+ response
87
+ ) {
88
+ let rcId = "" ;
89
+
90
+ try {
91
+ rcId = request . headers . customeruuid ;
92
+
93
+ if ( request . headers [ CUSTOMER_UUID ] ) {
94
+ rcId = request . headers [ CUSTOMER_UUID ] ;
95
+ }
151
96
152
- morgan . token ( "uri" , function getUri ( request , response ) {
153
- return request . _parsedUrl . pathname ;
154
- } ) ;
97
+ if ( request . headers [ AUTH_INFO_USER_ID ] ) {
98
+ rcId = request . headers [ AUTH_INFO_USER_ID ] ;
99
+ }
155
100
156
- morgan . token ( "query_string" , function getQueryString ( request ) {
157
- return request . _parsedUrl . query ;
158
- } ) ;
101
+ if ( response . locals . remoteClientId ) {
102
+ rcId = response . locals . remoteClientId ;
103
+ }
159
104
160
- const accessLogStream = fs . createWriteStream ( filePath , { flags : 'a' } ) ;
161
- const hostName = os . hostname ( ) ;
105
+ if ( ! rcId ) {
106
+ rcId = "unknown" ;
107
+ }
108
+ } catch ( e ) {
109
+ rcId = "error" ;
110
+ }
111
+
112
+ return rcId ;
113
+ } ) ;
114
+
115
+ morgan . token ( "bytes_received" , function getBytesReceived (
116
+ request ,
117
+ response
118
+ ) {
119
+ try {
120
+ if ( typeof request . body === "string" ) {
121
+ return Buffer . byteLength ( request . body , "utf-8" ) . toString ( ) ;
122
+ }
162
123
163
- return morgan (
164
- "{ \"@timestamp\": \":date[iso]\", \"host\": \"" + hostName + "\", \"loglevel\": \"INFO\", \"correlationId\": \":req[correlation-id]\", \"application_type\": \"service\", \"log_type\": \"access\", \"remote_address\": \":remote-addr\", \"status\": :status, \"request_method\": \":method\", \"uri\": \":uri\", \"query_string\": \":query_string\", \"response_time\": :response-time }" ,
165
- { stream : accessLogStream } ) ;
166
- }
167
- } ;
124
+ return "0" ;
125
+ } catch ( e ) {
126
+ return "error" ;
127
+ }
128
+ } ) ;
129
+
130
+ morgan . token ( "user_agent" , function getUserAgent ( request , response ) {
131
+ try {
132
+ const userAgent = request . headers [ "user-agent" ] ;
133
+ if ( typeof userAgent === "string" ) {
134
+ return userAgent ;
135
+ }
136
+ if ( Array . isArray ( userAgent ) && userAgent . length > 0 ) {
137
+ return userAgent [ 0 ] ;
138
+ }
139
+ return "" ;
140
+ } catch ( e ) {
141
+ return "error" ;
142
+ }
143
+ } ) ;
144
+
145
+ return morgan (
146
+ '{ "@timestamp": ":date[iso]", "host": ":host_name", "loglevel": "INFO",' +
147
+ ' "correlation-id": ":req[correlation-id]", "application_type": "service", "log_type": "access",' +
148
+ ' "service": ":service_name", "remote_address": ":remote-addr", "status": :status,' +
149
+ ' "request_method": ":method", "uri": ":uri", "query_string": ":query_string",' +
150
+ ' "response_time": :response-time, "protocol": ":protocol", "server_name": ":server_name",' +
151
+ ' "current_color": ":service_color", "remote_client_id": ":remote_client_id",' +
152
+ ' "user_agent": ":user_agent", ' +
153
+ `${ optKeys . length ? optKeys . join ( ", " ) + "," : "" } ` +
154
+ ' "bytes_received": ":bytes_received" }' ,
155
+ accessLogOptions
156
+ ) ;
157
+ }
158
+
159
+ accessLogMiddlewareFile ( filePath , dockerMode ) {
160
+ morgan . token ( "uri" , function getUri ( request , response ) {
161
+ return request . _parsedUrl . pathname ;
162
+ } ) ;
163
+
164
+ morgan . token ( "query_string" , function getQueryString ( request ) {
165
+ return request . _parsedUrl . query ;
166
+ } ) ;
167
+
168
+ const accessLogStream = fs . createWriteStream ( filePath , { flags : "a" } ) ;
169
+ const hostName = os . hostname ( ) ;
170
+
171
+ return morgan (
172
+ '{ "@timestamp": ":date[iso]", "host": "' +
173
+ hostName +
174
+ '", "loglevel": "INFO", "correlationId": ":req[correlation-id]", "application_type": "service", "log_type": "access", "remote_address": ":remote-addr", "status": :status, "request_method": ":method", "uri": ":uri", "query_string": ":query_string", "response_time": :response-time }' ,
175
+ { stream : accessLogStream }
176
+ ) ;
177
+ }
178
+ }
168
179
169
180
module . exports = ExpressMiddlewares ;
0 commit comments