Skip to content

Commit

Permalink
fix: Schema validator fixes (#75)
Browse files Browse the repository at this point in the history
* fix: updated logs according to log standards
fixed existing tests according to the changes
added a function that creates EventKindDescription
added a description and example to README.md about dynamic log transports

* refactor: remove npm run from package.json

* test: add tests for polaris logs according to changes in the log properties

* refactor: cr fixes

* style: remove unused import
  • Loading branch information
ronkatz96 authored Sep 3, 2020
1 parent 9982b30 commit 2c5836b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 28 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Through this interface you should set the following configuration to the `Polari
- **loggerLevel** (_LoggerLevel_) - The level the logger is listening on, can be one of the following levels: `fatal` /
`error` / `warn` / `info` / `debug` / `trace`.
- **logstashConfigurations** (_LogstashConfiguration[] - optional_) - Through this property you can set multiple logstash
hosts, ports and protocols (**Notice that you can use either TCP or UDP for each logstash config**).
hosts, ports and protocols (**Notice that you can use `TCP`/`UDP` or `DYNAMIC` for each logstash config**).
Use `DYNAMIC` to make polaris-logs decide which protocol to use according to the size of the log.
- **writeToConsole** (_boolean - optional_) - Determines if the logger should write the logs to the console.
- **writeFullMessageToConsole** (_boolean - optional_) - Set this property to `true`, if you decide to write full
detailed logs to the console, since only the `timestamp` accompanied by the `log level`, `message` and
Expand Down Expand Up @@ -87,6 +88,11 @@ const logstashConf: LogstashConfiguration[] = [
port: 6000,
protocol: LogstashProtocol.UDP,
},
{
host: '127.0.0.1',
port: 3000,
protocol: LogstashProtocol.DYNAMIC,
}
];

const logConf: LoggerConfiguration = {
Expand Down
35 changes: 26 additions & 9 deletions src/abstract-polaris-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LoggerConfiguration } from './configurations/logger-configuration';
import { Logger } from './logger-with-custom-levels';
import { PolarisLogProperties } from './polaris-log-properties';
import { createLogger } from './winston-logger';
import { EventKindDescription } from './entities';

const cleanDeep = require('clean-deep');

Expand Down Expand Up @@ -43,14 +44,9 @@ export abstract class AbstractPolarisLogger {
public abstract trace(message: string, ...args: any[]): void;

protected buildLog(message: string, polarisLogProperties?: PolarisLogProperties): any {
const eventKindDescription =
this.applicationLogProperties?.id || polarisLogProperties?.request?.requestingSystem?.id
? {
systemId: this.applicationLogProperties?.id,
requestingSystemId: polarisLogProperties?.request?.requestingSystem?.id,
}
: undefined;
const messageId = polarisLogProperties?.messageId || uuidv4();
const eventKindDescription = this.getEventKindDescription(polarisLogProperties);
this.setEntityOrEntities(polarisLogProperties);
const recordId = polarisLogProperties?.recordId || uuidv4();

const propertiesWithCustom = {
message,
Expand All @@ -59,12 +55,33 @@ export abstract class AbstractPolarisLogger {
customProperties: undefined, // in order for it to be removed, so it won't be a duplicate
...AbstractPolarisLogger.getAppPropertiesToAssign(this.applicationLogProperties),
eventKindDescription,
messageId,
recordId,
throwable:
polarisLogProperties &&
polarisLogProperties.throwable &&
serializeError(polarisLogProperties.throwable),
};
return cleanDeep(propertiesWithCustom);
}

private setEntityOrEntities(polarisLogProperties?: PolarisLogProperties): void {
if (polarisLogProperties?.entities && polarisLogProperties?.entity) {
if (!polarisLogProperties.entities.includes(polarisLogProperties.entity)) {
polarisLogProperties.entities.push(polarisLogProperties.entity);
}
polarisLogProperties.entity = undefined;
}
}

private getEventKindDescription(
polarisLogProperties?: PolarisLogProperties,
): EventKindDescription | undefined {
return this.applicationLogProperties?.id ||
polarisLogProperties?.request?.requestingSystem?.id
? {
systemId: this.applicationLogProperties?.id,
requestingSystemId: polarisLogProperties?.request?.requestingSystem?.id,
}
: undefined;
}
}
1 change: 0 additions & 1 deletion src/entities/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Reality } from './reality';

export interface Entity {
id?: string;
type?: string;
reality?: Reality;
name?: string;
secondaryIds?: string[];
Expand Down
2 changes: 1 addition & 1 deletion src/entities/reality.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Reality {
type?: string;
id?: number;
id?: string;
name?: string;
}
3 changes: 2 additions & 1 deletion src/polaris-log-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ export interface PolarisLogProperties {
logId?: string;
customProperties?: any;
response?: any;
messageId?: string;
recordId?: string;
eventKind?: string;
eventKindDescription?: EventKindDescription;
reality?: Reality;
request?: Request;
entity?: Entity;
entities?: Entity[];
upn?: string;
ip?: string;
host?: string;
Expand Down
9 changes: 5 additions & 4 deletions src/winston-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { DynamicLogstashTransport } from './transports/dynamic-logstash-transpor
import { LogstashTransport } from 'winston-logstash-ts';
import { LogstashProtocol } from './configurations/logstash-protocol';

const timestampFormat = 'DD-MM-YYYY HH:mm:ss';
const consoleTimestampFormat = 'DD-MM-YYYY HH:mm:ss';
const logstashTimestampFormat = 'YYYY-MM-DD HH:mm:ssZ';

const consoleFullFormat = winston.format.combine(
winston.format.timestamp({ format: timestampFormat }),
winston.format.timestamp({ format: consoleTimestampFormat }),
winston.format.align(),
winston.format.printf((info) => {
const { timestamp, level, message, ...args } = info;
Expand All @@ -20,7 +21,7 @@ const consoleFullFormat = winston.format.combine(
);

const consoleShortFormat = winston.format.combine(
winston.format.timestamp({ format: timestampFormat }),
winston.format.timestamp({ format: consoleTimestampFormat }),
winston.format.align(),
winston.format.printf((info) => {
const { timestamp, level, message, throwable } = info;
Expand All @@ -31,7 +32,7 @@ const consoleShortFormat = winston.format.combine(
);

const logstashFormat = winston.format.combine(
winston.format.timestamp({ format: timestampFormat }),
winston.format.timestamp({ format: logstashTimestampFormat }),
winston.format.printf((info) => {
return JSON.stringify(info);
}),
Expand Down
69 changes: 58 additions & 11 deletions test/polaris-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ describe('polaris-logger tests', () => {
logger.fatal(message);
expect(loggerImplMock.fatal).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

test('fatal - logging message & messagId - message & messageId are in the log', () => {
const logger = new PolarisLogger(config);
const messageId = uuidv4();
logger.fatal(message, { messageId });
const recordId = uuidv4();
logger.fatal(message, { recordId });
expect(loggerImplMock.fatal).toHaveBeenCalledWith({
message,
messageId,
recordId,
});
});

Expand All @@ -64,7 +64,7 @@ describe('polaris-logger tests', () => {
logger.error(message);
expect(loggerImplMock.error).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

Expand All @@ -73,7 +73,7 @@ describe('polaris-logger tests', () => {
logger.warn(message);
expect(loggerImplMock.warn).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

Expand All @@ -82,7 +82,7 @@ describe('polaris-logger tests', () => {
logger.info(message);
expect(loggerImplMock.info).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

Expand All @@ -91,7 +91,7 @@ describe('polaris-logger tests', () => {
logger.debug(message);
expect(loggerImplMock.debug).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

Expand All @@ -100,7 +100,7 @@ describe('polaris-logger tests', () => {
logger.trace(message);
expect(loggerImplMock.trace).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

Expand All @@ -115,7 +115,7 @@ describe('polaris-logger tests', () => {
eventKindDescription: { systemId: appProps.id },
systemId: appProps.id,
systemName: appProps.name,
messageId: expect.anything(),
recordId: expect.anything(),
}),
);
});
Expand All @@ -136,7 +136,54 @@ describe('polaris-logger tests', () => {
systemId: appProps.id,
systemName: appProps.name,
message,
messageId: expect.anything(),
recordId: expect.anything(),
});
});

test('info - logging message with entity in options - entity is in the log', () => {
const logger = new PolarisLogger(config, appProps);
logger.info(message, {
entity: {
id: '0',
},
});
expect(loggerImplMock.info).toHaveBeenCalledWith(
expect.objectContaining({
message,
recordId: expect.anything(),
entity: {
id: '0',
},
}),
);
});

test('info - logging message with entities in options - entities is in the log', () => {
const logger = new PolarisLogger(config, appProps);
logger.info(message, {
entities: [{ id: '0' }, { id: '1' }, { id: '2' }],
});
expect(loggerImplMock.info).toHaveBeenCalledWith(
expect.objectContaining({
message,
recordId: expect.anything(),
entities: [{ id: '0' }, { id: '1' }, { id: '2' }],
}),
);
});

test('info - logging message with entity and entities in options - entities is in the log', () => {
const logger = new PolarisLogger(config, appProps);
logger.info(message, {
entity: { id: '2' },
entities: [{ id: '0' }, { id: '1' }],
});
expect(loggerImplMock.info).toHaveBeenCalledWith(
expect.objectContaining({
message,
recordId: expect.anything(),
entities: [{ id: '0' }, { id: '1' }, { id: '2' }],
}),
);
});
});

0 comments on commit 2c5836b

Please sign in to comment.