azify-logger 1.0.53 → 1.0.54-test-1

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.
Files changed (3) hide show
  1. package/package.json +4 -3
  2. package/register.js +47 -18
  3. package/server.js +56 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.53",
3
+ "version": "1.0.54-test-1",
4
4
  "description": "Azify Logger Client - Centralized logging for OpenSearch",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -50,7 +50,7 @@
50
50
  "@opentelemetry/sdk-trace-base": "1.25.0",
51
51
  "@opentelemetry/semantic-conventions": "1.25.0",
52
52
  "adm-zip": "^0.5.16",
53
- "archiver": "^6.0.1",
53
+ "archiver": "^4.0.2",
54
54
  "axios": "^1.7.9",
55
55
  "bull": "^4.16.5",
56
56
  "cors": "^2.8.5",
@@ -77,7 +77,8 @@
77
77
  "ioredis": "^5.8.2"
78
78
  },
79
79
  "overrides": {
80
- "semver": "^7.5.2"
80
+ "semver": "^7.5.2",
81
+ "glob": "^13.0.0"
81
82
  },
82
83
  "engines": {
83
84
  "node": ">=20 <=22"
package/register.js CHANGED
@@ -10,7 +10,12 @@ try {
10
10
  }
11
11
  const ModuleForRequire = require('module')
12
12
  const nodeRequireOriginal = ModuleForRequire.prototype.require
13
- const bunyan = require('bunyan')
13
+ let bunyan = null
14
+ try {
15
+ bunyan = require('bunyan')
16
+ } catch (_) {
17
+ /* bunyan é opcional; sem ele o patch Bunyan não aplica, mas HTTP/Pino/axios devem continuar */
18
+ }
14
19
  const createBunyanStream = require('./streams/bunyan')
15
20
  const { createHttpLoggerTransport } = require('./streams/httpQueue')
16
21
  const { getRequestContext } = require('./store')
@@ -145,12 +150,34 @@ try {
145
150
 
146
151
  const debug = process.env.AZIFY_LOGGER_DEBUG === '1'
147
152
  const httpVerbose = process.env.AZIFY_LOGGER_HTTP_VERBOSE === '1'
153
+
154
+ function extractUrlFromReqResMessage(msgStr) {
155
+ const s = String(msgStr)
156
+ const m1 = s.match(/\[(?:REQUEST|RESPONSE)\]\s+\S+\s+(https?:\/\/\S+)/i)
157
+ if (m1) return m1[1].replace(/[)\]}>.,;]+$/, '')
158
+ const m2 = s.match(/\[(?:REQUEST|RESPONSE)\]\s+\S+\s+(\S+)/i)
159
+ if (m2 && /^https?:\/\//i.test(m2[1])) return m2[1].replace(/[)\]}>.,;]+$/, '')
160
+ return null
161
+ }
162
+
163
+ const OPAQUE_NO_URL = 'https://opaque.invalid/azify-logger-no-url'
164
+
148
165
  function sendOutboundLog(level, message, meta) {
149
166
  try {
150
167
  const msgStr = String(message)
151
168
  const isReqRes = msgStr.includes('[REQUEST]') || msgStr.includes('[RESPONSE]')
169
+ if (isReqRes && meta && typeof meta === 'object') {
170
+ const u = meta.url
171
+ const badUrl =
172
+ u === 'unknown' ||
173
+ !String(u || '').trim() ||
174
+ String(u).toLowerCase() === 'unknown'
175
+ if (badUrl) {
176
+ const fromMsg = extractUrlFromReqResMessage(msgStr)
177
+ meta = fromMsg ? { ...meta, url: fromMsg } : { ...meta, url: OPAQUE_NO_URL }
178
+ }
179
+ }
152
180
  if (isReqRes && meta && meta.url && isLoggerApiCall({ url: meta.url })) return
153
- if (isReqRes && meta && (meta.url === 'unknown' || !String(meta.url || '').trim() || String(meta.url).toLowerCase() === 'unknown')) return
154
181
  const source = meta && meta.__source
155
182
  if (debug && source === 'pino-stdout') {
156
183
  if (debug) try { process.stderr.write('[azify-logger] SENDOUTBOUND_ENTER level=' + level + ' source=' + source + ' msg=' + msgStr.slice(0, 60) + '\n') } catch (_) {}
@@ -363,22 +390,24 @@ try {
363
390
  return false
364
391
  }
365
392
 
366
- const originalCreate = bunyan.createLogger
367
- bunyan.createLogger = function patchedCreateLogger(options) {
368
- const logger = originalCreate.call(bunyan, options)
369
- try {
370
- const level = process.env.AZIFY_LOG_LEVEL || (options && options.level) || 'info'
371
- const loggerUrl = process.env.AZIFY_LOGGER_URL
372
- const serviceName = process.env.APP_NAME || (options && options.name)
373
- const environment = process.env.NODE_ENV
374
-
375
- logger.addStream({
376
- level,
377
- type: 'raw',
378
- stream: createBunyanStream({ loggerUrl, serviceName, environment })
379
- })
380
- } catch (_) {}
381
- return logger
393
+ if (bunyan && typeof bunyan.createLogger === 'function') {
394
+ const originalCreate = bunyan.createLogger
395
+ bunyan.createLogger = function patchedCreateLogger(options) {
396
+ const logger = originalCreate.call(bunyan, options)
397
+ try {
398
+ const level = process.env.AZIFY_LOG_LEVEL || (options && options.level) || 'info'
399
+ const loggerUrl = process.env.AZIFY_LOGGER_URL
400
+ const serviceName = process.env.APP_NAME || (options && options.name)
401
+ const environment = process.env.NODE_ENV
402
+
403
+ logger.addStream({
404
+ level,
405
+ type: 'raw',
406
+ stream: createBunyanStream({ loggerUrl, serviceName, environment })
407
+ })
408
+ } catch (_) {}
409
+ return logger
410
+ }
382
411
  }
383
412
 
384
413
  const getOtelTraceContext = () => {
package/server.js CHANGED
@@ -186,7 +186,11 @@ async function ensureIndexTemplate() {
186
186
  settings: {
187
187
  number_of_shards: 1,
188
188
  number_of_replicas: 0,
189
- 'index.refresh_interval': '5s'
189
+ 'index.refresh_interval': '5s',
190
+ 'index.mapping.total_fields.limit': Math.min(
191
+ Math.max(Number(process.env.AZIFY_LOGGER_OPENSEARCH_TOTAL_FIELDS_LIMIT) || 5000, 1000),
192
+ 20000
193
+ )
190
194
  },
191
195
  mappings: {
192
196
  properties: {
@@ -2053,7 +2057,7 @@ async function handleLog(req, res) {
2053
2057
 
2054
2058
  res.json({ success: true, message: 'Log enviado com sucesso', index: indexName })
2055
2059
 
2056
- const docToSend = _trimLogEntryForOpenSearch(logEntry)
2060
+ const docToSend = _normalizeLogEntryForOpenSearch(_trimLogEntryForOpenSearch(logEntry))
2057
2061
  _enqueueOpenSearchWrite(osUrl, indexName, docToSend, serviceName)
2058
2062
  }
2059
2063
 
@@ -2106,6 +2110,40 @@ function _trimLogEntryForOpenSearch(doc) {
2106
2110
  return out
2107
2111
  }
2108
2112
 
2113
+ function _normalizeLogEntryForOpenSearch(doc) {
2114
+ if (!doc || typeof doc !== 'object') return doc
2115
+ function coerceToString(v) {
2116
+ if (v == null) return v
2117
+ if (typeof v === 'string') return v
2118
+ if (Buffer.isBuffer(v)) return v.toString('utf8')
2119
+ if (typeof v === 'object') {
2120
+ try {
2121
+ return JSON.stringify(v)
2122
+ } catch (_) {
2123
+ return String(v)
2124
+ }
2125
+ }
2126
+ return String(v)
2127
+ }
2128
+ function normalizeNested(obj) {
2129
+ if (!obj || typeof obj !== 'object' || Buffer.isBuffer(obj)) return obj
2130
+ const o = { ...obj }
2131
+ for (const k of ['responseBody', 'requestBody']) {
2132
+ if (o[k] != null && typeof o[k] !== 'string') o[k] = coerceToString(o[k])
2133
+ }
2134
+ if (o.body != null && typeof o.body === 'object' && !Buffer.isBuffer(o.body)) {
2135
+ o.bodyJson = coerceToString(o.body)
2136
+ delete o.body
2137
+ }
2138
+ return o
2139
+ }
2140
+ const out = normalizeNested(doc)
2141
+ if (out.meta != null && typeof out.meta === 'object') {
2142
+ out.meta = normalizeNested(out.meta)
2143
+ }
2144
+ return out
2145
+ }
2146
+
2109
2147
  const _openSearchWriteQueue = []
2110
2148
  let _openSearchWriteInFlight = 0
2111
2149
  const _openSearchWriteMaxConcurrent = 10
@@ -2293,14 +2331,22 @@ function _drainOpenSearchQueue() {
2293
2331
  if (_openSearchCircuitRetryTimer.unref) _openSearchCircuitRetryTimer.unref()
2294
2332
  }
2295
2333
  }
2296
- if (_openSearchFailuresInRow <= 2) {
2297
- const status = error?.response?.status
2298
- const errorMsg = error?.response?.data?.error?.reason ||
2299
- error?.response?.data?.message ||
2300
- error?.message ||
2301
- 'Erro desconhecido'
2302
- console.error('❌ Falha ao enviar log para OpenSearch', { status, message: errorMsg })
2303
- if (error?.code) console.error('⚙️ Código de erro:', error.code)
2334
+ const status = error?.response?.status
2335
+ const rawMsg = error?.response?.data?.error?.reason ||
2336
+ error?.response?.data?.error?.type ||
2337
+ error?.response?.data?.message ||
2338
+ error?.message ||
2339
+ 'Erro desconhecido'
2340
+ const errorMsg = typeof rawMsg === 'string' ? rawMsg.slice(0, 800) : String(rawMsg).slice(0, 800)
2341
+ const logPayload = {
2342
+ index: job.indexName,
2343
+ serviceName: job.serviceName,
2344
+ status,
2345
+ message: errorMsg
2346
+ }
2347
+ console.error('❌ Falha ao enviar log para OpenSearch', logPayload)
2348
+ if (error?.code && _openSearchFailuresInRow <= 10) {
2349
+ console.error('⚙️ Código de erro:', error.code)
2304
2350
  }
2305
2351
  })
2306
2352
  .finally(() => {