Merge pull request #15245 from backstage/mob/log
backend-plugin-api: update logger interface with more methods and meta
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/backend-app-api': patch
|
||||
---
|
||||
|
||||
Updated logger implementations to match interface changes.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/backend-plugin-api': patch
|
||||
---
|
||||
|
||||
Updated `LoggerService` interface with more log methods and meta.
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
LoggerService,
|
||||
coreServices,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { LogMeta } from '@backstage/backend-plugin-api';
|
||||
import { Logger as WinstonLogger } from 'winston';
|
||||
|
||||
class BackstageLogger implements LoggerService {
|
||||
@@ -29,12 +30,24 @@ class BackstageLogger implements LoggerService {
|
||||
|
||||
private constructor(private readonly winston: WinstonLogger) {}
|
||||
|
||||
info(message: string, ...meta: any[]): void {
|
||||
this.winston.info(message, ...meta);
|
||||
error(message: string, meta?: LogMeta): void {
|
||||
this.winston.error(message, meta);
|
||||
}
|
||||
|
||||
child(fields: { [name: string]: string }): LoggerService {
|
||||
return new BackstageLogger(this.winston.child(fields));
|
||||
warn(message: string, meta?: LogMeta): void {
|
||||
this.winston.warn(message, meta);
|
||||
}
|
||||
|
||||
info(message: string, meta?: LogMeta): void {
|
||||
this.winston.info(message, meta);
|
||||
}
|
||||
|
||||
debug(message: string, meta?: LogMeta): void {
|
||||
this.winston.debug(message, meta);
|
||||
}
|
||||
|
||||
child(meta: LogMeta): LoggerService {
|
||||
return new BackstageLogger(this.winston.child(meta));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -205,9 +205,15 @@ export type LifecycleServiceShutdownHook = {
|
||||
// @public (undocumented)
|
||||
export interface LoggerService {
|
||||
// (undocumented)
|
||||
child(fields: { [name: string]: string }): LoggerService;
|
||||
child(meta: LogMeta): LoggerService;
|
||||
// (undocumented)
|
||||
info(message: string): void;
|
||||
debug(message: string, meta?: Error | LogMeta): void;
|
||||
// (undocumented)
|
||||
error(message: string, meta?: Error | LogMeta): void;
|
||||
// (undocumented)
|
||||
info(message: string, meta?: Error | LogMeta): void;
|
||||
// (undocumented)
|
||||
warn(message: string, meta?: Error | LogMeta): void;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
@@ -219,6 +225,11 @@ export function loggerToWinstonLogger(
|
||||
opts?: TransportStreamOptions,
|
||||
): Logger;
|
||||
|
||||
// @public (undocumented)
|
||||
export type LogMeta = {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export type PermissionsService = PermissionEvaluator | PermissionAuthorizer;
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ export type {
|
||||
LifecycleService,
|
||||
LifecycleServiceShutdownHook,
|
||||
} from './lifecycleServiceRef';
|
||||
export type { LoggerService } from './loggerServiceRef';
|
||||
export type { LoggerService, LogMeta } from './loggerServiceRef';
|
||||
export type { PermissionsService } from './permissionsServiceRef';
|
||||
export type { PluginMetadataService } from './pluginMetadataServiceRef';
|
||||
export type { RootLoggerService } from './rootLoggerServiceRef';
|
||||
|
||||
@@ -16,12 +16,21 @@
|
||||
|
||||
import { createServiceRef } from '../system/types';
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export type LogMeta = { [name: string]: unknown };
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface LoggerService {
|
||||
info(message: string): void;
|
||||
child(fields: { [name: string]: string }): LoggerService;
|
||||
error(message: string, meta?: Error | LogMeta): void;
|
||||
warn(message: string, meta?: Error | LogMeta): void;
|
||||
info(message: string, meta?: Error | LogMeta): void;
|
||||
debug(message: string, meta?: Error | LogMeta): void;
|
||||
|
||||
child(meta: LogMeta): LoggerService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,9 +26,28 @@ class BackstageLoggerTransport extends Transport {
|
||||
super(opts);
|
||||
}
|
||||
|
||||
log(info: { message: string }, callback: VoidFunction) {
|
||||
// TODO: add support for levels and fields
|
||||
this.backstageLogger.info(info.message);
|
||||
log(info: unknown, callback: VoidFunction) {
|
||||
if (typeof info !== 'object' || info === null) {
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
const { level, message, ...meta } = info as { [name: string]: unknown };
|
||||
switch (level) {
|
||||
case 'error':
|
||||
this.backstageLogger.error(String(message), meta);
|
||||
break;
|
||||
case 'warn':
|
||||
this.backstageLogger.warn(String(message), meta);
|
||||
break;
|
||||
case 'info':
|
||||
this.backstageLogger.info(String(message), meta);
|
||||
break;
|
||||
case 'debug':
|
||||
this.backstageLogger.debug(String(message), meta);
|
||||
break;
|
||||
default:
|
||||
this.backstageLogger.info(String(message), meta);
|
||||
}
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user