azify-logger 1.0.12 → 1.0.14
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/index.d.ts +5 -1
- package/index.js +9 -3
- package/middleware-express.d.ts +1 -0
- package/middleware-express.js +59 -0
- package/middleware-restify.js +12 -1
- package/package.json +1 -1
- package/server.js +17 -0
- package/streams/bunyan.d.ts +19 -0
- package/streams/bunyan.js +1 -1
- package/streams/pino.d.ts +19 -0
- package/streams/pino.js +1 -1
package/index.d.ts
CHANGED
|
@@ -62,8 +62,12 @@ export function createAzifyLogger(options?: {
|
|
|
62
62
|
}): AzifyLogger;
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
* Creates a new AzifyLogger instance using environment variables
|
|
65
|
+
* Creates a new AzifyLogger instance using environment variables and automatically intercepts console
|
|
66
66
|
* @returns New AzifyLogger instance configured from environment
|
|
67
|
+
* @example
|
|
68
|
+
* const logger = createAzifyLoggerFromEnv();
|
|
69
|
+
* logger.info('Hello world');
|
|
70
|
+
* console.log('This will also appear in OpenSearch!');
|
|
67
71
|
*/
|
|
68
72
|
export function createAzifyLoggerFromEnv(): AzifyLogger;
|
|
69
73
|
|
package/index.js
CHANGED
|
@@ -73,7 +73,7 @@ class AzifyLogger {
|
|
|
73
73
|
})
|
|
74
74
|
} catch (_) {}
|
|
75
75
|
|
|
76
|
-
await axios.post(`${this.options.loggerUrl}
|
|
76
|
+
await axios.post(`${this.options.loggerUrl}`, logData, {
|
|
77
77
|
timeout: 5000,
|
|
78
78
|
headers
|
|
79
79
|
})
|
|
@@ -148,18 +148,24 @@ function createAzifyLogger(options = {}) {
|
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
/**
|
|
151
|
-
* Creates a new AzifyLogger instance using environment variables
|
|
151
|
+
* Creates a new AzifyLogger instance using environment variables and automatically intercepts console
|
|
152
152
|
* @returns {AzifyLogger} New AzifyLogger instance configured from environment
|
|
153
153
|
* @example
|
|
154
154
|
* const logger = createAzifyLoggerFromEnv();
|
|
155
155
|
* logger.info('Hello world');
|
|
156
|
+
* console.log('This will also appear in OpenSearch!');
|
|
156
157
|
*/
|
|
157
158
|
function createAzifyLoggerFromEnv() {
|
|
158
|
-
|
|
159
|
+
const logger = createAzifyLogger({
|
|
159
160
|
serviceName: process.env.APP_NAME || 'azipay',
|
|
160
161
|
loggerUrl: process.env.AZIFY_LOGGER_URL || 'http://localhost:3001',
|
|
161
162
|
environment: process.env.NODE_ENV || 'development'
|
|
162
163
|
})
|
|
164
|
+
|
|
165
|
+
// Automatically intercept console methods
|
|
166
|
+
interceptConsole(logger)
|
|
167
|
+
|
|
168
|
+
return logger
|
|
163
169
|
}
|
|
164
170
|
|
|
165
171
|
/**
|
package/middleware-express.d.ts
CHANGED
package/middleware-express.js
CHANGED
|
@@ -71,6 +71,55 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
71
71
|
const requestTraceId = req.__azifyTraceId
|
|
72
72
|
const requestSpanId = req.__azifySpanId
|
|
73
73
|
const requestParentSpanId = req.__azifyParentSpanId
|
|
74
|
+
|
|
75
|
+
// Store original console methods
|
|
76
|
+
const originalConsole = {
|
|
77
|
+
log: console.log,
|
|
78
|
+
info: console.info,
|
|
79
|
+
warn: console.warn,
|
|
80
|
+
error: console.error
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Override console methods to use the request context
|
|
84
|
+
console.log = (...args) => {
|
|
85
|
+
const message = args.map(String).join(' ')
|
|
86
|
+
sendLog('info', message, {
|
|
87
|
+
traceId: requestTraceId,
|
|
88
|
+
spanId: requestSpanId,
|
|
89
|
+
parentSpanId: requestParentSpanId,
|
|
90
|
+
requestId: requestId
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
console.info = (...args) => {
|
|
95
|
+
const message = args.map(String).join(' ')
|
|
96
|
+
sendLog('info', message, {
|
|
97
|
+
traceId: requestTraceId,
|
|
98
|
+
spanId: requestSpanId,
|
|
99
|
+
parentSpanId: requestParentSpanId,
|
|
100
|
+
requestId: requestId
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
console.warn = (...args) => {
|
|
105
|
+
const message = args.map(String).join(' ')
|
|
106
|
+
sendLog('warn', message, {
|
|
107
|
+
traceId: requestTraceId,
|
|
108
|
+
spanId: requestSpanId,
|
|
109
|
+
parentSpanId: requestParentSpanId,
|
|
110
|
+
requestId: requestId
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
console.error = (...args) => {
|
|
115
|
+
const message = args.map(String).join(' ')
|
|
116
|
+
sendLog('error', message, {
|
|
117
|
+
traceId: requestTraceId,
|
|
118
|
+
spanId: requestSpanId,
|
|
119
|
+
parentSpanId: requestParentSpanId,
|
|
120
|
+
requestId: requestId
|
|
121
|
+
})
|
|
122
|
+
}
|
|
74
123
|
|
|
75
124
|
let baseUrl = req.url
|
|
76
125
|
if (baseUrl.includes('?')) {
|
|
@@ -108,6 +157,11 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
108
157
|
logResponse()
|
|
109
158
|
responseLogged = true
|
|
110
159
|
}
|
|
160
|
+
// Restore original console methods
|
|
161
|
+
console.log = originalConsole.log
|
|
162
|
+
console.info = originalConsole.info
|
|
163
|
+
console.warn = originalConsole.warn
|
|
164
|
+
console.error = originalConsole.error
|
|
111
165
|
})
|
|
112
166
|
|
|
113
167
|
res.on('close', () => {
|
|
@@ -115,6 +169,11 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
115
169
|
logResponse()
|
|
116
170
|
responseLogged = true
|
|
117
171
|
}
|
|
172
|
+
// Restore original console methods
|
|
173
|
+
console.log = originalConsole.log
|
|
174
|
+
console.info = originalConsole.info
|
|
175
|
+
console.warn = originalConsole.warn
|
|
176
|
+
console.error = originalConsole.error
|
|
118
177
|
})
|
|
119
178
|
|
|
120
179
|
let sentBody
|
package/middleware-restify.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Restify middleware for automatic request/response logging with azify-logger
|
|
3
|
+
* @param {Object} options - Configuration options
|
|
4
|
+
* @param {string} [options.serviceName] - Name of the service (defaults to APP_NAME env var or 'azipay')
|
|
5
|
+
* @param {string} [options.loggerUrl] - URL of the azify-logger service (defaults to AZIFY_LOGGER_URL env var or 'http://localhost:3001')
|
|
6
|
+
* @param {string} [options.environment] - Environment name (defaults to NODE_ENV env var or 'development')
|
|
7
|
+
* @returns {Function} Restify middleware function
|
|
8
|
+
* @example
|
|
9
|
+
* const azifyMiddleware = require('azify-logger/middleware-restify');
|
|
10
|
+
* server.use(azifyMiddleware({ serviceName: 'my-app' }));
|
|
11
|
+
*/
|
|
1
12
|
const axios = require('axios')
|
|
2
13
|
const { als, runWithRequestContext, startRequestContext, getRequestContext } = require('./store')
|
|
3
14
|
|
|
@@ -25,7 +36,7 @@ function createRestifyLoggingMiddleware(options = {}) {
|
|
|
25
36
|
}
|
|
26
37
|
|
|
27
38
|
try {
|
|
28
|
-
await axios.post(`${config.loggerUrl}
|
|
39
|
+
await axios.post(`${config.loggerUrl}`, logData, {
|
|
29
40
|
timeout: 5000
|
|
30
41
|
})
|
|
31
42
|
} catch (error) {}
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -167,6 +167,23 @@ app.post('/log', async (req, res) => {
|
|
|
167
167
|
return res.status(400).json({ success: false, message: 'Level and message are required.' })
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
// Filtrar logs verbosos do Prisma e outros logs desnecessários
|
|
171
|
+
const shouldFilterLog = (
|
|
172
|
+
message.includes('prisma:query') ||
|
|
173
|
+
message.includes('prisma:info') ||
|
|
174
|
+
message.includes('Starting a mysql pool') ||
|
|
175
|
+
message.includes('SELECT `') ||
|
|
176
|
+
message.includes('INSERT `') ||
|
|
177
|
+
message.includes('UPDATE `') ||
|
|
178
|
+
message.includes('DELETE `') ||
|
|
179
|
+
message.includes('%s: %s') ||
|
|
180
|
+
message.includes('Application Startup Time')
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
if (shouldFilterLog) {
|
|
184
|
+
return res.json({ success: true, message: 'Log filtrado (Prisma verboso)' })
|
|
185
|
+
}
|
|
186
|
+
|
|
170
187
|
const requestId = meta?.requestId
|
|
171
188
|
|
|
172
189
|
let traceContext = null
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Writable } from 'stream';
|
|
2
|
+
|
|
3
|
+
export interface BunyanStreamOptions {
|
|
4
|
+
loggerUrl?: string;
|
|
5
|
+
serviceName?: string;
|
|
6
|
+
environment?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a Bunyan stream for azify-logger
|
|
11
|
+
* @param options - Configuration options
|
|
12
|
+
* @returns Writable stream
|
|
13
|
+
* @example
|
|
14
|
+
* const bunyanStream = require('azify-logger/streams/bunyan');
|
|
15
|
+
* const stream = bunyanStream({ loggerUrl: 'http://localhost:3001' });
|
|
16
|
+
*/
|
|
17
|
+
declare function createBunyanStream(options?: BunyanStreamOptions): Writable;
|
|
18
|
+
|
|
19
|
+
export = createBunyanStream;
|
package/streams/bunyan.js
CHANGED
|
@@ -52,7 +52,7 @@ function createBunyanStream(options = {}) {
|
|
|
52
52
|
|
|
53
53
|
const payload = { level, message: record.msg || record.message || 'log', meta }
|
|
54
54
|
|
|
55
|
-
axios.post(`${loggerUrl}
|
|
55
|
+
axios.post(`${loggerUrl}`, payload, { headers, timeout: 3001 }).catch(() => {})
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Transform } from 'stream';
|
|
2
|
+
|
|
3
|
+
export interface PinoStreamOptions {
|
|
4
|
+
loggerUrl?: string;
|
|
5
|
+
serviceName?: string;
|
|
6
|
+
environment?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a Pino stream for azify-logger
|
|
11
|
+
* @param options - Configuration options
|
|
12
|
+
* @returns Transform stream
|
|
13
|
+
* @example
|
|
14
|
+
* const pinoStream = require('azify-logger/streams/pino');
|
|
15
|
+
* const stream = pinoStream({ loggerUrl: 'http://localhost:3001' });
|
|
16
|
+
*/
|
|
17
|
+
declare function createPinoStream(options?: PinoStreamOptions): Transform;
|
|
18
|
+
|
|
19
|
+
export = createPinoStream;
|
package/streams/pino.js
CHANGED
|
@@ -47,7 +47,7 @@ function createPinoStream(options = {}) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
const payload = { level, message: record.msg || record.message || 'log', meta }
|
|
50
|
-
axios.post(`${loggerUrl}
|
|
50
|
+
axios.post(`${loggerUrl}`, payload, { headers, timeout: 3001 }).catch(() => {})
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|