feat(core): implement winston support

This commit is contained in:
Philipp Kunz 2018-01-27 19:16:05 +01:00
parent 9e85f22891
commit fd1d92edd6
17 changed files with 273 additions and 99 deletions

View File

@ -1,11 +1,12 @@
export declare type Environment = 'local' | 'test' | 'staging' | 'production';
import { Client as ElasticClient } from 'elasticsearch';
export declare type Environment = "local" | "test" | "staging" | "production";
import { Client as ElasticClient } from "elasticsearch";
import { LogScheduler } from "./elasticlog.classes.logscheduler";
export interface LogContext {
zone?: string;
containerName?: string;
environment: Environment;
}
export declare type TLogSeverity = 'log' | 'info' | 'warn' | 'error' | 'fatal';
export declare type TLogSeverity = "log" | "info" | "warn" | "error" | "fatal";
export interface IStandardLogParams {
message: string;
severity: string;
@ -32,9 +33,5 @@ export declare class ElasticLog<T> {
* @param optionsArg
*/
private computeHostString(optionsArg);
log(logObject: IStandardLogParams): void;
}
export declare class LogScheduler {
logStorage: any[];
addFailedLog(objectArg: any | IStandardLogParams): void;
log(logObject: IStandardLogParams, scheduleOverwrite?: boolean): Promise<void>;
}

View File

@ -1,21 +1,30 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
// interfaces
const elasticsearch_1 = require("elasticsearch");
// other classes
const elasticlog_classes_logscheduler_1 = require("./elasticlog.classes.logscheduler");
class ElasticLog {
/**
* sets up an instance of Elastic log
* @param optionsArg
*/
constructor(optionsArg) {
this.logScheduler = new LogScheduler();
this.logScheduler = new elasticlog_classes_logscheduler_1.LogScheduler(this);
this.logContext = optionsArg.logContext;
this.client = new elasticsearch_1.Client({
host: this.computeHostString(optionsArg),
log: 'trace'
log: "trace"
});
}
;
/**
* computes the host string from the constructor options
* @param optionsArg
@ -33,31 +42,35 @@ class ElasticLog {
}
return hostString;
}
log(logObject) {
const now = new Date();
this.client.index({
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
type: 'log',
body: {
'@timestamp': now.toISOString(),
container: this.logContext.containerName,
environment: this.logContext.environment,
severity: logObject.severity,
message: logObject.message
}
}, (error, response) => {
if (error) {
console.log(error);
this.logScheduler.addFailedLog(logObject);
log(logObject, scheduleOverwrite = false) {
return __awaiter(this, void 0, void 0, function* () {
const now = new Date();
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
this.logScheduler.scheduleLog(logObject);
return;
}
this.client.index({
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
type: "log",
body: {
"@timestamp": now.toISOString(),
container: this.logContext.containerName,
environment: this.logContext.environment,
severity: logObject.severity,
message: logObject.message
}
}, (error, response) => {
if (error) {
console.log("ElasticLog encountered an error:");
console.log(error);
this.logScheduler.addFailedLog(logObject);
}
else {
console.log(`ElasticLog: ${logObject.message}`);
}
});
});
}
}
exports.ElasticLog = ElasticLog;
;
class LogScheduler {
addFailedLog(objectArg) {
}
}
exports.LogScheduler = LogScheduler;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmVsYXN0aWNsb2cuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9lbGFzdGljbG9nLmNsYXNzZXMuZWxhc3RpY2xvZy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUVBLGFBQWE7QUFDYixpREFBdUQ7QUF3QnZEO0lBS0U7OztPQUdHO0lBQ0gsWUFBYSxVQUF5QztRQU50RCxpQkFBWSxHQUFHLElBQUksWUFBWSxFQUFFLENBQUE7UUFPL0IsSUFBSSxDQUFDL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vQkFDVixPQUFPLENBQUMsR0FBRyxDQUFDLGtDQUFrQyxDQUFDLENBQUM7b0JBQ2hELE9BQU8sQ0FBQyxHQUFHLENBQUMsS0FBSyxDQUFDLENBQUM7b0JBQ25CLElBQUksQ0FBQyxZQUFZLENBQUMsWUFBWSxDQUFDLFNBQVMsQ0FBQyxDQUFDO2dCQUM1QyxDQUFDO2dCQUFDLElBQUksQ0FBQyxDQUFDO29CQUNOLE9BQU8sQ0FBQyxHQUFHLENBQUMsZUFBZSxTQUFTLENBQUMsT0FBTyxFQUFFLENBQUMsQ0FBQztnQkFDbEQsQ0FBQztZQUNILENBQUMsQ0FDRixDQUFDO1FBQ0osQ0FBQztLQUFBO0NBQ0Y7QUFqRUQsZ0NBaUVDIn0=

View File

@ -0,0 +1,11 @@
import { ElasticLog, IStandardLogParams } from "./elasticlog.classes.elasticlog";
export declare class LogScheduler {
elasticLogRef: ElasticLog<any>;
logsScheduled: boolean;
logStorage: any[];
constructor(elasticLogRefArg: ElasticLog<any>);
addFailedLog(objectArg: any | IStandardLogParams): void;
scheduleLog(logObject: any): void;
setRetry(): void;
deferSend(): void;
}

42
dist/elasticlog.classes.logscheduler.js vendored Normal file
View File

@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class LogScheduler {
constructor(elasticLogRefArg) {
this.logsScheduled = false;
this.logStorage = [];
this.elasticLogRef = elasticLogRefArg;
}
addFailedLog(objectArg) {
this.logStorage.push(objectArg);
this.setRetry();
}
scheduleLog(logObject) {
this.logStorage.push(logObject);
}
setRetry() {
setTimeout(() => {
const oldStorage = this.logStorage;
this.logStorage = [];
for (let logObject of oldStorage) {
this.elasticLogRef.log(logObject, true);
}
if (this.logStorage.length === 0) {
console.log("ElasticLog retry success!!!");
this.logsScheduled = false;
}
else {
console.log("ElasticLog retry failed");
this.setRetry();
}
}, 5000);
}
deferSend() {
if (!this.logsScheduled) {
console.log("Retry ElasticLog in 5 seconds!");
this.logsScheduled = true;
this.setRet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FBQyxVQUFVLENBQUMsTUFBTSxLQUFLLENBQUMsQ0FBQyxDQUFDLENBQUM7Z0JBQ2pDLE9BQU8sQ0FBQyxHQUFHLENBQUMsNkJBQTZCLENBQUMsQ0FBQztnQkFDM0MsSUFBSSxDQUFDLGFBQWEsR0FBRyxLQUFLLENBQUM7WUFDN0IsQ0FBQztZQUFDLElBQUksQ0FBQyxDQUFDO2dCQUNOLE9BQU8sQ0FBQyxHQUFHLENBQUMseUJBQXlCLENBQUMsQ0FBQztnQkFDdkMsSUFBSSxDQUFDLFFBQVEsRUFBRSxDQUFDO1lBQ2xCLENBQUM7UUFDSCxDQUFDLEVBQUUsSUFBSSxDQUFDLENBQUM7SUFDWCxDQUFDO0lBRUQsU0FBUztRQUNQLEVBQUUsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxDQUFDLENBQUM7WUFDeEIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxnQ0FBZ0MsQ0FBQyxDQUFDO1lBQzlDLElBQUksQ0FBQyxhQUFhLEdBQUcsSUFBSSxDQUFDO1lBQzFCLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztRQUNsQixDQUFDO0lBQ0gsQ0FBQztDQUNGO0FBekNELG9DQXlDQyJ9

View File

@ -0,0 +1,11 @@
import { Transport } from "winston-transport";
import { ElasticLog, IElasticLogConstructorOptions } from "./elasticlog.classes.elasticlog";
export interface IWinstonStandardLogParams {
message: string;
level: string;
}
export declare class ElasticWinstonTransport extends Transport {
client: ElasticLog<any>;
constructor(optsArg: IElasticLogConstructorOptions);
log(info: any, callback: any): void;
}

View File

@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const winston_transport_1 = require("winston-transport");
const elasticlog_classes_elasticlog_1 = require("./elasticlog.classes.elasticlog");
class ElasticWinstonTransport extends winston_transport_1.Transport {
constructor(optsArg) {
super(optsArg);
this.client = new elasticlog_classes_elasticlog_1.ElasticLog(optsArg);
}
log(info, callback) {
this.client.log(info);
callback();
}
}
exports.ElasticWinstonTransport = ElasticWinstonTransport;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLndpbnN0b250cmFuc3BvcnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9lbGFzdGljbG9nLmNsYXNzZXMud2luc3RvbnRyYW5zcG9ydC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHlEQUE4QztBQUM5QyxtRkFHeUM7QUFPekMsNkJBQXFDLFNBQVEsNkJBQVM7SUFHcEQsWUFBWSxPQUFzQztRQUNoRCxLQUFLLENBQUMsT0FBTyxDQUFDLENBQUM7UUFDZixJQUFJLENBQUMsTUFBTSxHQUFHLElBQUksMENBQVUsQ0FBQyxPQUFPLENBQUMsQ0FBQztJQUN4QyxDQUFDO0lBRUQsR0FBRyxDQUFDLElBQUksRUFBRSxRQUFRO1FBQ2hCLElBQUksQ0FBQyxNQUFNLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3RCLFFBQVEsRUFBRSxDQUFDO0lBQ2IsQ0FBQztDQUNGO0FBWkQsMERBWUMifQ==

View File

@ -1,3 +1,3 @@
import * as elasticsearch from 'elasticsearch';
import * as smartdelay from 'smartdelay';
import * as elasticsearch from "elasticsearch";
import * as smartdelay from "smartdelay";
export { elasticsearch, smartdelay };

View File

@ -4,4 +4,4 @@ const elasticsearch = require("elasticsearch");
exports.elasticsearch = elasticsearch;
const smartdelay = require("smartdelay");
exports.smartdelay = smartdelay;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZWxhc3RpY2xvZy5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0NBQStDO0FBRzdDLHNDQUFhO0FBRmYseUNBQXlDO0FBR3ZDLGdDQUFVIn0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZWxhc3RpY2xvZy5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0NBQStDO0FBRXRDLHNDQUFhO0FBRHRCLHlDQUF5QztBQUNqQixnQ0FBVSJ9

2
dist/index.d.ts vendored
View File

@ -1 +1 @@
export * from './elasticlog.classes.elasticlog';
export * from "./elasticlog.classes.elasticlog";

2
dist/index.js vendored
View File

@ -4,4 +4,4 @@ function __export(m) {
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./elasticlog.classes.elasticlog"));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHFEQUErQyJ9
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHFEQUFnRCJ9

View File

@ -19,7 +19,8 @@
"@types/elasticsearch": "^5.0.19",
"elasticsearch": "^14.0.0",
"smartdelay": "^1.0.4",
"typings-global": "^1.0.16"
"typings-global": "^1.0.16",
"winston-transport": "^3.0.1"
},
"config": {
"commitizen": {

View File

@ -1,46 +1,50 @@
export type Environment = 'local' | 'test' | 'staging' | 'production' ;
export type Environment = "local" | "test" | "staging" | "production";
// interfaces
import { Client as ElasticClient } from 'elasticsearch'
import { Client as ElasticClient } from "elasticsearch";
import { IWinstonStandardLogParams } from "./elasticlog.classes.winstontransport";
// other classes
import { LogScheduler } from "./elasticlog.classes.logscheduler";
export interface LogContext {
zone?: string,
containerName?: string
environment: Environment
zone?: string;
containerName?: string;
environment: Environment;
}
export type TLogSeverity = 'log' | 'info' | 'warn' | 'error' | 'fatal'
export type TLogSeverity = "log" | "info" | "warn" | "error" | "fatal";
export interface IStandardLogParams {
message: string
severity: string
message: string;
severity: string;
}
export interface IElasticLogConstructorOptions {
port: number
domain: string
ssl: boolean
user?: string
pass?: string
logContext: LogContext
port: number;
domain: string;
ssl: boolean;
user?: string;
pass?: string;
logContext: LogContext;
}
export class ElasticLog<T> {
client: ElasticClient
logContext: LogContext
logScheduler = new LogScheduler()
client: ElasticClient;
logContext: LogContext;
logScheduler = new LogScheduler(this);
/**
* sets up an instance of Elastic log
* @param optionsArg
*/
constructor (optionsArg: IElasticLogConstructorOptions) {
this.logContext = optionsArg.logContext
constructor(optionsArg: IElasticLogConstructorOptions) {
this.logContext = optionsArg.logContext;
this.client = new ElasticClient({
host: this.computeHostString(optionsArg),
log: 'trace'
log: "trace"
});
};
}
/**
* computes the host string from the constructor options
@ -48,42 +52,46 @@ export class ElasticLog<T> {
*/
private computeHostString(optionsArg: IElasticLogConstructorOptions): string {
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
if(optionsArg.user && optionsArg.pass) {
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`
if (optionsArg.user && optionsArg.pass) {
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
}
if(optionsArg.ssl) {
hostString = `https://${hostString}`
if (optionsArg.ssl) {
hostString = `https://${hostString}`;
} else {
hostString = `http://${hostString}`
hostString = `http://${hostString}`;
}
return hostString;
}
log(logObject: IStandardLogParams) {
const now = new Date()
this.client.index({
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
type: 'log',
body: {
'@timestamp': now.toISOString(),
container: this.logContext.containerName,
environment: this.logContext.environment,
severity: logObject.severity,
message: logObject.message
async log(logObject: IStandardLogParams, scheduleOverwrite = false) {
const now = new Date();
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
this.logScheduler.scheduleLog(logObject);
return;
}
this.client.index(
{
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(
-2
)}.${now.getDate()}`,
type: "log",
body: {
"@timestamp": now.toISOString(),
container: this.logContext.containerName,
environment: this.logContext.environment,
severity: logObject.severity,
message: logObject.message
}
},
(error, response) => {
if (error) {
console.log("ElasticLog encountered an error:");
console.log(error);
this.logScheduler.addFailedLog(logObject);
} else {
console.log(`ElasticLog: ${logObject.message}`);
}
}
}, (error, response) => {
if(error) {
console.log(error)
this.logScheduler.addFailedLog(logObject)
}
})
);
}
};
export class LogScheduler {
logStorage: any[]
addFailedLog(objectArg: any | IStandardLogParams) {
}
}
}

View File

@ -0,0 +1,47 @@
import {
ElasticLog,
IStandardLogParams
} from "./elasticlog.classes.elasticlog";
export class LogScheduler {
elasticLogRef: ElasticLog<any>;
logsScheduled = false;
logStorage: any[] = [];
constructor(elasticLogRefArg: ElasticLog<any>) {
this.elasticLogRef = elasticLogRefArg;
}
addFailedLog(objectArg: any | IStandardLogParams) {
this.logStorage.push(objectArg);
this.setRetry();
}
scheduleLog(logObject: any) {
this.logStorage.push(logObject);
}
setRetry() {
setTimeout(() => {
const oldStorage = this.logStorage;
this.logStorage = [];
for (let logObject of oldStorage) {
this.elasticLogRef.log(logObject, true);
}
if (this.logStorage.length === 0) {
console.log("ElasticLog retry success!!!");
this.logsScheduled = false;
} else {
console.log("ElasticLog retry failed");
this.setRetry();
}
}, 5000);
}
deferSend() {
if (!this.logsScheduled) {
console.log("Retry ElasticLog in 5 seconds!");
this.logsScheduled = true;
this.setRetry();
}
}
}

View File

@ -0,0 +1,27 @@
import { Transport } from "winston-transport";
import {
ElasticLog,
IElasticLogConstructorOptions
} from "./elasticlog.classes.elasticlog";
export interface IWinstonStandardLogParams {
message: string;
level: string;
}
export class ElasticWinstonTransport extends Transport {
client: ElasticLog<any>;
constructor(optsArg: IElasticLogConstructorOptions) {
super(optsArg);
this.client = new ElasticLog(optsArg);
}
log(info, callback) {
this.client.log({
severity: info.level,
message: info.message,
});
callback();
}
}

View File

@ -1,6 +1,3 @@
import * as elasticsearch from 'elasticsearch';
import * as smartdelay from 'smartdelay';
export {
elasticsearch,
smartdelay
}
import * as elasticsearch from "elasticsearch";
import * as smartdelay from "smartdelay";
export { elasticsearch, smartdelay };

View File

@ -1 +1 @@
export * from './elasticlog.classes.elasticlog'
export * from "./elasticlog.classes.elasticlog";

View File

@ -590,6 +590,10 @@ vinyl@^2.0.1:
remove-trailing-separator "^1.0.1"
replace-ext "^1.0.0"
winston-transport@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/winston-transport/-/winston-transport-3.0.1.tgz#8008b15eef5660c4fb3fa094d58ccbd08528c58d"
word-wrap@^1.0.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"