Skip to content

Commit

Permalink
refactor: replace console with logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 28, 2023
1 parent 2567501 commit 5d13f57
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
9 changes: 4 additions & 5 deletions src/modules/auth/auth.service.ts
Expand Up @@ -18,7 +18,7 @@ export class AuthService {
private readonly authTokenClient: SiweAuthTokensClient;
private authenticatePromise: Promise<void>;
private isAuthenticating = false;

private logger = getLogger();
/**
*
* @param signerService service used to sign authentication message
Expand All @@ -43,7 +43,7 @@ export class AuthService {

axiosRetry(this.httpClient, {
onRetry: (retryCount: number, error: AxiosError) => {
console.warn(
this.logger.warn(
`[AUTH SERVICE] retrying request to ${error.config.url} for ${retryCount} time`
);
},
Expand All @@ -52,7 +52,7 @@ export class AuthService {
const isRetry = await this.handleRequestError(error);
return isRetry;
} catch (_) {
console.warn(
this.logger.warn(
`[AUTH SERVICE] Error handling request error to ${error.config.url}`
);
return true;
Expand Down Expand Up @@ -132,8 +132,7 @@ export class AuthService {
if (!this.isBrowser && !this.refresh_token) return undefined;
getLogger().info('[AuthService] refreshing tokens');
const res = await this.httpClient.get<AuthTokens>(
`/refresh_token${
this.isBrowser ? '' : `?refresh_token=${this.refresh_token}`
`/refresh_token${this.isBrowser ? '' : `?refresh_token=${this.refresh_token}`
}`
);
getLogger().debug('[AuthService] refreshed tokens fetched');
Expand Down
Expand Up @@ -36,6 +36,7 @@ import { ERROR_MESSAGES } from '../../errors';
import { CacheClient } from '../cache-client';
import { KEY_TYPE } from './verifiable-credentials.const';
import { Claim } from '../claims/claims.types';
import { getLogger } from '../../config/logger.config';

/**
* Service responsible for managing verifiable credentials and presentations.
Expand All @@ -47,6 +48,7 @@ import { Claim } from '../claims/claims.types';
* ```
* */
export abstract class VerifiableCredentialsServiceBase {
private logger = getLogger();
/**
* Get prepared data for signing a credential.
*
Expand Down Expand Up @@ -129,7 +131,7 @@ export abstract class VerifiableCredentialsServiceBase {
constructor(
protected readonly _signerService: SignerService,
protected readonly _cacheClient: CacheClient
) {}
) { }

// * Should be overridden by the implementation
static async create(
Expand Down Expand Up @@ -217,7 +219,7 @@ export abstract class VerifiableCredentialsServiceBase {
presentation: requiredPresentation,
});
if (errors.length > 0) {
console.dir(errors, { depth: 20 });
this.logger.error(errors);
throw new Error(ERROR_MESSAGES.ERROR_CONTINUING_EXCHANGE);
}
if (nextVpRequest) {
Expand Down Expand Up @@ -563,11 +565,11 @@ export abstract class VerifiableCredentialsServiceBase {
},
issuerFields: params.issuerFields
? [
...params.issuerFields.map((field) => ({
...field,
value: field.value.toString(),
})),
]
...params.issuerFields.map((field) => ({
...field,
value: field.value.toString(),
})),
]
: [],
id: params.id,
},
Expand Down
15 changes: 5 additions & 10 deletions src/utils/logger.ts
Expand Up @@ -13,15 +13,14 @@ export enum LogLevel {
* Abstract overridable class for logger
*/
export abstract class ILogger {
constructor(protected readonly maxLevel: LogLevel = LogLevel.debug) { }
constructor(protected readonly maxLevel: LogLevel = LogLevel.debug) {}

debug = (message: unknown) => this.log(message, LogLevel.debug);
info = (message: unknown) => this.log(message, LogLevel.info);
warn = (message: unknown) => this.log(message, LogLevel.warn);
error = (message: unknown) => this.log(message, LogLevel.error);

log = (message: unknown, level: LogLevel) =>
this._log(message, level);
log = (message: unknown, level: LogLevel) => this._log(message, level);

protected abstract _log(message: unknown, level: LogLevel): void;
}
Expand All @@ -32,15 +31,11 @@ export abstract class ILogger {
export class ConsoleLogger extends ILogger {
private logger = winston.createLogger({
level: this.maxLevel,
format: combine(
colorize(),
simple()
),
transports:
new winston.transports.Console(),
format: combine(colorize(), simple()),
transports: new winston.transports.Console(),
});

_log(message: unknown, level: LogLevel) {
this.logger[level](message);
};
}
}

0 comments on commit 5d13f57

Please sign in to comment.