Skip to content

refactor: convert logger to lib #1222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions .env.demo
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ DEEPLINK_DOMAIN='https://link.credebl.id?url='

ENABLE_CORS_IP_LIST=http://localhost:3000, http://localhost:3001, http://localhost:5000,http://localhost:8085,https://verify.credebl.id,https://verifyed.credebl.id,https://verify-api.credebl.id,https://qa.credebl.id,https://dev.credebl.id,https://credebl.id

USER_NKEY_SEED=SUAILBO6CYQF4RPIZYCVCFRIOMJ7BW33PFZDFEZKOHV65MN3W37FFJA3JY
API_GATEWAY_NKEY_SEED=SUAI7SDZAHS24I7JWOB77BK6EFE3ZS3NCF5FW22PMWCXGE56OBPYHK5734
ORGANIZATION_NKEY_SEED=SUABORS4UFEV2OAWNEXB7JA76XNOF7A6YJAFOGRZTWOEGCXER36P2NP7JQ
AGENT_PROVISIONING_NKEY_SEED=SUAIUEPEC5D4KMTLK45UOPZ45JQ5QUMMIIJY2UT37RMQE6GENBTJTT2MSQ
AGENT_SERVICE_NKEY_SEED=SUABOFFEQNQY7YR4BIZDJENJ5T24CCDYOYTVHBKQLEWTZUTPTL3EQ3IZ3U
VERIFICATION_NKEY_SEED=SUAOTKYQMN6RGVWLZII22A4EFURAT65H4PGU3G5QXKM427HZ3JKIQZ7KU4
LEDGER_NKEY_SEED=SUABKZWIZCMUROUKCSHL774UOLKFQZR2UPQXII5FCGXHF25GFQX2XLIPLI
ISSUANCE_NKEY_SEED=SUAG7GEESSHO2ZF4J2IUKB6QPF4ENTLO7MLXZSSF67MEETTFZWFGJNDYVU
CONNECTION_NKEY_SEED=SUALLWKCDN2KBB4YTZUNUUFP7ZNSI4PBXVK7X5FPNX6LQ3DCHFGYX4JPLQ
ECOSYSTEM_NKEY_SEED=SUAGUAGZZLT2LYEA3SRWFXCJL32MXR5GCBAZNYJLLIUH4O76GDEGQAYM74
CREDENTAILDEFINITION_NKEY_SEED=SUAAYNLVKEP2E4JPCJ7OYSQ6OFCDBQ3GCTOPARBXBNP64JFESDWH7N3FQA
SCHEMA_NKEY_SEED=SUAESXIRPE4PBJR26T44XPPIAONQ3YJTN45VSTRNZUW77GAF5RBP3SEBFA
UTILITIES_NKEY_SEED=SUAHLF3PYEGNN3J2LZHHT6LOQ4GQ2CPGATEO66XRXLCEE6QYSM26TKCNUM
CLOUD_WALLET_NKEY_SEED=SUAJ3VT7IFZXVE7SSTX3JFE6F2U6DYDROW6VTCCGFVVB6D7O6C7OWJWTPM
GEOLOCATION_NKEY_SEED=SUAA3YQLMQPKEK224OVFGENQ3VRYD57LNPJFMGDULOO57CUYOQLAA7KBJU
NOTIFICATION_NKEY_SEED=SUAF5V6RN6HHOLBJX6UV7443PBNT7NSAJ6YCUOW7LTZQ77PXXAMH25AHPI
USER_NKEY_SEED=
API_GATEWAY_NKEY_SEED=
ORGANIZATION_NKEY_SEED=
AGENT_PROVISIONING_NKEY_SEED=
AGENT_SERVICE_NKEY_SEED=
VERIFICATION_NKEY_SEED=
LEDGER_NKEY_SEED=
ISSUANCE_NKEY_SEED=
CONNECTION_NKEY_SEED=
ECOSYSTEM_NKEY_SEED=
CREDENTAILDEFINITION_NKEY_SEED=
SCHEMA_NKEY_SEED=
UTILITIES_NKEY_SEED=
CLOUD_WALLET_NKEY_SEED=
GEOLOCATION_NKEY_SEED=
NOTIFICATION_NKEY_SEED=

KEYCLOAK_DOMAIN=http://localhost:8080/
KEYCLOAK_ADMIN_URL=http://localhost:8080
Expand Down Expand Up @@ -144,5 +144,5 @@ APP=api

#Schema-file-server
APP_PORT=4000
JWT_TOKEN_SECRET=c2e48ca31ac2a0b9af47f3a9f5a0809a858c296948c1326eb746bb7bc945a9d5
JWT_TOKEN_SECRET=
ISSUER=Credebl
4 changes: 2 additions & 2 deletions libs/config/src/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DevelopmentEnvironment } from '@credebl/enum/enum';
import { DevelopmentEnvironment } from '../../enum/src/enum';
import { Injectable } from '@nestjs/common';
import { ConfigService as NestConfigService } from '@nestjs/config';

@Injectable()
export class ConfigService {
constructor(private readonly configService: NestConfigService) {}
constructor(private readonly configService: NestConfigService) { }

get isProduction(): boolean {
return DevelopmentEnvironment.PRODUCTION === this.environment;
Expand Down
34 changes: 34 additions & 0 deletions libs/logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@credebl/logger",
"main": "src/index",
"types": "src/index",
"version": "0.0.1",
"files": [
"dist"
],
"scripts": {
"build": "pnpm run clean && pnpm run compile",
"clean": "rimraf ../../dist/libs/logger",
"compile": "tsc -p tsconfig.build.json",
"test": "jest"
},
"dependencies": {
"@credebl/common": "workspace:*",
"@elastic/ecs-winston-format": "^1.5.3",
"@nestjs/common": "^10.2.7",
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.1.3",
"morgan": "^1.10.0",
"nestjs-cls": "^4.3.0",
"winston": "3.4",
"winston-daily-rotate-file": "^5.0.0",
"winston-elasticsearch": "0.16.0",
"rxjs": "^7.8.1",
"uuid": "^9.0.0"
},
"devDependencies": {
"reflect-metadata": "^0.1.13",
"rimraf": "^4.4.0",
"typescript": "^5.1.6"
}
}
3 changes: 2 additions & 1 deletion libs/logger/src/logger.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogData, LogLevel } from '@credebl/logger/log';
import { LogData, LogLevel } from './log';

export const LoggerBaseKey = Symbol();
export const LoggerKey = Symbol();
Expand All @@ -18,3 +18,4 @@ export default interface Logger {
emergency(message: string | Error, data?: LogData, profile?: string): void;
startProfile(id: string): void;
};

14 changes: 7 additions & 7 deletions libs/logger/src/logger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import {

import WinstonLogger, {
WinstonLoggerTransportsKey
} from '@credebl/logger/winstonLogger';
} from './winstonLogger';
import Logger, {
LoggerBaseKey,
LoggerKey
} from '@credebl/logger/logger.interface';
import NestjsLoggerServiceAdapter from '@credebl/logger/nestjsLoggerServiceAdapter';
import ConsoleTransport from '@credebl/logger/transports/consoleTransport';
} from './logger.interface';
import NestjsLoggerServiceAdapter from './nestjsLoggerServiceAdapter';
import ConsoleTransport from './transports/consoleTransport';
import * as morgan from 'morgan';
import { ConfigService } from '../../config/src/config.service';
import LoggerService from '@credebl/logger/logger.service';
import { MICRO_SERVICE_NAME } from '@credebl/common/common.constant';
import LoggerService from './logger.service';
import { MICRO_SERVICE_NAME } from '@credebl/common/src/common.constant';

@Global()
@Module({
Expand Down Expand Up @@ -57,7 +57,7 @@ export class LoggerModule implements NestModule {
public constructor(
@Inject(LoggerKey) private readonly logger: Logger,
private readonly configService: ConfigService
) {}
) { }

public configure(consumer: MiddlewareConsumer): void {
consumer
Expand Down
22 changes: 11 additions & 11 deletions libs/logger/src/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Inject, Injectable, Scope } from '@nestjs/common';
import { INQUIRER } from '@nestjs/core';
import Logger, {
LoggerBaseKey
} from '@credebl/logger/logger.interface';
import { LogData, LogLevel } from '@credebl/logger/log';
} from './logger.interface';
import { LogData, LogLevel } from './log';
import { ConfigService } from '@nestjs/config';
import ContextStorageService, {
ContextStorageServiceKey
} from '@credebl/context/contextStorageService.interface';
import { MICRO_SERVICE_NAME } from '@credebl/common/common.constant';
} from '../../context/src/contextStorageService.interface';
import { MICRO_SERVICE_NAME } from '@credebl/common/src/common.constant';

@Injectable({ scope: Scope.TRANSIENT })
export default class LoggerService implements Logger {
Expand Down Expand Up @@ -42,27 +42,27 @@ export default class LoggerService implements Logger {
return this.logger.log(level, message, this.getLogData(data), profile);
}

public debug(message: string, data?: LogData, profile?: string) : void {
public debug(message: string, data?: LogData, profile?: string): void {
return this.logger.debug(message, this.getLogData(data), profile);
}

public info(message: string, data?: LogData, profile?: string) : void {
public info(message: string, data?: LogData, profile?: string): void {
return this.logger.info(message, this.getLogData(data), profile);
}

public warn(message: string | Error, data?: LogData, profile?: string) : void {
public warn(message: string | Error, data?: LogData, profile?: string): void {
return this.logger.warn(message, this.getLogData(data), profile);
}

public error(message: string | Error, data?: LogData, profile?: string) : void {
public error(message: string | Error, data?: LogData, profile?: string): void {
return this.logger.error(message, this.getLogData(data), profile);
}

public fatal(message: string | Error, data?: LogData, profile?: string) : void {
public fatal(message: string | Error, data?: LogData, profile?: string): void {
return this.logger.fatal(message, this.getLogData(data), profile);
}

public emergency(message: string | Error, data?: LogData, profile?: string) : void {
public emergency(message: string | Error, data?: LogData, profile?: string): void {
return this.logger.emergency(message, this.getLogData(data), profile);
}

Expand All @@ -78,7 +78,7 @@ export default class LoggerService implements Logger {
};
}

public startProfile(id: string) : void {
public startProfile(id: string): void {
this.logger.startProfile(id);
}
}
54 changes: 27 additions & 27 deletions libs/logger/src/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NestInterceptor, ExecutionContext, CallHandler, Inject } fr
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

import ContextStorageService, { ContextStorageServiceKey } from '@credebl/context/contextStorageService.interface';
import ContextStorageService, { ContextStorageServiceKey } from '../../context/src/contextStorageService.interface';
import Logger, { LoggerKey } from './logger.interface';
import { ClsService } from 'nestjs-cls';
import { v4 } from 'uuid';
Expand All @@ -13,34 +13,34 @@ export class LoggingInterceptor implements NestInterceptor {
constructor(
private readonly clsService: ClsService,
@Inject(ContextStorageServiceKey)
private readonly contextStorageService: ContextStorageService,
@Inject(LoggerKey) private readonly _logger: Logger,
) {}
private readonly contextStorageService: ContextStorageService,
@Inject(LoggerKey) private readonly _logger: Logger,
) { }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return this.clsService.run(() => {

this._logger.info('In LoggingInterceptor configuration');
const rpcContext = context.switchToRpc().getContext();
const headers = rpcContext.getHeaders();
if (!isNullUndefinedOrEmpty(headers)) {
this.contextStorageService.set('x-correlation-id', headers._description);
this.contextStorageService.setContextId(headers._description);
} else {
const newContextId = v4();
this.contextStorageService.set('x-correlation-id', newContextId);
this.contextStorageService.setContextId(newContextId);
}

return next.handle().pipe(
catchError((err) => {
this._logger.error(err);
return throwError(() => err);
})
);

});
return this.clsService.run(() => {

this._logger.info('In LoggingInterceptor configuration');
const rpcContext = context.switchToRpc().getContext();
const headers = rpcContext.getHeaders();

if (!isNullUndefinedOrEmpty(headers)) {
this.contextStorageService.set('x-correlation-id', headers._description);
this.contextStorageService.setContextId(headers._description);
} else {
const newContextId = v4();
this.contextStorageService.set('x-correlation-id', newContextId);
this.contextStorageService.setContextId(newContextId);
}

return next.handle().pipe(
catchError((err) => {
this._logger.error(err);
return throwError(() => err);
})
);

});

}

Expand Down
22 changes: 11 additions & 11 deletions libs/logger/src/nestjsLoggerServiceAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ConsoleLogger } from '@nestjs/common';
import Logger from '@credebl/logger/logger.interface';
import Logger from './logger.interface';
import { LoggerService } from '@nestjs/common/services/logger.service';
import { LogData } from '@credebl/logger/log';
import { LogData } from './log';

export default class NestjsLoggerServiceAdapter
extends ConsoleLogger
implements LoggerService
{
implements LoggerService {
public constructor(private readonly logger: Logger) {
super();
}

public info(message: any, ...optionalParams: any[]) : void {
public info(message: any, ...optionalParams: any[]): void {
return this.logger.info(message, this.getLogData(optionalParams));
}

public log(message: any, ...optionalParams: any[]) : void {
public log(message: any, ...optionalParams: any[]): void {
return this.logger.info(message, this.getLogData(optionalParams));
}

public error(message: any, ...optionalParams: any[]) : void {
public error(message: any, ...optionalParams: any[]): void {
return this.logger.error(message, this.getLogData(optionalParams));
}

public warn(message: any, ...optionalParams: any[]) : void {
public warn(message: any, ...optionalParams: any[]): void {
return this.logger.warn(message, this.getLogData(optionalParams));
}

public debug(message: any, ...optionalParams: any[]) : void {
public debug(message: any, ...optionalParams: any[]): void {
return this.logger.debug(message, this.getLogData(optionalParams));
}

public verbose(message: any, ...optionalParams: any[]) : void {
public verbose(message: any, ...optionalParams: any[]): void {
return this.logger.info(message, this.getLogData(optionalParams));
}

private getLogData(...optionalParams: any[]) : LogData {
private getLogData(...optionalParams: any[]): LogData {
return {
sourceClass: optionalParams[0] ? optionalParams[0] : undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion libs/logger/src/transports/consoleTransport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as winston from 'winston';
import { LogLevel } from '@credebl/logger/log';
import { LogLevel } from '../log';

enum LogColors {
red = '\x1b[31m',
Expand Down
Loading