@ecdt/logger 1.0.5 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecdt/logger",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Logger padronização de microsserviços da Econodata",
5
5
  "type": "commonjs",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -22,11 +22,33 @@ function createLogger(options = {}) {
22
22
 
23
23
  function createErrorCapture() {
24
24
  return function errorCapture(err, req, res, next) {
25
- res.error = {
25
+ const errorDetails = {
26
26
  message: err?.message,
27
27
  stack: err?.stack
28
+ };
29
+ if (err?.isAxiosError) {
30
+ const responseData = err.response?.data;
31
+ const dataSummary = typeof responseData === 'string' && responseData.length > 300
32
+ ? responseData.slice(0, 300) + '...[truncated]'
33
+ : responseData;
34
+
35
+ errorDetails.axiosError = {
36
+ config: {
37
+ method: err.config?.method,
38
+ url: err.config?.baseURL ?? err.config?.url,
39
+ headers: err.config?.headers,
40
+ },
41
+ response: {
42
+ status: err.response?.status,
43
+ statusText: err.response?.statusText,
44
+ headers: err.response?.headers,
45
+ data: dataSummary,
46
+ }
47
+ };
28
48
  }
29
- res.status(500).json({ error: 'Erro interno' });
49
+
50
+ res.error = errorDetails;
51
+ res.status(500).json({ error: res.error.message ?? 'Erro interno no servidor' });
30
52
  }
31
53
  }
32
54
 
@@ -9,14 +9,12 @@ function formatLog(tokens, req, res, options = {}){
9
9
  return `*!* Formato informado "[${format}]" não é suportado.\n*!*Formatos suportados: ${formatsSupported.join(' , ')} `
10
10
  }
11
11
 
12
- // Default infos
13
12
  const _method = tokens.method(req, res);
14
13
  const _url = tokens.url(req, res);
15
14
  const _status = tokens.status(req, res);
16
15
  const _responseTime = tokens['response-time'](req, res) + ' ms'
17
16
  const _payload = payload.getPayload(req);
18
- const _timestamp = req._startTime;
19
-
17
+
20
18
  // `extraFields` é clonado por request, então podemos anexar info do erro com segurança
21
19
  if(_status >= 500 && res.error && res.error.stack && normalized.includeStackErrors5xx) {
22
20
  normalized.extraFields.error = {
@@ -33,13 +31,12 @@ function formatLog(tokens, req, res, options = {}){
33
31
  status: _status,
34
32
  responseTime: _responseTime,
35
33
  payload: _payload,
36
- timestamp: _timestamp,
37
34
  ...normalized.extraFields
38
35
  });
39
36
  case 'string':
40
37
  const extraFieldsToString = formatExtraFields(normalized.extraFields)
41
38
 
42
- return `${_method} - ${_status} - ${_responseTime} - ${JSON.stringify(_payload)} - ${_timestamp} - ${extraFieldsToString}`;;
39
+ return `${_method} - ${_status} - ${_responseTime} - ${JSON.stringify(_payload)} - ${extraFieldsToString}`;;
43
40
  }
44
41
  }
45
42