Skip to content

Commit

Permalink
fix: use Logger interface instead of Console (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Sep 14, 2023
1 parent 7436eb0 commit fd6ea43
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/client/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import { debuglog } from 'node:util';
import { AsyncLocalStorage } from 'node:async_hooks';
import { Metadata } from '@grpc/grpc-js';
import { KV, RequestWithMeta, Map } from '../types/common';
import { KV, RequestWithMeta, Map, Logger } from '../types/common';
import { mergeMetadataToMap } from '../utils';

const debug = debuglog('layotto:client:api');

export type CreateMetadataHook = (localStorage?: AsyncLocalStorage<any>) => Record<string, string>;

export interface APIOptions {
logger?: Console;
logger?: Logger;
localStorage?: AsyncLocalStorage<any>;
/**
* Setting more request metadata here, e.g.: tracing headers
Expand All @@ -34,7 +34,7 @@ export interface APIOptions {

export class API {
protected readonly localStorage?: AsyncLocalStorage<any>;
protected readonly logger: Console;
protected readonly logger: Logger;
#createMetadataHook?: CreateMetadataHook;

constructor(options?: APIOptions) {
Expand Down
5 changes: 3 additions & 2 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ import { Binding } from './Binding';
import { Oss, OssOptions } from './Oss';
import { Cryption, CryptionOptions } from './Cryption';
import type { CreateMetadataHook } from './API';
import { Logger } from '../types/common';

const debug = debuglog('layotto:client:main');

export interface ClientOptions {
ossEnable?: boolean;
oss?: OssOptions;
cryption?: CryptionOptions;
logger?: Console;
logger?: Logger;
localStorage?: AsyncLocalStorage<any>;
createMetadataHook?: CreateMetadataHook;
}
Expand All @@ -47,7 +48,7 @@ export class Client {
readonly host: string;
readonly port: string;
protected readonly localStorage?: AsyncLocalStorage<any>;
protected readonly logger: Console;
protected readonly logger: Logger;
protected readonly createMetadataHook?: CreateMetadataHook;
protected readonly _runtime: RuntimeClient;
private readonly _address: string;
Expand Down
5 changes: 3 additions & 2 deletions src/server/GRPCServerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import {
} from '../../proto/runtime/v1/appcallback_pb';
import { PubSubCallback, TopicEventRequest } from '../types/PubSub';
import { convertMapToKVString, mergeMetadataToMap } from '../utils';
import { Logger } from '../types/common';

const debug = debuglog('layotto:server:grpc');

export interface GRPCServerOptions {
logger?: Console;
logger?: Logger;
localStorage?: AsyncLocalStorage<any>;
}

Expand All @@ -40,7 +41,7 @@ export class GRPCServerImpl implements IAppCallbackServer {
protected readonly handlersTopics: Record<string, PubSubCallback> = {};
protected readonly subscriptionsList: TopicSubscription[] = [];
protected readonly localStorage?: AsyncLocalStorage<any>;
protected readonly logger: Console;
protected readonly logger: Logger;

constructor(options?: GRPCServerOptions) {
this.logger = options?.logger ?? global.console;
Expand Down
5 changes: 3 additions & 2 deletions src/server/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ import { ServerCredentials, Server as GRPCServer } from '@grpc/grpc-js';
import { AppCallbackService } from '../../proto/runtime/v1/appcallback_grpc_pb';
import { GRPCServerImpl } from './GRPCServerImpl';
import { PubSub } from './PubSub';
import { Logger } from '../types/common';

const debug = debuglog('layotto:server:main');

export interface ServerOptions {
logger?: Console;
logger?: Logger;
localStorage?: AsyncLocalStorage<any>;
}

export class Server {
readonly port: string;
readonly pubsub: PubSub;
protected readonly localStorage?: AsyncLocalStorage<any>;
protected readonly logger: Console;
protected readonly logger: Logger;
private readonly _serverImpl: GRPCServerImpl;
private readonly _server: GRPCServer;
#start = false;
Expand Down
6 changes: 6 additions & 0 deletions src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ export function convertArrayToKVString(items: [string, string][]) {
}
return kv;
}

export interface Logger {
info(message?: any, ...optionalParams: any[]): void;
warn(message?: any, ...optionalParams: any[]): void;
error(message?: any, ...optionalParams: any[]): void;
}

0 comments on commit fd6ea43

Please sign in to comment.