@ecdt/logger 1.0.0

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.
@@ -0,0 +1,27 @@
1
+ const express = require("express");
2
+ const { createLogger } = require("../src");
3
+
4
+ const app = express();
5
+
6
+ app.use(
7
+ createLogger({
8
+ format: 'json',
9
+ extraFields: {
10
+ service: "ecdt-logger-example",
11
+ },
12
+ })
13
+ );
14
+
15
+ app.get("/health", (_req, res) => {
16
+ res.status(200).json({ ok: true });
17
+ });
18
+
19
+ app.get("/", (_req, res) => {
20
+ res.status(200).send("hello");
21
+ });
22
+
23
+ const port = Number(process.env.PORT || 7878);
24
+ app.listen(port, () => {
25
+ console.log(`Example server listening on http://localhost:${port}`);
26
+ });
27
+
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@ecdt/logger",
3
+ "version": "1.0.0",
4
+ "description": "Logger padronização de microsserviços da Econodata",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "example:express": "node examples/express.js"
9
+ },
10
+ "author": "",
11
+ "license": "ISC",
12
+ "engines": {
13
+ "npm": ">=10.9.2",
14
+ "node": ">=22.17.0"
15
+ },
16
+ "dependencies": {
17
+ "morgan": "^1.10.1"
18
+ },
19
+ "devDependencies": {
20
+ "express": "^4.21.2"
21
+ }
22
+ }
@@ -0,0 +1,3 @@
1
+ const formatsSupported = ['json', 'string']
2
+
3
+ module.exports = { formatsSupported }
package/src/index.js ADDED
@@ -0,0 +1,41 @@
1
+ var { formatsSupported } = require('./constants')
2
+ var morgan = require('morgan')
3
+
4
+ function createLogger(options = {}) {
5
+ return morgan((tokens, req, res) => {
6
+ return format(tokens, req, res, options);
7
+
8
+ });
9
+ }
10
+
11
+ function format(tokens, req, res, options = {}){
12
+ if(!options || !options.format) options.format == 'json'
13
+
14
+ const format = (options.format).toLowerCase()
15
+ if(!formatsSupported.includes(format)){
16
+ return `*!* Formato informado "[${format}]" não é suportado.\n*!*Formatos suportados: ${formatsSupported.join(' , ')} `
17
+ }
18
+
19
+ const payload = addPayload(tokens.method(req,res), req)
20
+
21
+ switch (format) {
22
+ case 'json':
23
+ return JSON.stringify({
24
+ method: tokens.method(req, res),
25
+ url: tokens.url(req, res),
26
+ status: tokens.status(req, res),
27
+ responseTime: tokens['response-time'](req, res) + ' ms',
28
+ payload: payload,
29
+ ...options.extraFields
30
+ });
31
+ }
32
+ }
33
+
34
+ function addPayload(log, method, req) {
35
+ switch (method) {
36
+ case "POST":
37
+ return req.body || {};
38
+ }
39
+ }
40
+
41
+ module.exports = { createLogger };