azify-logger 1.0.5 → 1.0.9
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/README.md +13 -13
- package/index.d.ts +86 -0
- package/index.js +61 -0
- package/middleware-express.js +29 -0
- package/package.json +3 -1
- package/server.js +5 -0
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Sistema de logging centralizado com OpenTelemetry e OpenSearch para múltiplas a
|
|
|
8
8
|
|
|
9
9
|
| Ambiente | URL |
|
|
10
10
|
|----------|-------------------------------------------------------|
|
|
11
|
-
| **Development** | `http://localhost:3001`
|
|
11
|
+
| **Development** | `http://localhost:3001/log` |
|
|
12
12
|
| **Staging** | `https://logsdashboard.azify.dev/send` |
|
|
13
13
|
| **Production** | `https://logsdashboard.azify.prd/send` (a configurar) |
|
|
14
14
|
|
|
@@ -82,20 +82,20 @@ const app = express()
|
|
|
82
82
|
|
|
83
83
|
## ⚙️ Variáveis de Ambiente
|
|
84
84
|
|
|
85
|
-
| Variável | Padrão
|
|
86
|
-
|
|
87
|
-
| `APP_NAME` | -
|
|
88
|
-
| `AZIFY_LOGGER_URL` | `http://localhost:3001`
|
|
85
|
+
| Variável | Padrão | Descrição |
|
|
86
|
+
|----------|-----------------------------------|-----------|
|
|
87
|
+
| `APP_NAME` | - | Nome da aplicação |
|
|
88
|
+
| `AZIFY_LOGGER_URL` | `http://localhost:3001/log` | URL do logger |
|
|
89
89
|
| `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318/v1/traces` | OTLP endpoint |
|
|
90
|
-
| `NODE_ENV` | `development`
|
|
91
|
-
| `AZIFY_LOGGER_AUTOREG_DISABLE` | `""`
|
|
90
|
+
| `NODE_ENV` | `development` | Ambiente |
|
|
91
|
+
| `AZIFY_LOGGER_AUTOREG_DISABLE` | `""` | Se `"1"`, desativa auto-registro do OTEL |
|
|
92
92
|
|
|
93
93
|
### 🌐 URLs por Ambiente
|
|
94
94
|
|
|
95
95
|
**Desenvolvimento (Local):**
|
|
96
96
|
```env
|
|
97
97
|
APP_NAME=minha-app
|
|
98
|
-
AZIFY_LOGGER_URL=http://localhost:3001
|
|
98
|
+
AZIFY_LOGGER_URL=http://localhost:3001/log
|
|
99
99
|
NODE_ENV=development
|
|
100
100
|
```
|
|
101
101
|
|
|
@@ -118,7 +118,7 @@ NODE_ENV=production
|
|
|
118
118
|
Apenas se estiver usando Docker Compose com rede compartilhada:
|
|
119
119
|
|
|
120
120
|
```env
|
|
121
|
-
AZIFY_LOGGER_URL=http://azify-logger:3001
|
|
121
|
+
AZIFY_LOGGER_URL=http://azify-logger:3001/log
|
|
122
122
|
```
|
|
123
123
|
|
|
124
124
|
### Docker e Node antigos
|
|
@@ -165,7 +165,7 @@ if (createAzifyBunyanStream) {
|
|
|
165
165
|
level: 'info',
|
|
166
166
|
type: 'raw',
|
|
167
167
|
stream: createAzifyBunyanStream({
|
|
168
|
-
loggerUrl: process.env.AZIFY_LOGGER_URL || 'http://localhost:3001',
|
|
168
|
+
loggerUrl: process.env.AZIFY_LOGGER_URL || 'http://localhost:3001/log',
|
|
169
169
|
serviceName: process.env.APP_NAME || 'app'
|
|
170
170
|
})
|
|
171
171
|
})
|
|
@@ -193,7 +193,7 @@ const { streams } = require('azify-logger')
|
|
|
193
193
|
const pino = require('pino')
|
|
194
194
|
|
|
195
195
|
const logger = pino({ level: 'info' }, streams.createPinoStream({
|
|
196
|
-
loggerUrl: 'http://localhost:3001',
|
|
196
|
+
loggerUrl: 'http://localhost:3001/log',
|
|
197
197
|
serviceName: 'nome-app'
|
|
198
198
|
}))
|
|
199
199
|
|
|
@@ -207,7 +207,7 @@ const { createAzifyLogger } = require('azify-logger')
|
|
|
207
207
|
|
|
208
208
|
const logger = createAzifyLogger({
|
|
209
209
|
serviceName: 'nome-app',
|
|
210
|
-
loggerUrl: 'http://localhost:3001'
|
|
210
|
+
loggerUrl: 'http://localhost:3001/log'
|
|
211
211
|
})
|
|
212
212
|
|
|
213
213
|
logger.info('Mensagem', { userId: '123' })
|
|
@@ -232,7 +232,7 @@ Aguarde alguns minutos. Você verá:
|
|
|
232
232
|
✅ Tudo pronto!
|
|
233
233
|
📊 OpenSearch: http://localhost:9200
|
|
234
234
|
🎨 OpenSearch Dashboards: http://localhost:5601
|
|
235
|
-
📝 Logger API: http://localhost:3001
|
|
235
|
+
📝 Logger API: http://localhost:3001/log
|
|
236
236
|
🔭 OTEL Collector: http://localhost:4318
|
|
237
237
|
```
|
|
238
238
|
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AzifyLogger class for structured logging with OpenTelemetry integration
|
|
3
|
+
*/
|
|
4
|
+
export class AzifyLogger {
|
|
5
|
+
/**
|
|
6
|
+
* Creates an instance of AzifyLogger
|
|
7
|
+
* @param options - Configuration options
|
|
8
|
+
*/
|
|
9
|
+
constructor(options?: {
|
|
10
|
+
serviceName?: string;
|
|
11
|
+
loggerUrl?: string;
|
|
12
|
+
environment?: string;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sends a log entry to the azify-logger service
|
|
17
|
+
* @param level - Log level (info, error, warn, debug)
|
|
18
|
+
* @param message - Log message
|
|
19
|
+
* @param meta - Additional metadata to include in the log
|
|
20
|
+
*/
|
|
21
|
+
log(level: string, message: string, meta?: Record<string, any>): Promise<void>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Logs an info level message
|
|
25
|
+
* @param message - Log message
|
|
26
|
+
* @param meta - Additional metadata
|
|
27
|
+
*/
|
|
28
|
+
info(message: string, meta?: Record<string, any>): Promise<void>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Logs an error level message with optional error object
|
|
32
|
+
* @param message - Log message
|
|
33
|
+
* @param error - Error object to include in metadata
|
|
34
|
+
* @param meta - Additional metadata
|
|
35
|
+
*/
|
|
36
|
+
error(message: string, error?: Error | null, meta?: Record<string, any>): Promise<void>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Logs a warning level message
|
|
40
|
+
* @param message - Log message
|
|
41
|
+
* @param meta - Additional metadata
|
|
42
|
+
*/
|
|
43
|
+
warn(message: string, meta?: Record<string, any>): Promise<void>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Logs a debug level message
|
|
47
|
+
* @param message - Log message
|
|
48
|
+
* @param meta - Additional metadata
|
|
49
|
+
*/
|
|
50
|
+
debug(message: string, meta?: Record<string, any>): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new AzifyLogger instance with custom options
|
|
55
|
+
* @param options - Configuration options
|
|
56
|
+
* @returns New AzifyLogger instance
|
|
57
|
+
*/
|
|
58
|
+
export function createAzifyLogger(options?: {
|
|
59
|
+
serviceName?: string;
|
|
60
|
+
loggerUrl?: string;
|
|
61
|
+
environment?: string;
|
|
62
|
+
}): AzifyLogger;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a new AzifyLogger instance using environment variables
|
|
66
|
+
* @returns New AzifyLogger instance configured from environment
|
|
67
|
+
*/
|
|
68
|
+
export function createAzifyLoggerFromEnv(): AzifyLogger;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Streams for different logging libraries
|
|
72
|
+
*/
|
|
73
|
+
export const streams: {
|
|
74
|
+
createBunyanStream: any;
|
|
75
|
+
createPinoStream: any;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Middleware for different frameworks
|
|
80
|
+
*/
|
|
81
|
+
export const middleware: {
|
|
82
|
+
restify: any;
|
|
83
|
+
express: () => any;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export default AzifyLogger;
|
package/index.js
CHANGED
|
@@ -8,7 +8,18 @@ if (process.env.AZIFY_LOGGER_AUTOREG_DISABLE !== '1') {
|
|
|
8
8
|
try { require('./register-restify') } catch (_) {}
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* AzifyLogger class for structured logging with OpenTelemetry integration
|
|
13
|
+
* @class
|
|
14
|
+
*/
|
|
11
15
|
class AzifyLogger {
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of AzifyLogger
|
|
18
|
+
* @param {Object} options - Configuration options
|
|
19
|
+
* @param {string} [options.serviceName='azipay'] - Name of the service
|
|
20
|
+
* @param {string} [options.loggerUrl='http://localhost:3001'] - URL of the azify-logger service
|
|
21
|
+
* @param {string} [options.environment] - Environment name (defaults to NODE_ENV or 'development')
|
|
22
|
+
*/
|
|
12
23
|
constructor(options = {}) {
|
|
13
24
|
this.options = {
|
|
14
25
|
serviceName: options.serviceName || 'azipay',
|
|
@@ -21,6 +32,13 @@ class AzifyLogger {
|
|
|
21
32
|
propagation.setGlobalPropagator(this.propagator)
|
|
22
33
|
}
|
|
23
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Sends a log entry to the azify-logger service
|
|
37
|
+
* @param {string} level - Log level (info, error, warn, debug)
|
|
38
|
+
* @param {string} message - Log message
|
|
39
|
+
* @param {Object} [meta={}] - Additional metadata to include in the log
|
|
40
|
+
* @returns {Promise<void>}
|
|
41
|
+
*/
|
|
24
42
|
async log(level, message, meta = {}) {
|
|
25
43
|
const span = trace.getSpan(context.active())
|
|
26
44
|
const spanContext = span?.spanContext()
|
|
@@ -64,10 +82,23 @@ class AzifyLogger {
|
|
|
64
82
|
}
|
|
65
83
|
}
|
|
66
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Logs an info level message
|
|
87
|
+
* @param {string} message - Log message
|
|
88
|
+
* @param {Object} [meta={}] - Additional metadata
|
|
89
|
+
* @returns {Promise<void>}
|
|
90
|
+
*/
|
|
67
91
|
info(message, meta = {}) {
|
|
68
92
|
return this.log('info', message, meta)
|
|
69
93
|
}
|
|
70
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Logs an error level message with optional error object
|
|
97
|
+
* @param {string} message - Log message
|
|
98
|
+
* @param {Error} [error=null] - Error object to include in metadata
|
|
99
|
+
* @param {Object} [meta={}] - Additional metadata
|
|
100
|
+
* @returns {Promise<void>}
|
|
101
|
+
*/
|
|
71
102
|
error(message, error = null, meta = {}) {
|
|
72
103
|
const logMeta = { ...meta }
|
|
73
104
|
if (error) {
|
|
@@ -80,19 +111,49 @@ class AzifyLogger {
|
|
|
80
111
|
return this.log('error', message, logMeta)
|
|
81
112
|
}
|
|
82
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Logs a warning level message
|
|
116
|
+
* @param {string} message - Log message
|
|
117
|
+
* @param {Object} [meta={}] - Additional metadata
|
|
118
|
+
* @returns {Promise<void>}
|
|
119
|
+
*/
|
|
83
120
|
warn(message, meta = {}) {
|
|
84
121
|
return this.log('warn', message, meta)
|
|
85
122
|
}
|
|
86
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Logs a debug level message
|
|
126
|
+
* @param {string} message - Log message
|
|
127
|
+
* @param {Object} [meta={}] - Additional metadata
|
|
128
|
+
* @returns {Promise<void>}
|
|
129
|
+
*/
|
|
87
130
|
debug(message, meta = {}) {
|
|
88
131
|
return this.log('debug', message, meta)
|
|
89
132
|
}
|
|
90
133
|
}
|
|
91
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Creates a new AzifyLogger instance with custom options
|
|
137
|
+
* @param {Object} options - Configuration options
|
|
138
|
+
* @param {string} [options.serviceName] - Name of the service
|
|
139
|
+
* @param {string} [options.loggerUrl] - URL of the azify-logger service
|
|
140
|
+
* @param {string} [options.environment] - Environment name
|
|
141
|
+
* @returns {AzifyLogger} New AzifyLogger instance
|
|
142
|
+
* @example
|
|
143
|
+
* const logger = createAzifyLogger({ serviceName: 'my-app' });
|
|
144
|
+
* logger.info('Hello world');
|
|
145
|
+
*/
|
|
92
146
|
function createAzifyLogger(options = {}) {
|
|
93
147
|
return new AzifyLogger(options)
|
|
94
148
|
}
|
|
95
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Creates a new AzifyLogger instance using environment variables
|
|
152
|
+
* @returns {AzifyLogger} New AzifyLogger instance configured from environment
|
|
153
|
+
* @example
|
|
154
|
+
* const logger = createAzifyLoggerFromEnv();
|
|
155
|
+
* logger.info('Hello world');
|
|
156
|
+
*/
|
|
96
157
|
function createAzifyLoggerFromEnv() {
|
|
97
158
|
return createAzifyLogger({
|
|
98
159
|
serviceName: process.env.APP_NAME || 'azipay',
|
package/middleware-express.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
const axios = require('axios')
|
|
2
2
|
const { als, runWithRequestContext, startRequestContext, getRequestContext } = require('./store')
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Creates an Express middleware for automatic request/response logging with azify-logger
|
|
6
|
+
* @param {Object} options - Configuration options
|
|
7
|
+
* @param {string} [options.serviceName] - Name of the service (defaults to APP_NAME env var or 'assemble')
|
|
8
|
+
* @param {string} [options.loggerUrl] - URL of the azify-logger service (defaults to AZIFY_LOGGER_URL env var or 'http://localhost:3001')
|
|
9
|
+
* @param {string} [options.environment] - Environment name (defaults to NODE_ENV env var or 'development')
|
|
10
|
+
* @returns {Function} Express middleware function
|
|
11
|
+
* @example
|
|
12
|
+
* const azifyMiddleware = require('azify-logger/middleware-express');
|
|
13
|
+
* app.use(azifyMiddleware({ serviceName: 'my-app' }));
|
|
14
|
+
*/
|
|
4
15
|
function createExpressLoggingMiddleware(options = {}) {
|
|
5
16
|
const config = {
|
|
6
17
|
serviceName: options.serviceName || process.env.APP_NAME || 'assemble',
|
|
@@ -8,6 +19,13 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
8
19
|
environment: options.environment || process.env.NODE_ENV || 'development'
|
|
9
20
|
}
|
|
10
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Sends a log entry to the azify-logger service
|
|
24
|
+
* @param {string} level - Log level (info, error, warn, debug)
|
|
25
|
+
* @param {string} message - Log message
|
|
26
|
+
* @param {Object} [meta={}] - Additional metadata to include in the log
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
11
29
|
async function sendLog(level, message, meta = {}) {
|
|
12
30
|
const logData = {
|
|
13
31
|
level,
|
|
@@ -33,6 +51,13 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
33
51
|
}
|
|
34
52
|
}
|
|
35
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Express middleware function that logs requests and responses
|
|
56
|
+
* @param {Object} req - Express request object
|
|
57
|
+
* @param {Object} res - Express response object
|
|
58
|
+
* @param {Function} next - Express next function
|
|
59
|
+
* @returns {void}
|
|
60
|
+
*/
|
|
36
61
|
return function azifyExpressLoggingMiddleware(req, res, next) {
|
|
37
62
|
const startTime = Date.now()
|
|
38
63
|
const requestId = req.requestId || require('uuid').v4()
|
|
@@ -206,6 +231,10 @@ function createExpressLoggingMiddleware(options = {}) {
|
|
|
206
231
|
originalEnd.call(this, chunk, encoding)
|
|
207
232
|
}
|
|
208
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Logs the response data to azify-logger
|
|
236
|
+
* @private
|
|
237
|
+
*/
|
|
209
238
|
function logResponse() {
|
|
210
239
|
const duration = Date.now() - startTime
|
|
211
240
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "azify-logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Azify Logger Client - Centralized logging for OpenSearch",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
6
7
|
"type": "commonjs",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"test": "echo \"No tests specified\" && exit 0",
|
|
@@ -47,6 +48,7 @@
|
|
|
47
48
|
},
|
|
48
49
|
"files": [
|
|
49
50
|
"index.js",
|
|
51
|
+
"index.d.ts",
|
|
50
52
|
"store.js",
|
|
51
53
|
"register-otel.js",
|
|
52
54
|
"register-restify.js",
|
package/server.js
CHANGED
|
@@ -32,6 +32,11 @@ const propagator = new W3CTraceContextPropagator()
|
|
|
32
32
|
|
|
33
33
|
const traceContextMap = new Map()
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Ensures the OpenSearch index exists and is properly configured
|
|
37
|
+
* @returns {Promise<void>}
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
35
40
|
async function ensureIndex() {
|
|
36
41
|
const indexName = 'logs-azify'
|
|
37
42
|
const osUrl = process.env.OPENSEARCH_URL || 'http://localhost:9200'
|