Skip to content

Commit

Permalink
WIP replace all calls from logxxxWithCorrelationIds to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelie-crouillebois committed Jul 2, 2024
1 parent 44793fc commit 703409c
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 79 deletions.
3 changes: 1 addition & 2 deletions api/lib/domain/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { EventDispatcher } from '../../infrastructure/events/EventDispatcher.js'
import { EventDispatcherLogger } from '../../infrastructure/events/EventDispatcherLogger.js';
import * as disabledPoleEmploiNotifier from '../../infrastructure/externals/pole-emploi/disabled-pole-emploi-notifier.js';
import * as poleEmploiNotifier from '../../infrastructure/externals/pole-emploi/pole-emploi-notifier.js';
import { monitoringTools as MonitoringTools } from '../../infrastructure/monitoring-tools.js';
import * as badgeAcquisitionRepository from '../../infrastructure/repositories/badge-acquisition-repository.js';
import * as campaignParticipationRepository from '../../infrastructure/repositories/campaign-participation-repository.js';
import * as campaignParticipationResultRepository from '../../infrastructure/repositories/campaign-participation-result-repository.js';
Expand Down Expand Up @@ -115,7 +114,7 @@ const handlersToBeInjected = {
};

function buildEventDispatcher(handlersStubs) {
const eventDispatcher = new EventDispatcher(new EventDispatcherLogger(MonitoringTools, config, performance));
const eventDispatcher = new EventDispatcher(new EventDispatcherLogger(logger, config, performance));

const handlersNames = _.map(handlersToBeInjected, (handler) => handler.name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import lodash from 'lodash';
import { PGSQL_FOREIGN_KEY_VIOLATION_ERROR } from '../../../db/pgsql-errors.js';
import { InvalidInputDataError } from '../../../src/shared/domain/errors.js';
import * as codeGenerator from '../../../src/shared/domain/services/code-generator.js';
import { logger } from '../../../src/shared/infrastructure/utils/logger.js';
import { CONCURRENCY_HEAVY_OPERATIONS } from '../../infrastructure/constants.js';
import { DomainTransaction } from '../../infrastructure/DomainTransaction.js';
import { monitoringTools } from '../../infrastructure/monitoring-tools.js';
import { DomainError, ObjectValidationError, OrganizationTagNotFound, TargetProfileInvalidError } from '../errors.js';
import { Organization, OrganizationForAdmin, OrganizationTag } from '../models/index.js';

Expand Down Expand Up @@ -312,5 +312,5 @@ function _monitorError(message, { data, error, event } = {}) {
monitoringData.error = { name: error.constructor.name };
}

monitoringTools.logErrorWithCorrelationIds(monitoringData);
logger.error(monitoringData);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PoleEmploiPayload } from '../../infrastructure/externals/pole-emploi/PoleEmploiPayload.js';
import * as httpErrorsHelper from '../../infrastructure/http/errors-helper.js';
import { httpAgent } from '../../infrastructure/http/http-agent.js';
import * as monitoringTools from '../../infrastructure/monitoring-tools.js';
import { PoleEmploiSending } from '../models/PoleEmploiSending.js';
import { logger } from '../../../src/shared/infrastructure/utils/logger.js';

const sendSharedParticipationResultsToPoleEmploi = async ({
campaignParticipationId,
Expand All @@ -20,7 +20,7 @@ const sendSharedParticipationResultsToPoleEmploi = async ({
notifierDependencies = {
httpAgent,
httpErrorsHelper,
monitoringTools,
logger,
},
}) => {
const participation = await campaignParticipationRepository.get(campaignParticipationId);
Expand Down
10 changes: 5 additions & 5 deletions api/lib/infrastructure/events/EventDispatcherLogger.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class EventDispatcherLogger {
constructor(monitoringTools, settings, performance) {
this._monitoringTools = monitoringTools;
constructor(logger, settings, performance) {
this._logger = logger;
this._settings = settings;
this._performance = performance;
}

onEventDispatchStarted(event, eventHandlerName) {
if (this._settings?.logging?.enableLogStartingEventDispatch) {
this._monitoringTools.logInfoWithCorrelationIds({
this._logger.info({
...buildLogBody({ event, eventHandlerName }),
message: 'EventDispatcher : Event dispatch started',
});
Expand All @@ -19,7 +19,7 @@ class EventDispatcherLogger {

onEventDispatchSuccess(event, eventHandlerName, loggingContext) {
if (this._settings?.logging?.enableLogEndingEventDispatch) {
this._monitoringTools.logInfoWithCorrelationIds({
this._logger.info({
...buildLogBody({ event, eventHandlerName, duration: this._duration(loggingContext) }),
message: 'EventDispatcher : Event dispatched successfully',
});
Expand All @@ -28,7 +28,7 @@ class EventDispatcherLogger {

onEventDispatchFailure(event, eventHandlerName, error) {
if (this._settings?.logging?.enableLogEndingEventDispatch) {
this._monitoringTools.logInfoWithCorrelationIds({
this._logger.info({
...buildLogBody({ event, eventHandlerName, error }),
message: 'EventDispatcher : An error occurred while dispatching the event',
});
Expand Down
10 changes: 5 additions & 5 deletions api/lib/infrastructure/events/EventHandlerDependenciesBuilder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ImportOrganizationLearnersJob } from '../../../src/prescription/learner-management/infrastructure/jobs/ImportOrganizationLearnersJob.js';
import { ValidateOrganizationImportFileJob } from '../../../src/prescription/learner-management/infrastructure/jobs/ValidateOrganizationImportFileJob.js';
import { logger } from '../../../src/shared/infrastructure/utils/logger.js';
import { GarAnonymizedBatchEventsLoggingJob } from '../../infrastructure/jobs/audit-log/GarAnonymizedBatchEventsLoggingJob.js';
import { monitoringTools } from '../../infrastructure/monitoring-tools.js';
import { UserAnonymizedEventLoggingJob } from '../jobs/audit-log/UserAnonymizedEventLoggingJob.js';
import { ParticipationResultCalculationJob } from '../jobs/campaign-result/ParticipationResultCalculationJob.js';
import { SendSharedParticipationResultsToPoleEmploiJob } from '../jobs/campaign-result/SendSharedParticipationResultsToPoleEmploiJob.js';
Expand All @@ -10,12 +10,12 @@ function build(classToInstanciate, domainTransaction) {
const dependencies = _buildDependencies(domainTransaction);

const instance = new classToInstanciate(dependencies);
return new EventErrorHandler(instance, monitoringTools);
return new EventErrorHandler(instance, logger);
}

function _buildDependencies(domainTransaction) {
return {
monitoringTools,
logger,
userAnonymizedEventLoggingJob: new UserAnonymizedEventLoggingJob(domainTransaction.knexTransaction),
garAnonymizedBatchEventsLoggingJob: new GarAnonymizedBatchEventsLoggingJob(domainTransaction.knexTransaction),
participationResultCalculationJob: new ParticipationResultCalculationJob(domainTransaction.knexTransaction),
Expand Down Expand Up @@ -48,7 +48,7 @@ class EventErrorHandler {
}

logHandlerStarting(event) {
this.logger.logInfoWithCorrelationIds({
this.logger.info({
message: {
event,
handlerName: this.handler.name,
Expand All @@ -59,7 +59,7 @@ class EventErrorHandler {
}

logHandlerFailed(event, error) {
this.logger.logErrorWithCorrelationIds({
this.logger.error({
message: {
event,
handlerName: this.handler.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { config } from '../../../config.js';
import { UnexpectedUserAccountError } from '../../../domain/errors.js';

const notify = async (userId, payload, dependencies) => {
const { authenticationMethodRepository, httpAgent, httpErrorsHelper, monitoringTools } = dependencies;
const { authenticationMethodRepository, httpAgent, httpErrorsHelper, logger } = dependencies;
if (config.featureToggles.deprecatePoleEmploiPushNotification) {
payload = { ...payload, deprecated: true };
}
monitoringTools.logInfoWithCorrelationIds({
logger.info({
event: 'participation-send-pole-emploi',
'pole-emploi-action': 'send-results',
'participation-state': participationState(payload),
Expand All @@ -35,7 +35,7 @@ const notify = async (userId, payload, dependencies) => {
const expiredDate = get(authenticationMethod, 'authenticationComplement.expiredDate');
const refreshToken = get(authenticationMethod, 'authenticationComplement.refreshToken');
if (!refreshToken || new Date(expiredDate) <= new Date()) {
monitoringTools.logInfoWithCorrelationIds({
logger.info({
event: 'participation-send-pole-emploi',
'pole-emploi-action': 'refresh-token',
'participation-state': participationState(payload),
Expand All @@ -57,7 +57,7 @@ const notify = async (userId, payload, dependencies) => {

if (!tokenResponse.isSuccessful) {
const serializedError = httpErrorsHelper.serializeHttpErrorResponse(tokenResponse);
monitoringTools.logErrorWithCorrelationIds({
logger.error({
event: 'participation-send-pole-emploi',
'pole-emploi-action': 'refresh-token',
'participation-state': participationState(payload),
Expand Down Expand Up @@ -100,7 +100,7 @@ const notify = async (userId, payload, dependencies) => {

if (!httpResponse.isSuccessful) {
const serializedError = httpErrorsHelper.serializeHttpErrorResponse(httpResponse);
monitoringTools.logErrorWithCorrelationIds({
logger.error({
event: 'participation-send-pole-emploi',
'pole-emploi-action': 'send-results',
'participation-state': participationState(payload),
Expand Down
11 changes: 5 additions & 6 deletions api/lib/infrastructure/http/http-agent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import perf_hooks from 'node:perf_hooks';

import axios from 'axios';
import { logger } from '../../../src/shared/infrastructure/utils/logger.js';

const { performance } = perf_hooks;

import { monitoringTools } from '../monitoring-tools.js';

class HttpResponse {
constructor({ code, data, isSuccessful }) {
this.code = code;
Expand All @@ -27,7 +26,7 @@ const httpAgent = {
}
const httpResponse = await axios.post(url, payload, config);
responseTime = performance.now() - startTime;
monitoringTools.logInfoWithCorrelationIds({
logger.info({
metrics: { responseTime },
message: `End POST request to ${url} success: ${httpResponse.status}`,
});
Expand All @@ -52,7 +51,7 @@ const httpAgent = {

const message = `End POST request to ${url} error: ${code || ''} ${JSON.stringify(data)}`;

monitoringTools.logErrorWithCorrelationIds({
logger.error({
metrics: { responseTime },
message,
});
Expand All @@ -77,7 +76,7 @@ const httpAgent = {
}
const httpResponse = await axios.get(url, config);
responseTime = performance.now() - startTime;
monitoringTools.logInfoWithCorrelationIds({
logger.info({
metrics: { responseTime },
message: `End GET request to ${url} success: ${httpResponse.status}`,
});
Expand All @@ -102,7 +101,7 @@ const httpAgent = {
data = null;
}

monitoringTools.logErrorWithCorrelationIds({
logger.error({
metrics: { responseTime },
message: `End GET request to ${url} error: ${code || ''} ${JSON.stringify(data)}`,
});
Expand Down
3 changes: 3 additions & 0 deletions api/src/devcomp/application/modules/controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { logger } from '../../../shared/infrastructure/utils/logger.js';

const getBySlug = async function (request, h, { moduleSerializer, usecases }) {
const { slug } = request.params;
const module = await usecases.getModule({ slug });
logger.info('hello world');

return moduleSerializer.serialize(module);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'node:fs/promises';

import { FileValidationError } from '../../../../lib/domain/errors.js';
import { eventBus } from '../../../../lib/domain/events/index.js';
import { logErrorWithCorrelationIds } from '../../../../lib/infrastructure/monitoring-tools.js';
import { ApplicationTransaction } from '../../shared/infrastructure/ApplicationTransaction.js';
import { usecases } from '../domain/usecases/index.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {

describe('Integration | Domain | UseCases | send-shared-participation-results-to-pole-emploi', function () {
let campaignParticipationId, userId, responseCode;
let httpAgentStub, httpErrorsHelperStub, monitoringToolsStub;
let httpAgentStub, httpErrorsHelperStub, loggerStub;

beforeEach(async function () {
httpAgentStub = { post: sinon.stub() };
monitoringToolsStub = { logErrorWithCorrelationIds: sinon.stub(), logInfoWithCorrelationIds: sinon.stub() };
loggerStub = { error: sinon.stub(), info: sinon.stub() };
httpErrorsHelperStub = { serializeHttpErrorResponse: sinon.stub() };
responseCode = Symbol('responseCode');

Expand Down Expand Up @@ -53,7 +53,7 @@ describe('Integration | Domain | UseCases | send-shared-participation-results-to
notifierDependencies: {
httpAgent: httpAgentStub,
httpErrorsHelper: httpErrorsHelperStub,
monitoringTools: monitoringToolsStub,
logger: loggerStub,
},
});

Expand Down
Loading

0 comments on commit 703409c

Please sign in to comment.