From a98f851133504ea8a3baead913b51382f052b74b Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Wed, 15 May 2024 14:04:31 +0200 Subject: [PATCH] refactor: create a void logger service impl Signed-off-by: Camila Belo --- packages/backend-app-api/api-report.md | 16 +++++++ .../backend-app-api/src/logging/VoidLogger.ts | 44 +++++++++++++++++++ packages/backend-app-api/src/logging/index.ts | 1 + .../src/next/services/mockServices.ts | 2 +- plugins/search-backend-module-pg/package.json | 1 + .../PgSearchEngine/PgSearchEngineIndexer.ts | 5 ++- yarn.lock | 1 + 7 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 packages/backend-app-api/src/logging/VoidLogger.ts diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index 5720c1c60f..1d681c308b 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -347,6 +347,22 @@ export const userInfoServiceFactory: () => ServiceFactory< 'plugin' >; +// @public +export class VoidLogger implements RootLoggerService { + // (undocumented) + child(_meta: JsonObject): LoggerService; + // (undocumented) + static create(): VoidLogger; + // (undocumented) + debug(_message: string, _meta?: JsonObject): void; + // (undocumented) + error(_message: string, _meta?: JsonObject): void; + // (undocumented) + info(_message: string, _meta?: JsonObject): void; + // (undocumented) + warn(_message: string, _meta?: JsonObject): void; +} + // @public export class WinstonLogger implements RootLoggerService { // (undocumented) diff --git a/packages/backend-app-api/src/logging/VoidLogger.ts b/packages/backend-app-api/src/logging/VoidLogger.ts new file mode 100644 index 0000000000..0f5acba07f --- /dev/null +++ b/packages/backend-app-api/src/logging/VoidLogger.ts @@ -0,0 +1,44 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + LoggerService, + RootLoggerService, +} from '@backstage/backend-plugin-api'; +import { JsonObject } from '@backstage/types'; + +/** + * An empty {@link @backstage/backend-plugin-api#LoggerService} implementation. + * + * @public + */ +export class VoidLogger implements RootLoggerService { + static create(): VoidLogger { + return new VoidLogger(); + } + + error(_message: string, _meta?: JsonObject): void {} + + warn(_message: string, _meta?: JsonObject): void {} + + info(_message: string, _meta?: JsonObject): void {} + + debug(_message: string, _meta?: JsonObject): void {} + + child(_meta: JsonObject): LoggerService { + return new VoidLogger(); + } +} diff --git a/packages/backend-app-api/src/logging/index.ts b/packages/backend-app-api/src/logging/index.ts index 14fe33f898..a7162553e3 100644 --- a/packages/backend-app-api/src/logging/index.ts +++ b/packages/backend-app-api/src/logging/index.ts @@ -14,5 +14,6 @@ * limitations under the License. */ +export { VoidLogger } from './VoidLogger'; export { WinstonLogger } from './WinstonLogger'; export type { WinstonLoggerOptions } from './WinstonLogger'; diff --git a/packages/backend-test-utils/src/next/services/mockServices.ts b/packages/backend-test-utils/src/next/services/mockServices.ts index a486a9ab0c..57621a9209 100644 --- a/packages/backend-test-utils/src/next/services/mockServices.ts +++ b/packages/backend-test-utils/src/next/services/mockServices.ts @@ -60,7 +60,7 @@ import { /** @internal */ function createLoggerMock() { return { - child: jest.fn().mockImplementation(() => createLoggerMock()), + child: jest.fn().mockImplementation(createLoggerMock), debug: jest.fn(), error: jest.fn(), info: jest.fn(), diff --git a/plugins/search-backend-module-pg/package.json b/plugins/search-backend-module-pg/package.json index 6645e2642f..6d61b7a050 100644 --- a/plugins/search-backend-module-pg/package.json +++ b/plugins/search-backend-module-pg/package.json @@ -47,6 +47,7 @@ "test": "backstage-cli package test" }, "dependencies": { + "@backstage/backend-app-api": "workspace:^", "@backstage/backend-common": "workspace:^", "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts index 9fe0e7091c..93b699fc74 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngineIndexer.ts @@ -14,12 +14,13 @@ * limitations under the License. */ -import { getVoidLogger } from '@backstage/backend-common'; +import { loggerToWinstonLogger } from '@backstage/backend-common'; import { BatchSearchEngineIndexer } from '@backstage/plugin-search-backend-node'; import { IndexableDocument } from '@backstage/plugin-search-common'; import { Knex } from 'knex'; import { Logger } from 'winston'; import { DatabaseStore } from '../database'; +import { VoidLogger } from '@backstage/backend-app-api'; /** @public */ export type PgSearchEngineIndexerOptions = { @@ -41,7 +42,7 @@ export class PgSearchEngineIndexer extends BatchSearchEngineIndexer { super({ batchSize: options.batchSize }); this.store = options.databaseStore; this.type = options.type; - this.logger = options.logger || getVoidLogger(); + this.logger = options.logger || loggerToWinstonLogger(VoidLogger.create()); } async initialize(): Promise { diff --git a/yarn.lock b/yarn.lock index 8ff4221b13..10c2ff9895 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6933,6 +6933,7 @@ __metadata: version: 0.0.0-use.local resolution: "@backstage/plugin-search-backend-module-pg@workspace:plugins/search-backend-module-pg" dependencies: + "@backstage/backend-app-api": "workspace:^" "@backstage/backend-common": "workspace:^" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^"