Log Server Action in OCPI requests
MODIFY
src/monitoring/prometheus/PrometheusMonitoringServer.ts
src/monitoring/prometheus/PrometheusMonitoringServer.ts
@@ -31,7 +31,9 @@ export default class PrometheusMonitoringServer extends MonitoringServer {
// Create HTTP Server
this.expressApplication = ExpressUtils.initApplication();
// Log Express Request
- this.expressApplication.use(Logging.traceExpressRequest.bind(this));
+ this.expressApplication.use(
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
+ );
// Handle requests
this.expressApplication.use(
// eslint-disable-next-line @typescript-eslint/no-misused-promises

MODIFY
src/server/ExpressUtils.ts
src/server/ExpressUtils.ts
@@ -57,9 +57,13 @@ export default class ExpressUtils {
return app;
}
- public static postInitApplication(expressApplication: Application): void {
+ public static postInitApplication(expressApplication: Application, traceHttpRequest = true): void {
// Log Express Response
- expressApplication.use(Logging.traceExpressResponse.bind(this));
+ if (traceHttpRequest) {
+ expressApplication.use(
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressResponse(req, res, next)
+ );
+ }
// Error Handling
expressApplication.use(Logging.traceExpressError.bind(this));
}

MODIFY
src/server/ocpi/OCPIServer.ts
src/server/ocpi/OCPIServer.ts
@@ -31,8 +31,6 @@ export default class OCPIServer {
this.expressApplication = ExpressUtils.initApplication(null, ocpiRestConfig.debug);
// Authenticate
this.expressApplication.use(this.initialize.bind(this));
- // Log Express Request
- this.expressApplication.use(Logging.traceExpressRequest.bind(this));
// Routers
this.expressApplication.use('/ocpi', new GlobalRouter().buildRoutes());
// Handle 404
@@ -50,7 +48,7 @@ export default class OCPIServer {
}
});
// Post init
- ExpressUtils.postInitApplication(this.expressApplication);
+ ExpressUtils.postInitApplication(this.expressApplication, false);
}
// Start the server

MODIFY
src/server/ocpp/soap/SoapOCPPServer.ts
src/server/ocpp/soap/SoapOCPPServer.ts
import { ServerAction, ServerType } from '../../../types/Server';
+import express, { NextFunction, Request, Response } from 'express';
import CentralSystemConfiguration from '../../../types/configuration/CentralSystemConfiguration';
import ChargingStationConfiguration from '../../../types/configuration/ChargingStationConfiguration';
@@ -11,7 +12,6 @@ import { ServerUtils } from '../../ServerUtils';
import Utils from '../../../utils/Utils';
import centralSystemService15 from './services/SoapCentralSystemService15';
import centralSystemService16 from './services/SoapCentralSystemService16';
-import express from 'express';
import fs from 'fs';
import global from '../../../types/GlobalType';
import http from 'http';
@@ -29,7 +29,9 @@ export default class SoapOCPPServer extends OCPPServer {
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, centralSystemConfig.debug);
// Log Express Request
- this.expressApplication.use(Logging.traceExpressRequest.bind(this));
+ this.expressApplication.use(
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
+ );
// Initialize the HTTP server
this.httpServer = ServerUtils.createHttpServer(this.centralSystemConfig, this.expressApplication);
}

MODIFY
src/server/odata/ODataServer.ts
src/server/odata/ODataServer.ts
@@ -23,7 +23,9 @@ export default class ODataServer {
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, oDataServerConfig.debug);
// Log Express Request
- this.expressApplication.use(Logging.traceExpressRequest.bind(this));
+ this.expressApplication.use(
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
+ );
// Get URL of the CentralSystemRestServer
const restConf = Configuration.getCentralSystemRestServerConfig();
const restServerUrl = `${restConf.protocol}://${restConf.host}:${restConf.port}/`;

MODIFY
src/server/oicp/OICPServer.ts
src/server/oicp/OICPServer.ts
@@ -20,7 +20,9 @@ export default class OICPServer {
// Initialize express app
this.expressApplication = ExpressUtils.initApplication(null, oicpRestConfig.debug);
// Log Express Request
- this.expressApplication.use(Logging.traceExpressRequest.bind(this));
+ this.expressApplication.use(
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next)
+ );
// New OICP Services Instances
const oicpServices = new OICPServices(this.oicpRestConfig);
// Register all services in express

MODIFY
src/server/rest/RestServer.ts
src/server/rest/RestServer.ts
@@ -32,12 +32,12 @@ export default class RestServer {
this.expressApplication.use('/client/api/:action',
AuthService.authenticate(),
SessionHashService.checkUserAndTenantValidity.bind(this),
- Logging.traceExpressRequest.bind(this),
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next),
RestServerService.restServiceSecured.bind(this)
);
// Util API
this.expressApplication.use('/client/util/:action',
- Logging.traceExpressRequest.bind(this),
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next),
RestServerService.restServiceUtil.bind(this)
);
// Post init

MODIFY
src/server/rest/v1/router/GlobalRouterV1.ts
src/server/rest/v1/router/GlobalRouterV1.ts
+import express, { NextFunction, Request, Response } from 'express';
+
import AssetRouter from './api/AssetRouter';
import AuthRouter from './auth/AuthRouter';
import AuthService from '../service/AuthService';
@@ -24,7 +26,6 @@ import TenantRouter from './api/TenantRouter';
import TransactionRouter from './api/TransactionRouter';
import UserRouter from './api/UserRouter';
import UtilRouter from './util/UtilRouter';
-import express from 'express';
export default class GlobalRouterV1 {
private router: express.Router;
@@ -49,7 +50,7 @@ export default class GlobalRouterV1 {
this.router.use('/api',
AuthService.authenticate(),
SessionHashService.checkUserAndTenantValidity.bind(this),
- Logging.traceExpressRequest.bind(this),
+ async (req: Request, res: Response, next: NextFunction) => Logging.traceExpressRequest(req, res, next),
[
new AssetRouter().buildRoutes(),
new BillingRouter().buildRoutes(),

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -294,7 +294,7 @@ export default class Logging {
messageSuccess, messageError, messageSuccessAndError, messageNoSuccessNoError);
}
- public static async traceExpressRequest(req: Request, res: Response, next: NextFunction): Promise<void> {
+ public static async traceExpressRequest(req: Request, res: Response, next: NextFunction, action?: ServerAction): Promise<void> {
if (Logging.getTraceConfiguration().traceIngressHttp) {
try {
// Get Tenant info
@@ -339,7 +339,7 @@ export default class Logging {
Utils.isDevelopmentEnv() && Logging.logConsoleInfo(message);
await Logging.logDebug({
tenantID,
- action: ServerAction.HTTP_REQUEST,
+ action: action ?? ServerAction.HTTP_REQUEST,
user: userID,
message,
module: MODULE_NAME, method: 'logExpressRequest',
@@ -358,14 +358,18 @@ export default class Logging {
});
req['performanceID'] = performanceID;
} finally {
+ // Express call does not provide action
+ if (!action) {
next();
}
- } else {
+ }
+ // Express call does not provide action
+ } else if (!action) {
next();
}
}
- public static traceExpressResponse(req: Request, res: Response, next: NextFunction): void {
+ public static traceExpressResponse(req: Request, res: Response, next: NextFunction, action?: ServerAction): void {
if (Logging.getTraceConfiguration().traceIngressHttp) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
res.on('finish', async () => {
@@ -420,7 +424,7 @@ export default class Logging {
await Logging.logDebug({
tenantID: tenantID,
user: req.user,
- action: ServerAction.HTTP_RESPONSE,
+ action: action ?? ServerAction.HTTP_RESPONSE,
message,
module: MODULE_NAME, method: 'logExpressResponse',
detailedMessages: {
@@ -441,8 +445,11 @@ export default class Logging {
}
});
}
+ // Express call does not provide action
+ if (!action) {
next();
}
+ }
public static async traceExpressError(error: Error, req: Request, res: Response, next: NextFunction): Promise<void> {
await Logging.logActionExceptionMessageAndSendResponse(

MODIFY
src/utils/RouterUtils.ts
src/utils/RouterUtils.ts
@@ -23,7 +23,13 @@ export default class RouterUtils {
handleMethod: (serverAction: ServerAction, req: Request, res: Response, next: NextFunction) => Promise<void>,
serverAction: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
try {
+ // Trace Request
+ await Logging.traceExpressRequest(req, res, next, serverAction);
+ // Process
await handleMethod(serverAction, req, res, next);
+ // Trace Response
+ Logging.traceExpressResponse(req, res, next, serverAction);
+ // Trace
} catch (error) {
Utils.isDevelopmentEnv() && Logging.logConsoleError(error.stack);
void Logging.logActionExceptionMessage(req.tenant?.id ?? Constants.DEFAULT_TENANT_ID, error.params?.action ?? ServerAction.OCPI_ENDPOINT, error);

Log Server Action in OCPI requests