azify-logger 1.0.33 → 1.0.34

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.
@@ -297,6 +297,15 @@ function createExpressLoggingMiddleware(options = {}) {
297
297
  timestamp: Date.now(),
298
298
  hostname
299
299
  }
300
+
301
+ if (config.captureRequestBody && req.body !== undefined && req.body != null) {
302
+ const serializedBody = safeSerializeBody(req.body)
303
+ if (typeof serializedBody === 'string') {
304
+ meta.requestBody = serializedBody
305
+ } else {
306
+ meta.requestBody = JSON.stringify(serializedBody)
307
+ }
308
+ }
300
309
  if (serviceObj) meta.service = serviceObj
301
310
  if (config.environment) meta.environment = config.environment
302
311
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
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
@@ -899,82 +899,49 @@ try {
899
899
  }
900
900
  }
901
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)
902
+ request = ensureRequest(input, init)
941
903
  }
942
904
 
943
905
  markSource(requestMeta, 'http-client')
944
906
  const hasTraceHeaders = !!(requestMeta.traceId && requestMeta.spanId)
945
907
  const shouldLogRequest =
946
908
  HTTP_CLIENT_MODE === 'all' ||
909
+ HTTP_CLIENT_MODE === 'errors' ||
947
910
  hasTraceHeaders
911
+
912
+ let requestWasLogged = false
948
913
 
949
914
  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
- }
915
+ const metaCopy = { ...requestMeta }
916
+ if (metaCopy.requestBody != null && typeof metaCopy.requestBody !== 'string') {
917
+ try {
918
+ metaCopy.requestBody = JSON.stringify(metaCopy.requestBody)
919
+ } catch (_) {
920
+ metaCopy.requestBody = String(metaCopy.requestBody)
958
921
  }
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
- }
922
+ }
923
+ const payload = {
924
+ level: 'info',
925
+ message: `[REQUEST] ${method} ${url}`,
926
+ meta: {
927
+ ...metaCopy,
928
+ service: {
929
+ name: serviceName,
930
+ version: '1.0.0'
931
+ },
932
+ environment,
933
+ timestamp: new Date().toISOString(),
934
+ hostname: os.hostname()
972
935
  }
936
+ }
937
+ try {
973
938
  transport.enqueue(payload, {
974
939
  'content-type': 'application/json'
975
940
  })
976
- } else {
941
+ requestWasLogged = true
942
+ } catch (enqueueErr) {
977
943
  sendOutboundLog('info', `[REQUEST] ${method} ${url}`, requestMeta)
944
+ requestWasLogged = true
978
945
  }
979
946
  }
980
947
 
@@ -994,34 +961,32 @@ try {
994
961
 
995
962
  const duration = Number((performance.now() - start).toFixed(2))
996
963
  const hasTraceHeaders = !!(requestMeta.traceId && requestMeta.spanId)
964
+ const wasRequestLogged = requestWasLogged || HTTP_CLIENT_MODE === 'all' || HTTP_CLIENT_MODE === 'errors' || hasTraceHeaders
997
965
  const shouldLogResponse =
998
966
  HTTP_CLIENT_MODE === 'all' ||
999
- (HTTP_CLIENT_MODE === 'errors' && response.status >= 400) ||
1000
- hasTraceHeaders
967
+ (HTTP_CLIENT_MODE === 'errors' && (response.status >= 400 || wasRequestLogged)) ||
968
+ hasTraceHeaders ||
969
+ requestWasLogged
970
+
971
+ let responseCloneForLogging = null
972
+ if (shouldLogResponse) {
973
+ try {
974
+ responseCloneForLogging = response.clone()
975
+ } catch (cloneError) {
976
+ responseCloneForLogging = null
977
+ }
978
+ }
1001
979
 
1002
980
  if (shouldLogResponse) {
1003
981
  const logResponse = async () => {
1004
982
  let responseBody = null
1005
983
  const contentType = response.headers.get('content-type') || ''
1006
984
 
1007
- try {
1008
- const clonedResponse = response.clone()
1009
- if (contentType.includes('application/json') || contentType.includes('text/')) {
1010
- const bodyText = await clonedResponse.text()
1011
- if (contentType.includes('application/json')) {
1012
- try {
1013
- responseBody = JSON.parse(bodyText)
1014
- } catch (_) {
1015
- responseBody = bodyText
1016
- }
1017
- } else {
1018
- responseBody = bodyText
1019
- }
1020
- }
1021
- } catch (cloneError) {
985
+ if (responseCloneForLogging) {
1022
986
  try {
987
+ const clonedResponse = responseCloneForLogging
1023
988
  if (contentType.includes('application/json') || contentType.includes('text/')) {
1024
- const bodyText = await response.text()
989
+ const bodyText = await clonedResponse.text()
1025
990
  if (contentType.includes('application/json')) {
1026
991
  try {
1027
992
  responseBody = JSON.parse(bodyText)
@@ -1032,9 +997,26 @@ try {
1032
997
  responseBody = bodyText
1033
998
  }
1034
999
  }
1035
- } catch (_) {
1036
- responseBody = null
1000
+ } catch (cloneError) {
1001
+ try {
1002
+ if (contentType.includes('application/json') || contentType.includes('text/')) {
1003
+ const bodyText = await response.text()
1004
+ if (contentType.includes('application/json')) {
1005
+ try {
1006
+ responseBody = JSON.parse(bodyText)
1007
+ } catch (_) {
1008
+ responseBody = bodyText
1009
+ }
1010
+ } else {
1011
+ responseBody = bodyText
1012
+ }
1013
+ }
1014
+ } catch (_) {
1015
+ responseBody = null
1016
+ }
1037
1017
  }
1018
+ } else {
1019
+ responseBody = null
1038
1020
  }
1039
1021
 
1040
1022
  try {
@@ -1050,7 +1032,37 @@ try {
1050
1032
  const message = `[RESPONSE] ${method} ${url} ${response.status} ${duration}ms`
1051
1033
 
1052
1034
  const level = response.status >= 500 ? 'error' : response.status >= 400 ? 'warn' : 'info'
1053
- sendOutboundLog(level, message, responseMeta)
1035
+
1036
+ const metaCopy = { ...responseMeta }
1037
+ if (metaCopy.responseBody != null && typeof metaCopy.responseBody !== 'string') {
1038
+ try {
1039
+ metaCopy.responseBody = JSON.stringify(metaCopy.responseBody)
1040
+ } catch (_) {
1041
+ metaCopy.responseBody = String(metaCopy.responseBody)
1042
+ }
1043
+ }
1044
+
1045
+ const payload = {
1046
+ level,
1047
+ message,
1048
+ meta: {
1049
+ ...metaCopy,
1050
+ service: {
1051
+ name: serviceName,
1052
+ version: '1.0.0'
1053
+ },
1054
+ environment,
1055
+ timestamp: new Date().toISOString(),
1056
+ hostname: os.hostname()
1057
+ }
1058
+ }
1059
+ try {
1060
+ transport.enqueue(payload, {
1061
+ 'content-type': 'application/json'
1062
+ })
1063
+ } catch (enqueueErr) {
1064
+ sendOutboundLog(level, message, responseMeta)
1065
+ }
1054
1066
  } catch (err) {
1055
1067
  try {
1056
1068
  const responseMeta = {
@@ -1058,20 +1070,67 @@ try {
1058
1070
  statusCode: response.status,
1059
1071
  responseTimeMs: duration,
1060
1072
  responseHeaders: Object.fromEntries(response.headers.entries()),
1061
- responseBody: null
1073
+ responseBody: responseBody != null ? String(responseBody) : null
1062
1074
  }
1063
1075
 
1064
1076
  markSource(responseMeta, 'http-client')
1065
1077
  const message = `[RESPONSE] ${method} ${url} ${response.status} ${duration}ms`
1066
1078
 
1067
1079
  const level = response.status >= 500 ? 'error' : response.status >= 400 ? 'warn' : 'info'
1068
- sendOutboundLog(level, message, responseMeta)
1069
- } catch (_) {
1080
+
1081
+ const metaCopy = { ...responseMeta }
1082
+ if (metaCopy.responseBody != null && typeof metaCopy.responseBody !== 'string') {
1083
+ try {
1084
+ metaCopy.responseBody = JSON.stringify(metaCopy.responseBody)
1085
+ } catch (_) {
1086
+ metaCopy.responseBody = String(metaCopy.responseBody)
1087
+ }
1088
+ }
1089
+ const payload = {
1090
+ level,
1091
+ message,
1092
+ meta: {
1093
+ ...metaCopy,
1094
+ service: {
1095
+ name: serviceName,
1096
+ version: '1.0.0'
1097
+ },
1098
+ environment,
1099
+ timestamp: new Date().toISOString(),
1100
+ hostname: os.hostname()
1101
+ }
1102
+ }
1103
+ try {
1104
+ transport.enqueue(payload, {
1105
+ 'content-type': 'application/json'
1106
+ })
1107
+ } catch (enqueueErr) {
1108
+ sendOutboundLog(level, message, responseMeta)
1109
+ }
1110
+ } catch (innerErr) {
1111
+ console.error('[azify-logger] Erro ao logar resposta (fallback):', innerErr)
1070
1112
  }
1071
1113
  }
1072
1114
  }
1073
1115
 
1074
- logResponse().catch(() => {})
1116
+ logResponse().catch((err) => {
1117
+ console.error('[azify-logger] Erro ao executar logResponse para', method, url, ':', err)
1118
+ const responseMeta = {
1119
+ ...requestMeta,
1120
+ statusCode: response.status,
1121
+ responseTimeMs: duration,
1122
+ responseHeaders: Object.fromEntries(response.headers.entries()),
1123
+ responseBody: null
1124
+ }
1125
+ markSource(responseMeta, 'http-client')
1126
+ const message = `[RESPONSE] ${method} ${url} ${response.status} ${duration}ms`
1127
+ const level = response.status >= 500 ? 'error' : response.status >= 400 ? 'warn' : 'info'
1128
+ try {
1129
+ sendOutboundLog(level, message, responseMeta)
1130
+ } catch (fallbackErr) {
1131
+ console.error('[azify-logger] Erro ao enviar log de fallback:', fallbackErr)
1132
+ }
1133
+ })
1075
1134
  }
1076
1135
 
1077
1136
  return response