Skip to content

Commit

Permalink
Merge pull request #53 from Enigmatis/polaris-logs-export-create-logger
Browse files Browse the repository at this point in the history
refactor!: changes according to api changes
  • Loading branch information
chenshoo authored Feb 10, 2020
2 parents 9af242c + 36a8b4a commit a880a17
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 37 deletions.
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"@enigmatis/polaris-common": "^1.0.10",
"@types/serialize-error": "^2.1.0",
"@types/winston": "^2.4.4",
"@types/uuid": "^3.4.7",
"clean-deep": "^3.2.0",
"uuid": "^3.4.0",
"serialize-error": "^3.0.0",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^3.10.0",
Expand Down
4 changes: 4 additions & 0 deletions src/entities/entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Reality } from './reality';

export interface Entity {
id?: string;
type?: string;
reality?: Reality;
name?: string;
secondaryIds?: string[];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface EventKindDescriptionLogProperty {
export interface EventKindDescription {
systemId?: string;
requestingSystemId?: string;
}
8 changes: 4 additions & 4 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './event-kind-description-log-property';
export * from './reality-log-property';
export * from './request-log-property';
export * from './system-log-property';
export * from './event-kind-description';
export * from './reality';
export * from './request';
export * from './requesting-system';
export * from './entity';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface RealityLogProperty {
export interface Reality {
type?: string;
id?: number;
name?: string;
Expand Down
8 changes: 0 additions & 8 deletions src/entities/request-log-property.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/entities/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RequestingSystem } from './requesting-system';

export interface Request {
requestingIp?: string;
requestingUserIdentifier?: string;
requestingSystem?: RequestingSystem;
requestQuery?: any;
requestingHost?: string;
request?: any;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface SystemLogProperty {
export interface RequestingSystem {
name?: string;
id?: string;
}
20 changes: 8 additions & 12 deletions src/polaris-log-properties.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import {
Entity,
EventKindDescriptionLogProperty,
RealityLogProperty,
RequestLogProperty,
} from './entities';
import { Entity, EventKindDescription, Reality, Request } from './entities';

export interface PolarisLogProperties {
throwable?: object;
elapsedTime?: number;
logId?: string;
customProperties?: object;
requestId?: string;
response?: object;
recordId?: string;
messageId?: string;
eventKind?: string;
eventKindDescription?: EventKindDescriptionLogProperty;
reality?: RealityLogProperty;
request?: RequestLogProperty;
eventKindDescription?: EventKindDescription;
reality?: Reality;
request?: Request;
entity?: Entity;
entities?: Entity[];
upn?: string;
ip?: string;
host?: string;
}
15 changes: 11 additions & 4 deletions src/polaris-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { Logger } from 'winston';
import { LoggerConfiguration } from './configurations/logger-configuration';
import { PolarisLogProperties } from './polaris-log-properties';
import { createLogger } from './winston-logger';
const uuidv1 = require('uuid/v1');

export class PolarisLogger {
private static getAppPropertiesToAssign(applicationProperties?: ApplicationProperties) {
if (applicationProperties) {
return {
systemId: applicationProperties.id,
systemName: applicationProperties.name,
eventKindDescription: {
systemId: applicationProperties.id,
},
version: applicationProperties.version,
environment: applicationProperties.environment,
component: applicationProperties.component,
Expand Down Expand Up @@ -56,12 +54,21 @@ export class PolarisLogger {
}

private buildLog(message: string, polarisLogProperties?: PolarisLogProperties) {
const eventKindDescription =
this.applicationLogProperties?.id || polarisLogProperties?.request?.requestingSystem?.id
? {
systemId: this.applicationLogProperties?.id,
requestingSystemId: polarisLogProperties?.request?.requestingSystem?.id,
}
: undefined;
const messageId = polarisLogProperties?.messageId || uuidv1();
const propertiesWithCustom = {
message,
...(polarisLogProperties && polarisLogProperties.customProperties),
...polarisLogProperties,
...PolarisLogger.getAppPropertiesToAssign(this.applicationLogProperties),
customProperties: undefined,
eventKindDescription,
messageId,
throwable:
polarisLogProperties &&
polarisLogProperties.throwable &&
Expand Down
2 changes: 1 addition & 1 deletion src/type-declarations/clean-deep.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare function clean(toClean: object): object;

declare module 'clean-deep' {
export = clean
export = clean;
}
20 changes: 19 additions & 1 deletion test/polaris-logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid = require('uuid/v1');
import { Logger } from 'winston';
import { LoggerConfiguration } from '../src/configurations/logger-configuration';
import { ApplicationProperties } from '../src/main';
Expand Down Expand Up @@ -34,7 +35,7 @@ describe('polaris-logger tests', () => {
const message: string = 'log message';

test('creating a polaris logger with application properties and configuration - winston createLogger was called with configuration', () => {
const logger = new PolarisLogger(config, appProps);
new PolarisLogger(config, appProps);

expect(createLogger).toHaveBeenCalledWith(config);
});
Expand All @@ -44,6 +45,17 @@ describe('polaris-logger tests', () => {
logger.fatal(message);
expect(loggerImplMock.fatal).toHaveBeenCalledWith({
message,
messageId: expect.anything(),
});
});

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

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

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

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

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

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

Expand All @@ -98,6 +115,7 @@ describe('polaris-logger tests', () => {
eventKindDescription: { systemId: appProps.id },
systemId: appProps.id,
systemName: appProps.name,
messageId: expect.anything(),
}),
);
});
Expand Down
7 changes: 5 additions & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
true,
"never-prefix"
],
"object-literal-sort-keys": false
"object-literal-sort-keys": false,
"no-var-requires": false,
"no-submodule-imports": false,
"no-unused-expression": false
}
}
}

0 comments on commit a880a17

Please sign in to comment.