Skip to content

Commit

Permalink
Removed queue batch from logger client
Browse files Browse the repository at this point in the history
  • Loading branch information
flops committed Aug 29, 2018
1 parent b712c57 commit 547b1c3
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 288 deletions.
50 changes: 22 additions & 28 deletions packages/logger/src/abstract-logger-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Stack } from '@testring/utils';
import { transport } from '@testring/transport';
import { ILogEntity, ILoggerClient, ITransport, LoggerMessageTypes, LogLevel, LogTypes } from '@testring/types';
import {
ILogEntity,
ILoggerClient,
ITransport,
LoggerMessageTypes,
LogLevel,
LogTypes,
} from '@testring/types';

const nanoid = require('nanoid');

Expand All @@ -14,19 +21,19 @@ const decomposeStepName = (stepID: string): string => {
return stepID.split(STEP_NAME_SEPARATOR)[0];
};

export abstract class AbstractLoggerClient implements ILoggerClient {
type LoggerStack = Stack<string>;
type AbstractLoggerType = ILoggerClient<ITransport, string | null, LoggerStack>;

export abstract class AbstractLoggerClient implements AbstractLoggerType {
constructor(
protected transportInstance: ITransport = transport,
protected prefix: string = '',
protected prefix: string | null = null,
protected stepStack: LoggerStack = new Stack(),
) {
}

protected abstract broadcast(messageType: string, payload: any): void;

protected stepStack: Stack<string> = new Stack();

protected logBatch: Array<ILogEntity> = [];

protected getCurrentStep(): string | null {
return this.stepStack.getLastElement();
}
Expand All @@ -46,13 +53,13 @@ export abstract class AbstractLoggerClient implements ILoggerClient {

const stepUid = logType === LogTypes.step && currentStep
? currentStep
: undefined;
: null;

const parentStep = logType === LogTypes.step
? previousStep
: currentStep;

const prefix = this.prefix || undefined;
const prefix = this.prefix || null;

return {
time,
Expand All @@ -72,23 +79,10 @@ export abstract class AbstractLoggerClient implements ILoggerClient {
): void {
const logEntry = this.buildEntry(type, content, logLevel);

if (this.getCurrentStep()) {
this.logBatch.push(logEntry);
} else {
this.broadcast(
LoggerMessageTypes.REPORT,
logEntry
);
}
}

protected sendBatchedLog(): void {
this.broadcast(
LoggerMessageTypes.REPORT_BATCH,
this.logBatch
LoggerMessageTypes.REPORT,
logEntry
);

this.logBatch = [];
}

public log(...args): void {
Expand Down Expand Up @@ -153,10 +147,6 @@ export abstract class AbstractLoggerClient implements ILoggerClient {
}
}
}

if (stepID && this.stepStack.length === 0) {
this.sendBatchedLog();
}
}

public async step(message: string, callback: () => any): Promise<void> {
Expand All @@ -170,4 +160,8 @@ export abstract class AbstractLoggerClient implements ILoggerClient {

this.endStep(message);
}

public getLogger(prefix: string | null = this.prefix, stepStack: LoggerStack = this.stepStack) {
return new (this.constructor as any)(this.transportInstance, prefix, stepStack) as this;
}
}
15 changes: 6 additions & 9 deletions packages/logger/src/logger-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export class LoggerServer extends PluggableModule implements ILoggerServer {
this.transportInstance.on(LoggerMessageTypes.REPORT, (entry: ILogEntity, processID?: string) => {
this.log(entry, processID);
});

this.transportInstance.on(LoggerMessageTypes.REPORT_BATCH, (batch: Array<ILogEntity>, processID?: string) => {
batch.forEach((entry) => this.log(entry, processID));
});
}

private async runQueue(retry: number = this.numberOfRetries): Promise<void> {
Expand Down Expand Up @@ -85,24 +81,25 @@ export class LoggerServer extends PluggableModule implements ILoggerServer {
}
}

private log(entry: ILogEntity, processID?: string): void {
private log(logEntity: ILogEntity, processID?: string): void {
// fast checking aliases
if (this.config.silent) {
return;
}

// filtering by log level
if (LogLevelNumeric[entry.logLevel] < LogLevelNumeric[this.config.logLevel]) {
if (LogLevelNumeric[logEntity.logLevel] < LogLevelNumeric[this.config.logLevel]) {
return;
}

const shouldRun = this.queue.length === 0;
const formattedMessage = formatLog(entry);
const formattedMessage = formatLog(logEntity);
const meta = processID ? { processID } : {};

this.stdout.write(`${formattedMessage}\n`);
this.queue.push({
logEntity: entry,
meta: processID ? { processID } : {},
logEntity,
meta,
});

if (shouldRun) {
Expand Down
4 changes: 3 additions & 1 deletion packages/logger/test/fixtures/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export const LOG_ENTITY: ILogEntity = {
time: new Date(),
logLevel: LogLevel.verbose,
content: ['foo', 'bar'],
parentStep: null
parentStep: null,
stepUid: null,
prefix: null,
};

0 comments on commit 547b1c3

Please sign in to comment.