output log messages with correct level

This commit is contained in:
Hazelnoot 2025-06-06 13:36:33 -04:00
parent 00c0bdbc94
commit d0ae76214c
2 changed files with 19 additions and 6 deletions

View file

@ -8,6 +8,8 @@ import { Injectable } from '@nestjs/common';
import { UnrecoverableError } from 'bullmq'; import { UnrecoverableError } from 'bullmq';
import { HttpRequestService } from '@/core/HttpRequestService.js'; import { HttpRequestService } from '@/core/HttpRequestService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js';
import { StatusError } from '@/misc/status-error.js'; import { StatusError } from '@/misc/status-error.js';
import { CONTEXT, PRELOADED_CONTEXTS } from './misc/contexts.js'; import { CONTEXT, PRELOADED_CONTEXTS } from './misc/contexts.js';
import { validateContentTypeSetAsJsonLD } from './misc/validator.js'; import { validateContentTypeSetAsJsonLD } from './misc/validator.js';
@ -17,12 +19,12 @@ import type { JsonLd as JsonLdObject, RemoteDocument } from 'jsonld/jsonld-spec.
// RsaSignature2017 implementation is based on https://github.com/transmute-industries/RsaSignature2017 // RsaSignature2017 implementation is based on https://github.com/transmute-industries/RsaSignature2017
class JsonLd { class JsonLd {
public debug = false;
public preLoad = true; public preLoad = true;
public loderTimeout = 5000; public loderTimeout = 5000;
constructor( constructor(
private httpRequestService: HttpRequestService, private httpRequestService: HttpRequestService,
private readonly logger: Logger,
) { ) {
} }
@ -84,7 +86,7 @@ class JsonLd {
const transformedData = { ...data }; const transformedData = { ...data };
delete transformedData['signature']; delete transformedData['signature'];
const cannonidedData = await this.normalize(transformedData); const cannonidedData = await this.normalize(transformedData);
if (this.debug) console.debug(`cannonidedData: ${cannonidedData}`); this.logger.debug('cannonidedData', cannonidedData);
const documentHash = this.sha256(cannonidedData.toString()); const documentHash = this.sha256(cannonidedData.toString());
const verifyData = `${optionsHash}${documentHash}`; const verifyData = `${optionsHash}${documentHash}`;
return verifyData; return verifyData;
@ -115,7 +117,7 @@ class JsonLd {
if (this.preLoad) { if (this.preLoad) {
if (url in PRELOADED_CONTEXTS) { if (url in PRELOADED_CONTEXTS) {
if (this.debug) console.debug(`HIT: ${url}`); this.logger.debug(`Preload HIT: ${url}`);
return { return {
contextUrl: undefined, contextUrl: undefined,
document: PRELOADED_CONTEXTS[url], document: PRELOADED_CONTEXTS[url],
@ -124,7 +126,7 @@ class JsonLd {
} }
} }
if (this.debug) console.debug(`MISS: ${url}`); this.logger.debug(`Preload MISS: ${url}`);
const document = await this.fetchDocument(url); const document = await this.fetchDocument(url);
return { return {
contextUrl: undefined, contextUrl: undefined,
@ -169,13 +171,16 @@ class JsonLd {
@Injectable() @Injectable()
export class JsonLdService { export class JsonLdService {
private readonly logger: Logger;
constructor( constructor(
private httpRequestService: HttpRequestService, private httpRequestService: HttpRequestService,
loggerService: LoggerService,
) { ) {
this.logger = loggerService.getLogger('json-ld');
} }
@bindThis @bindThis
public use(): JsonLd { public use(): JsonLd {
return new JsonLd(this.httpRequestService); return new JsonLd(this.httpRequestService, this.logger);
} }
} }

View file

@ -23,6 +23,14 @@ export type DataElement = DataObject | Error | string | null;
// https://stackoverflow.com/questions/61148466/typescript-type-that-matches-any-object-but-not-arrays // https://stackoverflow.com/questions/61148466/typescript-type-that-matches-any-object-but-not-arrays
export type DataObject = Record<string, unknown> | (object & { length?: never; }); export type DataObject = Record<string, unknown> | (object & { length?: never; });
const levelFuncs = {
error: 'error',
warning: 'warn',
success: 'info',
info: 'log',
debug: 'debug',
} as const satisfies Record<Level, keyof typeof console>;
// eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export
export default class Logger { export default class Logger {
private context: Context; private context: Context;
@ -86,7 +94,7 @@ export default class Logger {
} else if (data != null) { } else if (data != null) {
args.push(data); args.push(data);
} }
console.log(...args); console[levelFuncs[level]](...args);
} }
@bindThis @bindThis