Skip to content

Commit

Permalink
fix: throwable is now serialized appropriately, made logstash optional
Browse files Browse the repository at this point in the history
  • Loading branch information
osher-sade committed Feb 14, 2019
1 parent fc51d06 commit ea8ffe6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 44 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Through this interface you should set the following configuration to the ``Polar

+ **loggerLevel** (*string*) - The level the logger is listening on, can be one of the following levels: ``fatal`` /
``error`` / ``warn`` / ``info`` / ``trace`` / ``debug``
+ **logstashHost** (*string*)
+ **logstashPort** (*number*)
+ **logstashConfiguration** (*LogstashConfiguration - optional*) - Through this property you can set your logstashHost
and logstashPort (**Notice that we use UDP to write logs to the logstash service**).
+ **writeToConsole** (*boolean - optional*) - Determines if the logger should write the logs to the console.
+ **writeFullMessageToConsole** (*boolean - optional*) - If you do decide to write logs to console, only the timestamp
accompanied by the log level and message will be written, in order to see the whole log, which includes the properties
that would be sent to the logstash, set this property to ``true``.
+ **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
``throwable`` will be written by default.
+ **logFilePath** (*string - optional*) - If provided, the logs will be written to the specified path.
+ **dailyRotateFileConfiguration** (*DailyRotateFileConfiguration - optional*) - If you are interested in daily log file
instead of just **one** log file, see the configuration section below. It creates a log file for each day. Those daily
Expand Down Expand Up @@ -57,13 +57,17 @@ const appProps: ApplicationLogProperties = {
component: 'component',
};

const logConf: LoggerConfiguration = {
loggerLevel: 'trace',
const logstashConf = {
logstashHost: '127.0.0.1',
logstashPort: 3000,
};

const logConf: LoggerConfiguration = {
loggerLevel: 'trace',
logstashConfiguration: logstashConf,
writeToConsole: true,
writeFullMessageToConsole: true,
// logFilePath: 'D:\\example.log',
writeFullMessageToConsole: false,
// logFilePath: 'D:\\example.txt',
dailyRotateFileConfiguration: {
directoryPath: 'D:\\',
fileNamePrefix: 'polaris',
Expand Down
16 changes: 13 additions & 3 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,9 +65,11 @@
"homepage": "https://github.com/Enigmatis/polaris-logs#readme",
"dependencies": {
"@types/node": "10.12.12",
"@types/serialize-error": "^2.1.0",
"@types/winston": "^2.4.4",
"clean-deep": "^3.0.2",
"rimraf": "^2.6.2",
"serialize-error": "^3.0.0",
"winston": "^3.1.0",
"winston-daily-rotate-file": "^3.6.0",
"winston-logstash-transport": "^2.0.0"
Expand Down
4 changes: 2 additions & 2 deletions src/configurations/logger-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DailyRotateFileConfiguration } from './daily-rotate-file-configuration';
import { LogstashConfiguration } from './logstash-configuration';

export interface LoggerConfiguration {
loggerLevel: string;
logstashHost: string;
logstashPort: number;
logstashConfiguration?: LogstashConfiguration;
writeToConsole?: boolean;
writeFullMessageToConsole?: boolean;
logFilePath?: string;
Expand Down
4 changes: 4 additions & 0 deletions src/configurations/logstash-configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface LogstashConfiguration {
logstashHost: string;
logstashPort: number;
}
2 changes: 2 additions & 0 deletions src/polaris-logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cleanDeep = require('clean-deep');
import * as serializeError from 'serialize-error';
import { Logger } from 'winston';
import { LoggerConfiguration } from './configurations/logger-configuration';
import { ApplicationLogProperties } from './entities';
Expand Down Expand Up @@ -61,6 +62,7 @@ export class PolarisLogger {
...polarisLogProperties,
...PolarisLogger.getAppPropertiesToAssign(this.applicationLogProperties),
customProperties: undefined,
throwable: serializeError(polarisLogProperties && polarisLogProperties.throwable),
};

return cleanDeep(propertiesWithCustom);
Expand Down
21 changes: 13 additions & 8 deletions src/winston-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ const consoleShortFormat = winston.format.combine(
winston.format.timestamp(),
winston.format.align(),
winston.format.printf(info => {
const { timestamp, level, message } = info;
const { timestamp, level, message, throwable } = info;
const ts = timestamp.slice(0, 19).replace('T', ' ');
return `${ts} [${level}]: ${message}`;
return `${ts} [${level}]: ${message} \n ${
throwable ? JSON.stringify(throwable, null, 2) : ''
}`;
}),
);

Expand Down Expand Up @@ -56,15 +58,18 @@ export const createLogger = (loggerConfiguration: LoggerConfiguration) => {
level: loggerConfiguration.loggerLevel,
levels: customLevels.levels,
format: winston.format.json(),
transports: [
exitOnError: false, // do not exit on handled exceptions
});

if (loggerConfiguration.logstashConfiguration) {
logger.add(
new LogstashTransport({
host: loggerConfiguration.logstashHost,
port: loggerConfiguration.logstashPort,
host: loggerConfiguration.logstashConfiguration.logstashHost,
port: loggerConfiguration.logstashConfiguration.logstashPort,
format: logstashFormat,
}),
],
exitOnError: false, // do not exit on handled exceptions
});
);
}

winston.addColors(customLevels.colors);
if (loggerConfiguration.writeToConsole) {
Expand Down
2 changes: 0 additions & 2 deletions test/polaris-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ describe('polaris-logger tests', () => {
};
const config: LoggerConfiguration = {
loggerLevel: 'info',
logstashHost: '127.0.0.1',
logstashPort: 8080,
};
const message: string = 'log message';

Expand Down
34 changes: 14 additions & 20 deletions test/winston-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ describe('winston-logger tests', () => {
test('createLogger - basic configuration - creates the logger with basic configuration', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
};

winstonLogger.createLogger(config);
Expand All @@ -66,6 +64,18 @@ describe('winston-logger tests', () => {
exitOnError: false,
}),
);
});

test('createLogger - configuration with logstash - logstash transport have been called with configured properties', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashConfiguration: {
logstashHost,
logstashPort,
},
};

winstonLogger.createLogger(config);

expect(LogstashTransport).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -78,8 +88,6 @@ describe('winston-logger tests', () => {
test('createLogger - configuration with console writing - console transport have been called', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
writeToConsole: true,
};

Expand All @@ -88,11 +96,9 @@ describe('winston-logger tests', () => {
expect(winston.transports.Console).toHaveBeenCalled();
});

test('createLogger - configuration with file writing - file transport have been called with configuration', () => {
test('createLogger - configuration with file writing - file transport have been called with configured properties', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
logFilePath: filePath,
};

Expand All @@ -105,11 +111,9 @@ describe('winston-logger tests', () => {
);
});

test('createLogger - configuration with rotate file writing - rotate file transport have been called with configuration', () => {
test('createLogger - configuration with rotate file writing - rotate file transport have been called with configured properties', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
dailyRotateFileConfiguration: {
directoryPath,
fileNamePrefix,
Expand All @@ -132,8 +136,6 @@ describe('winston-logger tests', () => {
test('createLogger - configuration with default days for rotate file writing - rotate file transport have been called with default days', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
dailyRotateFileConfiguration: {
directoryPath,
fileNamePrefix,
Expand All @@ -153,8 +155,6 @@ describe('winston-logger tests', () => {
test('createLogger - configuration with console and file writing - both console and file transports have been called', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
writeToConsole: true,
logFilePath: filePath,
};
Expand All @@ -168,8 +168,6 @@ describe('winston-logger tests', () => {
test('createLogger - configuration with console and rotate file writing - both console and rotate file transports have been called', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
writeToConsole: true,
dailyRotateFileConfiguration: {
directoryPath,
Expand All @@ -187,8 +185,6 @@ describe('winston-logger tests', () => {
test('createLogger - configuration with file and rotate file - only rotate file transport have been called', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
logFilePath: filePath,
dailyRotateFileConfiguration: {
directoryPath,
Expand All @@ -206,8 +202,6 @@ describe('winston-logger tests', () => {
test('createLogger - configuration with console and file and rotate file writing - only console and rotate file transports have been called', () => {
const config: LoggerConfiguration = {
loggerLevel,
logstashHost,
logstashPort,
writeToConsole: true,
logFilePath: filePath,
dailyRotateFileConfiguration: {
Expand Down

0 comments on commit ea8ffe6

Please sign in to comment.