Moved Axios logs in Logging
MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -359,4 +359,5 @@ export enum ServerAction {
HTTP_REQUEST = 'HttpRequest',
HTTP_RESPONSE = 'HttpResponse',
+ HTTP_ERROR = 'HttpError',
}

MODIFY
src/utils/AxiosFactory.ts
src/utils/AxiosFactory.ts
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
-import axiosRetry from 'axios-retry';
-import { ServerAction } from '../types/Server';
import Constants from './Constants';
import Logging from './Logging';
+import axiosRetry from 'axios-retry';
const MODULE_NAME = 'AxiosFactory';
@@ -28,76 +27,18 @@ export default class AxiosFactory {
});
// Add a Request interceptor
axiosInstance.interceptors.request.use((request: AxiosRequestConfig) => {
- request['timestamp'] = new Date();
- Logging.logDebug({
- tenantID: tenantID,
- action: ServerAction.HTTP_REQUEST,
- message: `HTTP Request >> ${request.method.toLocaleUpperCase()} '${request.url}'`,
- module: MODULE_NAME, method: 'interceptor',
- detailedMessages: { request }
- });
+ Logging.logAxiosRequest(tenantID, request);
return request;
}, async (error: AxiosError) => {
- // Error handling is done outside to get the proper module information
- Logging.logError({
- tenantID: tenantID,
- action: ServerAction.HTTP_REQUEST,
- message: `HTTP Request Error >> ${error.config.method.toLocaleUpperCase()}/${error.code} '${error.config.url}' - ${error.message}`,
- module: MODULE_NAME, method: 'interceptor',
- detailedMessages: {
- url: error.config.url,
- message: error.message,
- axiosError: error.toJSON(),
- }
- });
+ Logging.logAxiosError(tenantID, error);
return Promise.reject(error);
});
// Add a Response interceptor
axiosInstance.interceptors.response.use((response: AxiosResponse) => {
- // Compute duration
- let durationSecs = 0;
- if (response.config['timestamp']) {
- durationSecs = (new Date().getTime() - response.config['timestamp'].getTime()) / 1000;
- }
- // Compute Length
- let contentLengthKB = response.headers['content-length'] as number;
- if (contentLengthKB) {
- contentLengthKB /= 1000;
- }
- Logging.logDebug({
- tenantID: tenantID,
- action: ServerAction.HTTP_RESPONSE,
- message: response.config['timestamp'] ?
- `HTTP Response - ${durationSecs}s - ${!isNaN(contentLengthKB) ? contentLengthKB : '? '}kB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'` :
- `HTTP Response - ${!isNaN(contentLengthKB) ? contentLengthKB : '? '}kB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'`,
- module: MODULE_NAME, method: 'interceptor',
- detailedMessages: {
- status: response.status,
- request: response.config,
- response: response.data
- }
- });
+ Logging.logAxiosResponse(tenantID, response);
return response;
}, async (error: AxiosError) => {
- // Compute duration
- let durationSecs = 0;
- if (error.config['timestamp']) {
- durationSecs = (new Date().getTime() - error.config['timestamp'].getTime()) / 1000;
- }
- // Error handling is done outside to get the proper module information
- Logging.logError({
- tenantID: tenantID,
- action: ServerAction.HTTP_RESPONSE,
- message: error.config['timestamp'] ?
- `HTTP Response Error - ${durationSecs}s << ${error.config.method.toLocaleUpperCase()}/${error.code} '${error.config.url}' - ${error.message}` :
- `HTTP Response Error << ${error.config.method.toLocaleUpperCase()}/${error.code} '${error.config.url}' - ${error.message}`,
- module: MODULE_NAME, method: 'interceptor',
- detailedMessages: {
- url: error.config.url,
- message: error.message,
- axiosError: error.toJSON(),
- }
- });
+ Logging.logAxiosError(tenantID, error);
return Promise.reject(error);
});
// Add

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
+import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import { Log, LogLevel, LogType } from '../types/Log';
import { NextFunction, Request, Response } from 'express';
import { PerformanceObserver, performance } from 'perf_hooks';
-import jwtDecode from 'jwt-decode';
import AppAuthError from '../exception/AppAuthError';
import AppError from '../exception/AppError';
@@ -17,6 +17,7 @@ import UserToken from '../types/UserToken';
import Utils from './Utils';
import cfenv from 'cfenv';
import cluster from 'cluster';
+import jwtDecode from 'jwt-decode';
import os from 'os';
import { v4 as uuid } from 'uuid';
@@ -167,7 +168,7 @@ export default class Logging {
tenantID: tenantID,
action: ServerAction.HTTP_REQUEST,
user: userToken,
- message: `HTTP Request << ${req.method} '${req.url}'`,
+ message: `Express HTTP Request << ${req.method} '${req.url}'`,
module: MODULE_NAME, method: 'logExpressRequest',
detailedMessages: {
url: req.url,
@@ -198,17 +199,15 @@ export default class Logging {
durationSecs = (new Date().getTime() - req['timestamp'].getTime()) / 1000;
}
// Compute Length
- let contentLengthKB = res.getHeader('content-length') as number;
- if (contentLengthKB) {
- contentLengthKB /= 1000;
+ let contentLengthKB = 0;
+ if (res.getHeader('content-length')) {
+ contentLengthKB = res.getHeader('content-length') as number / 1000;
}
Logging.logDebug({
tenantID: tenantID,
user: req.user,
action: ServerAction.HTTP_RESPONSE,
- message: req['timestamp'] ?
- `HTTP Response - ${durationSecs}s - ${!isNaN(contentLengthKB) ? contentLengthKB : '? '}kB >> ${req.method}/${res.statusCode} '${req.url}'` :
- `HTTP Response - ${!isNaN(contentLengthKB) ? contentLengthKB : '? '}kB >> ${req.method}/${res.statusCode} '${req.url}'`,
+ message: `Express HTTP Response - ${(durationSecs > 0) ? durationSecs : '?'}s - ${(contentLengthKB > 0) ? contentLengthKB : '?'}kB >> ${req.method}/${res.statusCode} '${req.url}'`,
module: MODULE_NAME, method: 'logExpressResponse',
detailedMessages: {
request: req.url,
@@ -222,13 +221,62 @@ export default class Logging {
}
public static logExpressError(error: Error, req: Request, res: Response, next: NextFunction): void {
- // Rebuild the Action if possible
- let action: ServerAction;
- const uriAction = req.url.split('/')[3];
- if (uriAction) {
- action = uriAction.split('?')[0] as ServerAction;
+ // Log
+ Logging.logActionExceptionMessageAndSendResponse(ServerAction.HTTP_ERROR, error, req, res, next);
+ }
+
+ public static logAxiosRequest(tenantID: string, request: AxiosRequestConfig): void {
+ request['timestamp'] = new Date();
+ Logging.logDebug({
+ tenantID: tenantID,
+ action: ServerAction.HTTP_REQUEST,
+ message: `Axios HTTP Request >> ${request.method.toLocaleUpperCase()} '${request.url}'`,
+ module: MODULE_NAME, method: 'interceptor',
+ detailedMessages: { request }
+ });
+ }
+
+ public static logAxiosResponse(tenantID: string, response: AxiosResponse): void {
+ // Compute duration
+ let durationSecs = 0;
+ if (response.config['timestamp']) {
+ durationSecs = (new Date().getTime() - response.config['timestamp'].getTime()) / 1000;
+ }
+ // Compute Length
+ let contentLengthKB = 0;
+ if (response.config.headers['Content-Length']) {
+ contentLengthKB = response.config.headers['Content-Length'] / 1000;
}
- Logging.logActionExceptionMessageAndSendResponse(action, error, req, res, next);
+ Logging.logDebug({
+ tenantID: tenantID,
+ action: ServerAction.HTTP_RESPONSE,
+ message: `Axios HTTP Response - ${(durationSecs > 0) ? durationSecs : '?'}s - ${(contentLengthKB > 0) ? contentLengthKB : '?'}kB << ${response.config.method.toLocaleUpperCase()}/${response.status} '${response.config.url}'`,
+ module: MODULE_NAME, method: 'interceptor',
+ detailedMessages: {
+ status: response.status,
+ statusText: response.statusText,
+ request: response.config,
+ response: response.data
+ }
+ });
+ }
+
+ public static logAxiosError(tenantID: string, error: AxiosError): void {
+ // Error handling is done outside to get the proper module information
+ Logging.logError({
+ tenantID: tenantID,
+ action: ServerAction.HTTP_ERROR,
+ message: `Axios HTTP Error >> ${error.config.method.toLocaleUpperCase()}/${error.response.status} '${error.config.url}' - ${error.message}`,
+ module: MODULE_NAME, method: 'interceptor',
+ detailedMessages: {
+ url: error.config.url,
+ status: error.response.status,
+ statusText: error.response.statusText,
+ message: error.message,
+ response: error.response.data,
+ axiosError: error.toJSON(),
+ }
+ });
}
public static logSendAction(module: string, tenantID: string, chargeBoxID: string, action: ServerAction, args: any): void {

Moved Axios logs in Logging