azify-logger 1.0.32 → 1.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
4
4
  "description": "Azify Logger Client - Centralized logging for OpenSearch",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/register.js CHANGED
@@ -84,11 +84,32 @@ try {
84
84
  return
85
85
  }
86
86
 
87
+ const metaCopy = { ...meta }
88
+ if (metaCopy.requestBody != null) {
89
+ if (typeof metaCopy.requestBody !== 'string') {
90
+ try {
91
+ metaCopy.requestBody = JSON.stringify(metaCopy.requestBody)
92
+ } catch (_) {
93
+ metaCopy.requestBody = String(metaCopy.requestBody)
94
+ }
95
+ }
96
+ }
97
+
98
+ if (metaCopy.meta && metaCopy.meta.requestBody != null) {
99
+ if (typeof metaCopy.meta.requestBody !== 'string') {
100
+ try {
101
+ metaCopy.meta.requestBody = JSON.stringify(metaCopy.meta.requestBody)
102
+ } catch (_) {
103
+ metaCopy.meta.requestBody = String(metaCopy.meta.requestBody)
104
+ }
105
+ }
106
+ }
107
+
87
108
  const payload = {
88
109
  level,
89
110
  message,
90
111
  meta: {
91
- ...meta,
112
+ ...metaCopy,
92
113
  service: {
93
114
  name: serviceName,
94
115
  version: (meta && meta.service && meta.service.version) || '1.0.0'
@@ -739,6 +760,7 @@ try {
739
760
  } catch (_) {}
740
761
 
741
762
  try {
763
+
742
764
  if (typeof globalThis.fetch === 'function') {
743
765
  const g = globalThis
744
766
  if (!g.__azifyLoggerFetchPatched) {
@@ -760,6 +782,32 @@ try {
760
782
  return originalFetch(input, init)
761
783
  }
762
784
 
785
+ let url = 'unknown'
786
+ let method = 'GET'
787
+
788
+ if (typeof input === 'string') {
789
+ url = input
790
+ method = (init?.method || 'GET').toUpperCase()
791
+ } else if (input instanceof URL) {
792
+ url = input.toString()
793
+ method = (init?.method || 'GET').toUpperCase()
794
+ } else if (typeof Request !== 'undefined' && input instanceof Request) {
795
+ url = input.url
796
+ method = input.method.toUpperCase()
797
+ } else {
798
+ try {
799
+ url = String(input)
800
+ method = (init?.method || 'GET').toUpperCase()
801
+ } catch (_) {
802
+ return originalFetch(input, init)
803
+ }
804
+ }
805
+
806
+ const testMeta = { url }
807
+ if (isLoggerApiCall(testMeta)) {
808
+ return originalFetch(input, init)
809
+ }
810
+
763
811
  let requestBody = null
764
812
  if (init && init.body != null) {
765
813
  try {
@@ -772,36 +820,30 @@ try {
772
820
  requestBody = '[FormData]'
773
821
  } else if (body instanceof URLSearchParams) {
774
822
  requestBody = body.toString()
775
- } else if (typeof body === 'object') {
823
+ } else if (typeof body === 'object' && body !== null) {
776
824
  requestBody = JSON.stringify(body)
777
825
  } else {
778
826
  requestBody = String(body)
779
827
  }
828
+ } catch (error) {
829
+ requestBody = null
830
+ }
831
+ } else if (typeof Request !== 'undefined' && input instanceof Request && input.body && !input.bodyUsed) {
832
+ try {
833
+ const cloned = input.clone()
834
+ const text = await cloned.text()
835
+ requestBody = text
780
836
  } catch (_) {
781
837
  requestBody = null
782
838
  }
783
839
  }
784
840
 
785
- const request = ensureRequest(input, init)
786
- const method = request.method.toUpperCase()
787
- const url = request.url
788
-
789
- const testMeta = { url }
790
- if (isLoggerApiCall(testMeta)) {
791
- return originalFetch(request)
792
- }
793
-
794
841
  const ctx = getRequestContext()
795
842
  const traceId = (ctx?.traceId) || randomUUID()
796
843
  const parentSpanId = (ctx?.spanId) || null
797
844
  const requestId = (ctx?.requestId) || randomUUID()
798
845
  const spanId = randomBytes(8).toString('hex')
799
846
 
800
- request.headers.set('x-trace-id', traceId)
801
- request.headers.set('x-span-id', spanId)
802
- request.headers.set('x-parent-span-id', parentSpanId || '')
803
- request.headers.set('x-request-id', requestId)
804
-
805
847
  const requestMeta = {
806
848
  traceId,
807
849
  spanId,
@@ -809,16 +851,131 @@ try {
809
851
  requestId,
810
852
  method,
811
853
  url,
812
- headers: Object.fromEntries(request.headers.entries())
854
+ headers: {}
813
855
  }
814
856
 
815
857
  if (requestBody != null) {
816
- requestMeta.requestBody = requestBody
858
+ let finalRequestBody = requestBody
859
+ if (typeof requestBody !== 'string') {
860
+ try {
861
+ finalRequestBody = JSON.stringify(requestBody)
862
+ } catch (_) {
863
+ finalRequestBody = String(requestBody)
864
+ }
865
+ }
866
+ requestMeta.requestBody = finalRequestBody
867
+ }
868
+
869
+ let request
870
+ try {
871
+ let headers
872
+ if (init?.headers instanceof Headers) {
873
+ headers = new Headers(init.headers)
874
+ } else if (init?.headers) {
875
+ headers = new Headers(init.headers)
876
+ } else {
877
+ headers = new Headers()
878
+ }
879
+
880
+ headers.set('x-trace-id', traceId)
881
+ headers.set('x-span-id', spanId)
882
+ headers.set('x-parent-span-id', parentSpanId || '')
883
+ headers.set('x-request-id', requestId)
884
+
885
+ requestMeta.headers = Object.fromEntries(headers.entries())
886
+
887
+ request = ensureRequest(input, {
888
+ ...init,
889
+ headers: headers
890
+ })
891
+ } catch (error) {
892
+ if (init?.headers) {
893
+ try {
894
+ requestMeta.headers = typeof init.headers === 'object' && !(init.headers instanceof Headers)
895
+ ? init.headers
896
+ : Object.fromEntries(new Headers(init.headers).entries())
897
+ } catch (_) {
898
+ requestMeta.headers = {}
899
+ }
900
+ }
901
+
902
+ markSource(requestMeta, 'http-client')
903
+ const hasTraceHeaders = !!(requestMeta.traceId && requestMeta.spanId)
904
+ const shouldLogRequest =
905
+ HTTP_CLIENT_MODE === 'all' ||
906
+ hasTraceHeaders
907
+
908
+ if (shouldLogRequest) {
909
+ if (hasTraceHeaders) {
910
+ const metaCopy = { ...requestMeta }
911
+ if (metaCopy.requestBody != null && typeof metaCopy.requestBody !== 'string') {
912
+ try {
913
+ metaCopy.requestBody = JSON.stringify(metaCopy.requestBody)
914
+ } catch (_) {
915
+ metaCopy.requestBody = String(metaCopy.requestBody)
916
+ }
917
+ }
918
+ const payload = {
919
+ level: 'info',
920
+ message: `[REQUEST] ${method} ${url}`,
921
+ meta: {
922
+ ...metaCopy,
923
+ service: {
924
+ name: serviceName,
925
+ version: '1.0.0'
926
+ },
927
+ environment,
928
+ timestamp: new Date().toISOString(),
929
+ hostname: os.hostname()
930
+ }
931
+ }
932
+ transport.enqueue(payload, {
933
+ 'content-type': 'application/json'
934
+ })
935
+ } else {
936
+ sendOutboundLog('info', `[REQUEST] ${method} ${url}`, requestMeta)
937
+ }
938
+ }
939
+
940
+ return originalFetch(input, init)
817
941
  }
818
942
 
819
943
  markSource(requestMeta, 'http-client')
820
- if (HTTP_CLIENT_MODE === 'all') {
821
- sendOutboundLog('info', `[REQUEST] ${method} ${url}`, requestMeta)
944
+ const hasTraceHeaders = !!(requestMeta.traceId && requestMeta.spanId)
945
+ const shouldLogRequest =
946
+ HTTP_CLIENT_MODE === 'all' ||
947
+ hasTraceHeaders
948
+
949
+ if (shouldLogRequest) {
950
+ if (hasTraceHeaders) {
951
+ const metaCopy = { ...requestMeta }
952
+ if (metaCopy.requestBody != null && typeof metaCopy.requestBody !== 'string') {
953
+ try {
954
+ metaCopy.requestBody = JSON.stringify(metaCopy.requestBody)
955
+ } catch (_) {
956
+ metaCopy.requestBody = String(metaCopy.requestBody)
957
+ }
958
+ }
959
+ const payload = {
960
+ level: 'info',
961
+ message: `[REQUEST] ${method} ${url}`,
962
+ meta: {
963
+ ...metaCopy,
964
+ service: {
965
+ name: serviceName,
966
+ version: '1.0.0'
967
+ },
968
+ environment,
969
+ timestamp: new Date().toISOString(),
970
+ hostname: os.hostname()
971
+ }
972
+ }
973
+ transport.enqueue(payload, {
974
+ 'content-type': 'application/json'
975
+ })
976
+ } else {
977
+ sendOutboundLog('info', `[REQUEST] ${method} ${url}`, requestMeta)
978
+ }
822
979
  }
823
980
 
824
981
  const childCtx = {
@@ -226,6 +226,15 @@ function sanitizePayload(payload) {
226
226
  if (sanitized.meta.responseBody) {
227
227
  sanitized.meta.responseBody = sanitizeBody(sanitized.meta.responseBody)
228
228
  }
229
+ if (sanitized.meta.requestBody != null) {
230
+ if (typeof sanitized.meta.requestBody !== 'string') {
231
+ try {
232
+ sanitized.meta.requestBody = JSON.stringify(sanitized.meta.requestBody)
233
+ } catch (_) {
234
+ sanitized.meta.requestBody = String(sanitized.meta.requestBody)
235
+ }
236
+ }
237
+ }
229
238
  }
230
239
 
231
240
  return sanitized
@@ -248,6 +257,18 @@ async function deliver(entry) {
248
257
  sanitizedPayload = entry.payload
249
258
  }
250
259
  }
260
+
261
+ if (sanitizedPayload && typeof sanitizedPayload === 'object' && sanitizedPayload.meta) {
262
+ if (sanitizedPayload.meta.requestBody != null) {
263
+ if (typeof sanitizedPayload.meta.requestBody !== 'string') {
264
+ try {
265
+ sanitizedPayload.meta.requestBody = JSON.stringify(sanitizedPayload.meta.requestBody)
266
+ } catch (_) {
267
+ sanitizedPayload.meta.requestBody = String(sanitizedPayload.meta.requestBody)
268
+ }
269
+ }
270
+ }
271
+ }
251
272
 
252
273
  await axios.post(target, sanitizedPayload, {
253
274
  headers: entry.headers || {},
@@ -325,6 +346,16 @@ async function processEntry(raw) {
325
346
  return
326
347
  }
327
348
 
349
+ if (entry && entry.payload && entry.payload.meta && entry.payload.meta.requestBody != null) {
350
+ if (typeof entry.payload.meta.requestBody !== 'string') {
351
+ try {
352
+ entry.payload.meta.requestBody = JSON.stringify(entry.payload.meta.requestBody)
353
+ } catch (_) {
354
+ entry.payload.meta.requestBody = String(entry.payload.meta.requestBody)
355
+ }
356
+ }
357
+ }
358
+
328
359
  const attempts = Number(entry.attempts || 0)
329
360
  try {
330
361
  await deliver(entry)
package/server.js CHANGED
@@ -154,6 +154,7 @@ async function ensureIndexTemplate() {
154
154
  userAgent: { type: 'text' },
155
155
  environment: { type: 'keyword' },
156
156
  hostname: { type: 'keyword' },
157
+ requestBody: { type: 'text' },
157
158
  responseBody: { type: 'text' },
158
159
  error: {
159
160
  properties: {
@@ -1691,28 +1692,16 @@ async function handleLog(req, res) {
1691
1692
  }
1692
1693
 
1693
1694
  if (typeof bodyValue === 'string') {
1694
- if (bodyValue.trim().startsWith('{') || bodyValue.trim().startsWith('[')) {
1695
- try {
1696
- let parsed = JSON.parse(bodyValue)
1697
- if (typeof parsed === 'object') {
1698
- return parsed
1699
- }
1700
- } catch (_) { }
1701
- }
1702
1695
  return bodyValue
1703
1696
  } else if (typeof bodyValue === 'object' && bodyValue !== null) {
1704
- return bodyValue
1697
+ try {
1698
+ return JSON.stringify(bodyValue)
1699
+ } catch (_) {
1700
+ return String(bodyValue)
1701
+ }
1705
1702
  } else if (Buffer.isBuffer(bodyValue)) {
1706
1703
  try {
1707
- let str = bodyValue.toString('utf8')
1708
- if (str.trim().startsWith('{') || str.trim().startsWith('[')) {
1709
- try {
1710
- return JSON.parse(str)
1711
- } catch (_) {
1712
- return str
1713
- }
1714
- }
1715
- return str
1704
+ return bodyValue.toString('utf8')
1716
1705
  } catch (_) {
1717
1706
  return '[Unable to serialize Buffer]'
1718
1707
  }