Skip to content

Commit

Permalink
Merge pull request #29 from ralphv/rv_add_payload_to_logger
Browse files Browse the repository at this point in the history
add context for logger with payload
  • Loading branch information
ralphv authored Sep 26, 2024
2 parents 7469542 + 1786008 commit 1e3a02c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 22 deletions.
15 changes: 12 additions & 3 deletions src/GallifreyRulesEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export class GallifreyRulesEngine {
asyncActionEvent.eventName,
asyncActionEvent.eventId,
source,
asyncActionEvent.payload,
);

try {
Expand Down Expand Up @@ -228,6 +229,7 @@ export class GallifreyRulesEngine {
scheduledEvent.event.eventName,
scheduledEvent.event.eventId,
scheduledEvent.event.source,
scheduledEvent.event.payload,
);

await this.getLogger().info(
Expand Down Expand Up @@ -276,8 +278,8 @@ export class GallifreyRulesEngine {
...event,
namespace: this.getNamespace(),
} as GallifreyEventTypeInternal<EventPayloadType>;
const { entityName, eventName, eventId, eventLag, source } = internalEvent;
const engineEventContext = await this.createEngineEventContext(entityName, eventName, eventId, source);
const { entityName, eventName, eventId, eventLag, source, payload } = internalEvent;
const engineEventContext = await this.createEngineEventContext(entityName, eventName, eventId, source, payload);
await this.getLogger().info(
engineEventContext,
`handleEvent [START: ${eventId}]: ${JSON.stringify({
Expand Down Expand Up @@ -1353,7 +1355,13 @@ export class GallifreyRulesEngine {
console.log(colors.yellow(textSync('Gallifrey Rules', { horizontalLayout: 'full' })), EOL);
}

protected async createEngineEventContext(entityName: string, eventName: string, eventId: string, source: string) {
protected async createEngineEventContext(
entityName: string,
eventName: string,
eventId: string,
source: string,
payload: any,
) {
const engineEventContext = new EngineEventContext(
AssertNotNull(this.getNamespace()),
entityName,
Expand All @@ -1362,6 +1370,7 @@ export class GallifreyRulesEngine {
source,
this.getEventLevelConfig(entityName, eventName),
this.getLogger(),
payload,
);
const configAccessor = await this.providersContext?.configuration?.getConfigurationAccessorInterface(
engineEventContext,
Expand Down
11 changes: 10 additions & 1 deletion src/GallifreyRulesEngineForTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ export class GallifreyRulesEngineForTesting extends GallifreyRulesEngine {
eventName: string,
eventId: string,
source: string,
payload: any,
): Promise<EngineEventContext> {
this.lastEngineCreateContext = await super.createEngineEventContext(entityName, eventName, eventId, source);
this.lastEngineCreateContext = await super.createEngineEventContext(
entityName,
eventName,
eventId,
source,
payload,
);
return this.lastEngineCreateContext;
}

Expand Down Expand Up @@ -102,6 +109,7 @@ export class GallifreyRulesEngineForTesting extends GallifreyRulesEngine {
internalEvent.eventName,
event.eventId,
event.source,
event.payload,
);
return await this.doAction<ActionPayloadType, ActionResponseType>(
internalEvent,
Expand Down Expand Up @@ -135,6 +143,7 @@ export class GallifreyRulesEngineForTesting extends GallifreyRulesEngine {
internalEvent.eventName,
event.eventId,
event.source,
event.payload,
);
return await this.pullDataObject<DataObjectRequestType, DataObjectResponseType>(
internalEvent,
Expand Down
15 changes: 15 additions & 0 deletions src/engine-interfaces/EngineFullEventContextInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* author: Ralph Varjabedian
*/

import { EngineEventContextInterface } from './index';

/**
* Answers basic questions about the context of the current event and data related to it including the payload
*/
export default interface EngineFullEventContextInterface extends EngineEventContextInterface {
/**
* Payload
*/
getPayload(): any;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export {
default as TestingJournalLoggerProvider,
TestingJournalLoggerProviderMethods,
} from './testing-modules/TestingJournalLoggerProvider';
export { GallifreyRulesEngineConsumerInterface } from './consumers/GallifreyRulesEngineConsumerInterface';
export { GallifreyRulesEngineConsumerInterface } from './consumers/GallifreyRulesEngineConsumerInterface';
18 changes: 9 additions & 9 deletions src/interfaces/Providers/LoggerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
* author: Ralph Varjabedian
*/
import ModuleInterface from '../../base-interfaces/ModuleInterface';
import EngineEventContextInterface from '../../engine-interfaces/EngineEventContextInterface';
import EngineFullEventContextInterface from '../../engine-interfaces/EngineFullEventContextInterface';

export default interface LoggerInterface extends ModuleInterface {
debug(context: EngineEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
info(context: EngineEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
warn(context: EngineEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
error(context: EngineEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
debug(context: EngineFullEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
info(context: EngineFullEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
warn(context: EngineFullEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
error(context: EngineFullEventContextInterface | undefined, message: string, payload?: any): Promise<void>;
}

export class __LoggerInterface implements LoggerInterface {
debug(context: EngineEventContextInterface | undefined, message: string, payload: any): Promise<void> {
debug(context: EngineFullEventContextInterface | undefined, message: string, payload: any): Promise<void> {
return Promise.reject('un-callable code');
}

error(context: EngineEventContextInterface | undefined, message: string, payload: any): Promise<void> {
error(context: EngineFullEventContextInterface | undefined, message: string, payload: any): Promise<void> {
return Promise.reject('un-callable code');
}

info(context: EngineEventContextInterface | undefined, message: string, payload: any): Promise<void> {
info(context: EngineFullEventContextInterface | undefined, message: string, payload: any): Promise<void> {
return Promise.reject('un-callable code');
}

warn(context: EngineEventContextInterface | undefined, message: string, payload: any): Promise<void> {
warn(context: EngineFullEventContextInterface | undefined, message: string, payload: any): Promise<void> {
return Promise.reject('un-callable code');
}
}
11 changes: 8 additions & 3 deletions src/lib/EngineEventContext.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import EngineEventContextInterface from '../engine-interfaces/EngineEventContextInterface';
import { ScheduledEventType } from '../engine-events/ScheduledEventType';
import { AssertNotNull } from './Utils';
import Config from './Config';
import SafeJournalLoggerWrapper from '../SafeJournalLoggerWrapper';
import { LoggerInterface } from '../interfaces/Providers';
import EngineFullEventContextInterface from '../engine-interfaces/EngineFullEventContextInterface';

export class EngineEventContext implements EngineEventContextInterface {
export class EngineEventContext implements EngineFullEventContextInterface {
private eventStore: { [key: string]: any } = {};
private scheduledEvent: ScheduledEventType | undefined;
private journalLogger: SafeJournalLoggerWrapper | undefined;
Expand All @@ -18,6 +18,7 @@ export class EngineEventContext implements EngineEventContextInterface {
private readonly source: string,
private readonly eventLevelConfig: Config,
private readonly logger: LoggerInterface,
private readonly payload: any,
) {}

getEntityName(): string {
Expand All @@ -44,7 +45,7 @@ export class EngineEventContext implements EngineEventContextInterface {
this.journalLogger = journalLogger;
}

async addToEventStore(context: EngineEventContextInterface, key: string, value: any) {
async addToEventStore(context: EngineFullEventContextInterface, key: string, value: any) {
if (this.isInEventStore(key)) {
await this.logger.warn(context, `Key already exists in eventStore: ${key}`);
}
Expand Down Expand Up @@ -77,4 +78,8 @@ export class EngineEventContext implements EngineEventContextInterface {
getEventLevelConfig(): Config {
return this.eventLevelConfig;
}

getPayload(): string {
return this.payload;
}
}
10 changes: 5 additions & 5 deletions src/modules/ConsoleLoggerProvider.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { GallifreyProvider, ProviderType } from '../interfaces/InterfaceDecorators';
import { LoggerInterface } from '../interfaces/Providers';
import { logger } from '../lib/logger';
import EngineEventContextInterface from '../engine-interfaces/EngineEventContextInterface';
import EngineFullEventContextInterface from '../engine-interfaces/EngineFullEventContextInterface';

@GallifreyProvider(ProviderType.Logger)
export default class ConsoleLoggerProvider implements LoggerInterface {
async debug(context: EngineEventContextInterface, message: string, payload?: any): Promise<void> {
async debug(context: EngineFullEventContextInterface, message: string, payload?: any): Promise<void> {
logger.debug(message, payload);
}

async error(context: EngineEventContextInterface, message: string, payload?: any): Promise<void> {
async error(context: EngineFullEventContextInterface, message: string, payload?: any): Promise<void> {
logger.error(message, payload);
}

async info(context: EngineEventContextInterface, message: string, payload?: any): Promise<void> {
async info(context: EngineFullEventContextInterface, message: string, payload?: any): Promise<void> {
logger.info(message, payload);
}

async warn(context: EngineEventContextInterface, message: string, payload?: any): Promise<void> {
async warn(context: EngineFullEventContextInterface, message: string, payload?: any): Promise<void> {
logger.warn(message, payload);
}
}

0 comments on commit 1e3a02c

Please sign in to comment.