backend-plugin-api: loggerToWinstonLogger refactor

Co-authored-by: Johan Haals <johan.haals@gmail.com>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-07-08 14:48:08 +02:00
parent f147543044
commit 7f5a1f9b04
13 changed files with 105 additions and 38 deletions
-1
View File
@@ -21,4 +21,3 @@
*/
export { createBackend } from './wiring/types';
export { loggerToWinstonLogger } from './services/implementations/loggerService';
@@ -18,9 +18,9 @@ import { loadBackendConfig } from '@backstage/backend-common';
import {
configServiceRef,
createServiceFactory,
loggerToWinstonLogger,
loggerServiceRef,
} from '@backstage/backend-plugin-api';
import { loggerToWinstonLogger } from './loggerService';
export const configFactory = createServiceFactory({
service: configServiceRef,
@@ -23,38 +23,26 @@ import {
import { Logger as WinstonLogger } from 'winston';
class BackstageLogger implements Logger {
constructor(
private readonly options: {
winston: WinstonLogger;
},
) {}
static fromWinston(logger: WinstonLogger): BackstageLogger {
return new BackstageLogger(logger);
}
private constructor(private readonly winston: WinstonLogger) {}
info(message: string, ...meta: any[]): void {
this.options.winston.info(message, ...meta);
this.winston.info(message, ...meta);
}
child(fields: { [name: string]: string }): Logger {
return new BackstageLogger({
winston: this.options.winston.child(fields),
});
return new BackstageLogger(this.winston.child(fields));
}
toWinston() {
return this.options.winston;
}
}
export function loggerToWinstonLogger(logger: Logger): WinstonLogger {
return (logger as BackstageLogger).toWinston();
}
export const loggerFactory = createServiceFactory({
service: loggerServiceRef,
deps: {},
factory: async () => {
const root = new BackstageLogger({
winston: createRootLogger(),
});
const root = BackstageLogger.fromWinston(createRootLogger());
return async (pluginId: string) => {
return root.child({ pluginId });
};
@@ -19,9 +19,9 @@ import {
loggerServiceRef,
createServiceFactory,
tokenManagerServiceRef,
loggerToWinstonLogger,
} from '@backstage/backend-plugin-api';
import { ServerTokenManager } from '@backstage/backend-common';
import { loggerToWinstonLogger } from './loggerService';
export const tokenManagerFactory = createServiceFactory({
service: tokenManagerServiceRef,
@@ -19,9 +19,9 @@ import {
configServiceRef,
createServiceFactory,
loggerServiceRef,
loggerToWinstonLogger,
urlReaderServiceRef,
} from '@backstage/backend-plugin-api';
import { loggerToWinstonLogger } from './loggerService';
export const urlReaderFactory = createServiceFactory({
service: urlReaderServiceRef,
+3 -1
View File
@@ -38,7 +38,9 @@
"@backstage/backend-common": "^0.14.1-next.2",
"@backstage/plugin-permission-common": "^0.6.3-next.0",
"@backstage/backend-tasks": "^0.3.3-next.2",
"express": "^4.17.1"
"express": "^4.17.1",
"winston": "^3.2.1",
"winston-transport": "^4.5.0"
},
"devDependencies": {
"@backstage/cli": "^0.18.0-next.2",
+1 -2
View File
@@ -20,6 +20,5 @@
* @packageDocumentation
*/
export * from './services';
export * from './wiring';
export * from './services/system/types';
export * from './services/definitions';
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Backstage Authors
* Copyright 2022 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.
@@ -14,11 +14,4 @@
* limitations under the License.
*/
import { PluginEnvironment } from './types';
describe('test', () => {
it('unbreaks the test runner', () => {
const unbreaker = {} as PluginEnvironment;
expect(unbreaker).toBeTruthy();
});
});
export { loggerToWinstonLogger } from './loggerToWinstonLogger';
@@ -0,0 +1,43 @@
/*
* Copyright 2022 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 { Logger as BackstageLogger } from '../definitions';
import { Logger as WinstonLogger, createLogger } from 'winston';
import Transport, { TransportStreamOptions } from 'winston-transport';
class BackstageLoggerTransport extends Transport {
constructor(
private readonly backstageLogger: BackstageLogger,
opts?: TransportStreamOptions,
) {
super(opts);
}
log(info: { message: string }, callback: VoidFunction) {
// TODO: add support for levels and fields
this.backstageLogger.info(info.message);
callback();
}
}
export function loggerToWinstonLogger(
logger: BackstageLogger,
opts?: TransportStreamOptions,
): WinstonLogger {
return createLogger({
transports: [new BackstageLoggerTransport(logger, opts)],
});
}
@@ -0,0 +1,19 @@
/*
* Copyright 2022 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.
*/
export * from './definitions';
export * from './helpers';
export * from './system';
@@ -0,0 +1,25 @@
/*
* Copyright 2022 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.
*/
export type {
ServiceRef,
TypesToServiceRef,
DepsToDepFactories,
FactoryFunc,
ServiceFactory,
AnyServiceFactory,
} from './types';
export { createServiceRef, createServiceFactory } from './types';
-1
View File
@@ -34,7 +34,6 @@
"clean": "backstage-cli package clean"
},
"dependencies": {
"@backstage/backend-app-api": "^0.0.0",
"@backstage/backend-plugin-api": "^0.0.0",
"@backstage/plugin-catalog-node": "^0.0.0",
"@backstage/backend-common": "^0.14.1-next.2",
@@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { loggerToWinstonLogger } from '@backstage/backend-app-api';
import {
configServiceRef,
createBackendPlugin,
databaseServiceRef,
loggerServiceRef,
loggerToWinstonLogger,
permissionsServiceRef,
urlReaderServiceRef,
httpRouterServiceRef,