azify-logger 1.0.54-test → 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 (2) hide show
  1. package/package.json +1 -1
  2. package/register.js +27 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azify-logger",
3
- "version": "1.0.54-test",
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",
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')
@@ -146,7 +151,6 @@ try {
146
151
  const debug = process.env.AZIFY_LOGGER_DEBUG === '1'
147
152
  const httpVerbose = process.env.AZIFY_LOGGER_HTTP_VERBOSE === '1'
148
153
 
149
- /** Quando o patch não consegue ler a URL dos args (undici/http), meta.url fica 'unknown', mas a mensagem ainda traz a URL. */
150
154
  function extractUrlFromReqResMessage(msgStr) {
151
155
  const s = String(msgStr)
152
156
  const m1 = s.match(/\[(?:REQUEST|RESPONSE)\]\s+\S+\s+(https?:\/\/\S+)/i)
@@ -156,6 +160,8 @@ try {
156
160
  return null
157
161
  }
158
162
 
163
+ const OPAQUE_NO_URL = 'https://opaque.invalid/azify-logger-no-url'
164
+
159
165
  function sendOutboundLog(level, message, meta) {
160
166
  try {
161
167
  const msgStr = String(message)
@@ -168,13 +174,10 @@ try {
168
174
  String(u).toLowerCase() === 'unknown'
169
175
  if (badUrl) {
170
176
  const fromMsg = extractUrlFromReqResMessage(msgStr)
171
- if (fromMsg) {
172
- meta = { ...meta, url: fromMsg }
173
- }
177
+ meta = fromMsg ? { ...meta, url: fromMsg } : { ...meta, url: OPAQUE_NO_URL }
174
178
  }
175
179
  }
176
180
  if (isReqRes && meta && meta.url && isLoggerApiCall({ url: meta.url })) return
177
- if (isReqRes && meta && (meta.url === 'unknown' || !String(meta.url || '').trim() || String(meta.url).toLowerCase() === 'unknown')) return
178
181
  const source = meta && meta.__source
179
182
  if (debug && source === 'pino-stdout') {
180
183
  if (debug) try { process.stderr.write('[azify-logger] SENDOUTBOUND_ENTER level=' + level + ' source=' + source + ' msg=' + msgStr.slice(0, 60) + '\n') } catch (_) {}
@@ -387,22 +390,24 @@ try {
387
390
  return false
388
391
  }
389
392
 
390
- const originalCreate = bunyan.createLogger
391
- bunyan.createLogger = function patchedCreateLogger(options) {
392
- const logger = originalCreate.call(bunyan, options)
393
- try {
394
- const level = process.env.AZIFY_LOG_LEVEL || (options && options.level) || 'info'
395
- const loggerUrl = process.env.AZIFY_LOGGER_URL
396
- const serviceName = process.env.APP_NAME || (options && options.name)
397
- const environment = process.env.NODE_ENV
398
-
399
- logger.addStream({
400
- level,
401
- type: 'raw',
402
- stream: createBunyanStream({ loggerUrl, serviceName, environment })
403
- })
404
- } catch (_) {}
405
- 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
+ }
406
411
  }
407
412
 
408
413
  const getOtelTraceContext = () => {