Skip to content

Commit

Permalink
feat: support custom logger factory (#96)
Browse files Browse the repository at this point in the history
* feat: support custom logger factory
  • Loading branch information
mariodu authored Jan 4, 2024
1 parent 9d545d2 commit 0f04f1d
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 185 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ node_modules
.code/caches
.code/logs
.code/run
.code/*.log
.code/logs*
.nyc_output
coverage

Expand Down
7 changes: 4 additions & 3 deletions benchmark/delegate/_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ function createDelegate(filename, options, callback) {
return main;

function main(setup) {
require('#self/lib/logger').setSink(
require('#self/lib/logger').getPrettySink('benchmark.log')
);
const { Loggers, loggers } = require('#self/lib/loggers');
const sink = Loggers.getPrettySink('benchmark.log');
loggers.setSink(sink);

const {
NoslatedDelegateService: DelegateService,
} = require('#self/delegate/index');
Expand Down
27 changes: 27 additions & 0 deletions fixtures/logger_factory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const getLogger = (filename) => {
return {
info: (message) => {
console.info(`[${filename}] ${message}`);
}
}
};

class LoggerFactory {
loggers = new Map();

createLogger(filename) {
const logger = getLogger(filename);
this.loggers.set(filename, logger);
return logger;
}

get(name) {
return this.loggers.get(name);
}

close() {
console.debug('close logger factory');
}
}

module.exports = LoggerFactory;
5 changes: 5 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ export interface LoggerConfig {
* 默认为 YYYY-MM-DD HH:mm:ss.SSS
*/
timestampFormat: string;
/**
* 自定义 LoggerFactory 模块路径
* module.exports = CustomLoggerFactory;
*/
customFactoryPath?: string;
}

export interface GrpcConfig {
Expand Down
8 changes: 6 additions & 2 deletions src/control_plane/__test__/e2e/ema_scaling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ describe(common.testName(__filename), function () {
useEmaScaling: true,
workerTrafficStatsPullingMs: 1000,
},
systemCircuitBreaker: {
// 部分 CI 环境性能较差,防止触发 breaker
systemLoad1Limit: 50,
},
}),
});

Expand All @@ -40,7 +44,7 @@ describe(common.testName(__filename), function () {
},
]);

const sequence = [1, 1, 1, 5, 0, 0, 0];
const sequence = [1, 1, 1, 10, 0, 0, 0];

for (const concurrency of sequence) {
await makeConcurrencyRequest('aworker_echo_ema', concurrency, env);
Expand All @@ -52,7 +56,7 @@ describe(common.testName(__filename), function () {
});

async function request(functionName: string, env: DefaultEnvironment) {
const data = Buffer.from('100');
const data = Buffer.from('200');

const response = await env.agent.invoke(functionName, data, {
method: 'POST',
Expand Down
3 changes: 1 addition & 2 deletions src/control_plane/control_plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { EventBus } from '#self/lib/event-bus';
import { StateManager } from './worker_stats/state_manager';
import { CodeManager } from './code_manager';
import { FunctionProfileUpdateEvent } from '#self/lib/function_profile';
import { clearAllLoggers } from '@midwayjs/logger';

/**
* ControlPlane
Expand Down Expand Up @@ -79,7 +78,7 @@ export class ControlPlane extends BaseOf(EventEmitter) {
*/
async _close() {
await this._ctx.dispose();
clearAllLoggers();
loggers.close();
}

private async _onPresetFunctionProfile(event: FunctionProfileUpdateEvent) {
Expand Down
4 changes: 2 additions & 2 deletions src/control_plane/worker_stats/stat_logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import loggers from '#self/lib/logger';
import { Loggers } from '#self/lib/loggers';
import { ILogger } from '@midwayjs/logger';
import { Config } from '#self/config';
import dayjs from 'dayjs';
Expand All @@ -9,7 +9,7 @@ export class StatLogger {
private timestampFormat: string;

constructor(public config: Config) {
this.exitLogger = loggers.getPrettySink('resource_usage.log');
this.exitLogger = Loggers.getPrettySink('resource_usage.log');
this.timestampFormat = this.config.logger.timestampFormat;
}

Expand Down
3 changes: 1 addition & 2 deletions src/data_plane/data_plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DataPlaneHost } from './data_plane_host';
import { getCurrentPlaneId } from '#self/lib/util';
import { Logger, loggers } from '#self/lib/loggers';
import { DaprAdaptor } from '#self/delegate/dapr_adaptor';
import { clearAllLoggers } from '@midwayjs/logger';

export interface ConfigurableDataPlaneDeps {
config?: Config;
Expand Down Expand Up @@ -39,7 +38,7 @@ export class DataPlane extends Base {
return Promise.all([
this.dataFlowController.close(),
this.host.close(),
clearAllLoggers(),
loggers.close(),
]);
}

Expand Down
6 changes: 3 additions & 3 deletions src/data_plane/request_logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Metadata } from '#self/delegate/request_response';
import loggers from '#self/lib/logger';
import { Loggers } from '#self/lib/loggers';
import { ILogger } from '@midwayjs/logger';
import { kDefaultRequestId, kDefaultWorkerName } from '#self/lib/constants';
import { Config } from '#self/config';
Expand All @@ -11,8 +11,8 @@ export class RequestLogger {
private timestampFormat: string;

constructor(public config: Config) {
this.accessLogger = loggers.getPrettySink('access.log');
this.errorLogger = loggers.getPrettySink('error.log');
this.accessLogger = Loggers.getPrettySink('access.log');
this.errorLogger = Loggers.getPrettySink('error.log');
this.timestampFormat = this.config.logger.timestampFormat;
}

Expand Down
9 changes: 4 additions & 5 deletions src/delegate/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventEmitter from 'events';
import path from 'path';
import { Meter, createNoopMeter } from '@opentelemetry/api';
import Logger from '../lib/logger';
import loggers from '../lib/logger';
import { aworker } from '../proto/aworker';
import { CredentialRegistration } from './registration';
import { DefaultNamespaceResolver, NamespaceResolver } from './namespace';
Expand All @@ -13,9 +13,8 @@ import { NoslatedServer } from './noslated_ipc';
import { Readable } from 'stream';
import { Metadata, MetadataInit } from './request_response';
import { DaprAdaptor } from './dapr_adaptor';
import { clearAllLoggers } from '@midwayjs/logger';

const logger = Logger.get('delegate');
const logger = loggers.get('delegate');

const { CredentialTargetType } = aworker.ipc;

Expand Down Expand Up @@ -267,7 +266,7 @@ export class NoslatedDelegateService extends EventEmitter {

const server = new NoslatedServer(
this.#sharedState.serverPath,
Logger.get('noslated server')
loggers.get('noslated server')
);

server.onRequest = this.#onRequest;
Expand Down Expand Up @@ -297,7 +296,7 @@ export class NoslatedDelegateService extends EventEmitter {
}
});

clearAllLoggers();
loggers.close();
}

/**
Expand Down
Loading

0 comments on commit 0f04f1d

Please sign in to comment.