micronodelib 1.0.1 → 1.0.2
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 +41 -0
- package/dist/logging/logger.js +9 -10
- package/dist/logging/middleware.d.ts +1 -1
- package/dist/logging/middleware.js +19 -19
- package/package.json +2 -2
package/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Micronodelib
|
2
|
+
|
3
|
+
Common core for NodeJs project.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Request context injected
|
8
|
+
- Http request log
|
9
|
+
- Standard logging format
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
```shell
|
14
|
+
npm install micronodelib
|
15
|
+
|
16
|
+
# Or
|
17
|
+
|
18
|
+
yarn add micronodelib
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```shell
|
24
|
+
import express, {Application} from "express";
|
25
|
+
import {Logger, httpContextMiddleware, requestLogMiddleware} from "micronodelib";
|
26
|
+
const app: Application = express();
|
27
|
+
|
28
|
+
# Enable request context
|
29
|
+
app.use(httpContextMiddleware);
|
30
|
+
|
31
|
+
# Enable http request log
|
32
|
+
app.use(requestLogMiddleware({
|
33
|
+
# Ignore request log for specific routes
|
34
|
+
ignoreRoutes: ["/actuator/health"]
|
35
|
+
}));
|
36
|
+
|
37
|
+
app.listen(3000, () => {
|
38
|
+
# Using standard log level: debug, info, warn, error
|
39
|
+
Logger.info('App started');
|
40
|
+
})
|
41
|
+
```
|
package/dist/logging/logger.js
CHANGED
@@ -35,17 +35,16 @@ const util = __importStar(require("util"));
|
|
35
35
|
// and used to log messages.
|
36
36
|
const winstonLogger = winston_1.default.createLogger({
|
37
37
|
format: (0, configuration_1.formatter)(info => {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
return {
|
39
|
+
level: info.level.toUpperCase(),
|
40
|
+
msg: info.message,
|
41
|
+
message: null,
|
42
|
+
ts: Date.parse(info.timestamp) / 1000,
|
43
|
+
request_meta: {
|
44
|
+
request_id: (0, express_http_context_1.get)('reqId'),
|
45
|
+
},
|
46
|
+
stacktrace: info.stack
|
43
47
|
};
|
44
|
-
info.stacktrace = info.stack;
|
45
|
-
info.message = "";
|
46
|
-
delete info.timestamp;
|
47
|
-
delete info.stack;
|
48
|
-
return info;
|
49
48
|
}),
|
50
49
|
transports: configuration_1.transports,
|
51
50
|
});
|
@@ -2,4 +2,4 @@ import { Handler } from "express";
|
|
2
2
|
export interface RequestLogOptions {
|
3
3
|
ignoreRoutes: string[];
|
4
4
|
}
|
5
|
-
export declare const requestLogMiddleware: (opt
|
5
|
+
export declare const requestLogMiddleware: (opt?: RequestLogOptions | undefined) => Handler;
|
@@ -13,29 +13,29 @@ const requestLogMiddleware = function (opt) {
|
|
13
13
|
msg: 'finish router',
|
14
14
|
format: (0, configuration_1.formatter)(info => {
|
15
15
|
const req = info.meta.req;
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
16
|
+
return {
|
17
|
+
level: info.level.toUpperCase(),
|
18
|
+
msg: info.message,
|
19
|
+
message: null,
|
20
|
+
ts: Date.parse(info.timestamp) / 1000,
|
21
|
+
request_meta: {
|
22
|
+
status: info.meta.res.statusCode,
|
23
|
+
execution_time: info.meta.responseTime,
|
24
|
+
request_pattern: url_1.default.parse(req.url).pathname,
|
25
|
+
request_path: url_1.default.parse(req.url).pathname,
|
26
|
+
request_method: req.method,
|
27
|
+
query: new URLSearchParams(req.query).toString(),
|
28
|
+
url: req.url,
|
29
|
+
device_id: req.headers['device-id'],
|
30
|
+
request_id: req.headers['x-request-id'],
|
31
|
+
client_ip: req.headers['x-forwarded-for'] || (req.connection ? req.connection.remoteAddress : null),
|
32
|
+
user_agent: req.headers['user-agent']
|
33
|
+
}
|
30
34
|
};
|
31
|
-
info.message = "";
|
32
|
-
delete info.timestamp;
|
33
|
-
delete info.meta;
|
34
|
-
return info;
|
35
35
|
}),
|
36
36
|
transports: configuration_1.transports,
|
37
37
|
ignoreRoute: function (req, res) {
|
38
|
-
return opt.ignoreRoutes.length > 0 && opt.ignoreRoutes.indexOf(req.url) >= 0;
|
38
|
+
return opt !== undefined && opt.ignoreRoutes.length > 0 && opt.ignoreRoutes.indexOf(req.url) >= 0;
|
39
39
|
}
|
40
40
|
});
|
41
41
|
};
|
package/package.json
CHANGED