feat(core): implement winston support
This commit is contained in:
parent
9e85f22891
commit
fd1d92edd6
13
dist/elasticlog.classes.elasticlog.d.ts
vendored
13
dist/elasticlog.classes.elasticlog.d.ts
vendored
@ -1,11 +1,12 @@
|
|||||||
export declare type Environment = 'local' | 'test' | 'staging' | 'production';
|
export declare type Environment = "local" | "test" | "staging" | "production";
|
||||||
import { Client as ElasticClient } from 'elasticsearch';
|
import { Client as ElasticClient } from "elasticsearch";
|
||||||
|
import { LogScheduler } from "./elasticlog.classes.logscheduler";
|
||||||
export interface LogContext {
|
export interface LogContext {
|
||||||
zone?: string;
|
zone?: string;
|
||||||
containerName?: string;
|
containerName?: string;
|
||||||
environment: Environment;
|
environment: Environment;
|
||||||
}
|
}
|
||||||
export declare type TLogSeverity = 'log' | 'info' | 'warn' | 'error' | 'fatal';
|
export declare type TLogSeverity = "log" | "info" | "warn" | "error" | "fatal";
|
||||||
export interface IStandardLogParams {
|
export interface IStandardLogParams {
|
||||||
message: string;
|
message: string;
|
||||||
severity: string;
|
severity: string;
|
||||||
@ -32,9 +33,5 @@ export declare class ElasticLog<T> {
|
|||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
private computeHostString(optionsArg);
|
private computeHostString(optionsArg);
|
||||||
log(logObject: IStandardLogParams): void;
|
log(logObject: IStandardLogParams, scheduleOverwrite?: boolean): Promise<void>;
|
||||||
}
|
|
||||||
export declare class LogScheduler {
|
|
||||||
logStorage: any[];
|
|
||||||
addFailedLog(objectArg: any | IStandardLogParams): void;
|
|
||||||
}
|
}
|
||||||
|
65
dist/elasticlog.classes.elasticlog.js
vendored
65
dist/elasticlog.classes.elasticlog.js
vendored
@ -1,21 +1,30 @@
|
|||||||
"use strict";
|
"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 });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
// interfaces
|
// interfaces
|
||||||
const elasticsearch_1 = require("elasticsearch");
|
const elasticsearch_1 = require("elasticsearch");
|
||||||
|
// other classes
|
||||||
|
const elasticlog_classes_logscheduler_1 = require("./elasticlog.classes.logscheduler");
|
||||||
class ElasticLog {
|
class ElasticLog {
|
||||||
/**
|
/**
|
||||||
* sets up an instance of Elastic log
|
* sets up an instance of Elastic log
|
||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
constructor(optionsArg) {
|
constructor(optionsArg) {
|
||||||
this.logScheduler = new LogScheduler();
|
this.logScheduler = new elasticlog_classes_logscheduler_1.LogScheduler(this);
|
||||||
this.logContext = optionsArg.logContext;
|
this.logContext = optionsArg.logContext;
|
||||||
this.client = new elasticsearch_1.Client({
|
this.client = new elasticsearch_1.Client({
|
||||||
host: this.computeHostString(optionsArg),
|
host: this.computeHostString(optionsArg),
|
||||||
log: 'trace'
|
log: "trace"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
;
|
|
||||||
/**
|
/**
|
||||||
* computes the host string from the constructor options
|
* computes the host string from the constructor options
|
||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
@ -33,31 +42,35 @@ class ElasticLog {
|
|||||||
}
|
}
|
||||||
return hostString;
|
return hostString;
|
||||||
}
|
}
|
||||||
log(logObject) {
|
log(logObject, scheduleOverwrite = false) {
|
||||||
const now = new Date();
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
this.client.index({
|
const now = new Date();
|
||||||
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
|
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
||||||
type: 'log',
|
this.logScheduler.scheduleLog(logObject);
|
||||||
body: {
|
return;
|
||||||
'@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);
|
|
||||||
}
|
}
|
||||||
|
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;
|
exports.ElasticLog = ElasticLog;
|
||||||
;
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmVsYXN0aWNsb2cuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9lbGFzdGljbG9nLmNsYXNzZXMuZWxhc3RpY2xvZy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBRUEsYUFBYTtBQUNiLGlEQUF3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FBQyxHQUFHLENBQUMsS0FBSyxDQUFDLENBQUM7b0JBQ25CLElBQUksQ0FBQyxZQUFZLENBQUMsWUFBWSxDQUFDLFNBQVMsQ0FBQyxDQUFDO2dCQUM1QyxDQUFDO2dCQUFDLElBQUksQ0FBQyxDQUFDO29CQUNOLE9BQU8sQ0FBQyxHQUFHLENBQUMsZUFBZSxTQUFTLENBQUMsT0FBTyxFQUFFLENBQUMsQ0FBQztnQkFDbEQsQ0FBQztZQUNILENBQUMsQ0FDRixDQUFDO1FBQ0osQ0FBQztLQUFBO0NBQ0Y7QUFqRUQsZ0NBaUVDIn0=
|
||||||
class LogScheduler {
|
|
||||||
addFailedLog(objectArg) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exports.LogScheduler = LogScheduler;
|
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmVsYXN0aWNsb2cuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9lbGFzdGljbG9nLmNsYXNzZXMuZWxhc3RpY2xvZy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUVBLGFBQWE7QUFDYixpREFBdUQ7QUF3QnZEO0lBS0U7OztPQUdHO0lBQ0gsWUFBYSxVQUF5QztRQU50RCxpQkFBWSxHQUFHLElBQUksWUFBWSxFQUFFLENBQUE7UUFPL0IsSUFBSSxDQUFDLFVBQVUsR0FBRyxVQUFVLENBQUMsVUFBVSxDQUFBO1FBQ3ZDLElBQUksQ0FBQyxNQUFNLEdBQUcsSUFBSSxzQkFBYSxDQUFDO1lBQzlCLElBQUksRUFBRSxJQUFJLENBQUMsaUJBQWlCLENBQUMsVUFBVSxDQUFDO1lBQ3hDLEdBQUcsRUFBRSxPQUFPO1NBQ2IsQ0FBQyxDQUFDO0lBQ0wsQ0FBQztJQUFBLENBQUM7SUFFRjs7O09BR0c7SUFDSyxpQkFBaUIsQ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VBQUUsSUFBSSxDQUFDLFVBQVUsQ0FBQyxhQUFhO2dCQUN4QyxXQUFXLEVBQUUsSUFBSSxDQUFDLFVBQVUsQ0FBQyxXQUFXO2dCQUN4QyxRQUFRLEVBQUUsU0FBUyxDQUFDLFFBQVE7Z0JBQzVCLE9BQU8sRUFBRSxTQUFTLENBQUMsT0FBTzthQUMzQjtTQUNGLEVBQUUsQ0FBQyxLQUFLLEVBQUUsUUFBUSxFQUFFLEVBQUU7WUFDckIsRUFBRSxDQUFBLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQztnQkFDVCxPQUFPLENBQUMsR0FBRyxDQUFDLEtBQUssQ0FBQyxDQUFBO2dCQUNsQixJQUFJLENBQUMsWUFBWSxDQUFDLFlBQVksQ0FBQyxTQUFTLENBQUMsQ0FBQTtZQUMzQyxDQUFDO1FBQ0gsQ0FBQyxDQUFDLENBQUE7SUFDSixDQUFDO0NBRUY7QUF0REQsZ0NBc0RDO0FBQUEsQ0FBQztBQUVGO0lBRUUsWUFBWSxDQUFDLFNBQW1DO0lBRWhELENBQUM7Q0FDRjtBQUxELG9DQUtDIn0=
|
|
11
dist/elasticlog.classes.logscheduler.d.ts
vendored
Normal file
11
dist/elasticlog.classes.logscheduler.d.ts
vendored
Normal 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
42
dist/elasticlog.classes.logscheduler.js
vendored
Normal 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.setRetry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.LogScheduler = LogScheduler;
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5jbGFzc2VzLmxvZ3NjaGVkdWxlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL2VsYXN0aWNsb2cuY2xhc3Nlcy5sb2dzY2hlZHVsZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFLQTtJQUtFLFlBQVksZ0JBQWlDO1FBSDdDLGtCQUFhLEdBQUcsS0FB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2xCLENBQUM7UUFDSCxDQUFDLEVBQUUsSUFBSSxDQUFDLENBQUM7SUFDWCxDQUFDO0lBRUQsU0FBUztRQUNQLEVBQUUsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxDQUFDLENBQUM7WUFDeEIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxnQ0FBZ0MsQ0FBQyxDQUFDO1lBQzlDLElBQUksQ0FBQyxhQUFhLEdBQUcsSUFBSSxDQUFDO1lBQzFCLElBQUksQ0FBQyxRQUFRLEVBQUUsQ0FBQztRQUNsQixDQUFDO0lBQ0gsQ0FBQztDQUNGO0FBekNELG9DQXlDQyJ9
|
11
dist/elasticlog.classes.winstontransport.d.ts
vendored
Normal file
11
dist/elasticlog.classes.winstontransport.d.ts
vendored
Normal 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;
|
||||||
|
}
|
16
dist/elasticlog.classes.winstontransport.js
vendored
Normal file
16
dist/elasticlog.classes.winstontransport.js
vendored
Normal 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==
|
4
dist/elasticlog.plugins.d.ts
vendored
4
dist/elasticlog.plugins.d.ts
vendored
@ -1,3 +1,3 @@
|
|||||||
import * as elasticsearch from 'elasticsearch';
|
import * as elasticsearch from "elasticsearch";
|
||||||
import * as smartdelay from 'smartdelay';
|
import * as smartdelay from "smartdelay";
|
||||||
export { elasticsearch, smartdelay };
|
export { elasticsearch, smartdelay };
|
||||||
|
2
dist/elasticlog.plugins.js
vendored
2
dist/elasticlog.plugins.js
vendored
@ -4,4 +4,4 @@ const elasticsearch = require("elasticsearch");
|
|||||||
exports.elasticsearch = elasticsearch;
|
exports.elasticsearch = elasticsearch;
|
||||||
const smartdelay = require("smartdelay");
|
const smartdelay = require("smartdelay");
|
||||||
exports.smartdelay = smartdelay;
|
exports.smartdelay = smartdelay;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZWxhc3RpY2xvZy5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0NBQStDO0FBRzdDLHNDQUFhO0FBRmYseUNBQXlDO0FBR3ZDLGdDQUFVIn0=
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxhc3RpY2xvZy5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvZWxhc3RpY2xvZy5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsK0NBQStDO0FBRXRDLHNDQUFhO0FBRHRCLHlDQUF5QztBQUNqQixnQ0FBVSJ9
|
2
dist/index.d.ts
vendored
2
dist/index.d.ts
vendored
@ -1 +1 @@
|
|||||||
export * from './elasticlog.classes.elasticlog';
|
export * from "./elasticlog.classes.elasticlog";
|
||||||
|
2
dist/index.js
vendored
2
dist/index.js
vendored
@ -4,4 +4,4 @@ function __export(m) {
|
|||||||
}
|
}
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
__export(require("./elasticlog.classes.elasticlog"));
|
__export(require("./elasticlog.classes.elasticlog"));
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHFEQUErQyJ9
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHFEQUFnRCJ9
|
@ -19,7 +19,8 @@
|
|||||||
"@types/elasticsearch": "^5.0.19",
|
"@types/elasticsearch": "^5.0.19",
|
||||||
"elasticsearch": "^14.0.0",
|
"elasticsearch": "^14.0.0",
|
||||||
"smartdelay": "^1.0.4",
|
"smartdelay": "^1.0.4",
|
||||||
"typings-global": "^1.0.16"
|
"typings-global": "^1.0.16",
|
||||||
|
"winston-transport": "^3.0.1"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"commitizen": {
|
"commitizen": {
|
||||||
|
@ -1,46 +1,50 @@
|
|||||||
export type Environment = 'local' | 'test' | 'staging' | 'production' ;
|
export type Environment = "local" | "test" | "staging" | "production";
|
||||||
|
|
||||||
// interfaces
|
// 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 {
|
export interface LogContext {
|
||||||
zone?: string,
|
zone?: string;
|
||||||
containerName?: string
|
containerName?: string;
|
||||||
environment: Environment
|
environment: Environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TLogSeverity = 'log' | 'info' | 'warn' | 'error' | 'fatal'
|
export type TLogSeverity = "log" | "info" | "warn" | "error" | "fatal";
|
||||||
|
|
||||||
export interface IStandardLogParams {
|
export interface IStandardLogParams {
|
||||||
message: string
|
message: string;
|
||||||
severity: string
|
severity: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IElasticLogConstructorOptions {
|
export interface IElasticLogConstructorOptions {
|
||||||
port: number
|
port: number;
|
||||||
domain: string
|
domain: string;
|
||||||
ssl: boolean
|
ssl: boolean;
|
||||||
user?: string
|
user?: string;
|
||||||
pass?: string
|
pass?: string;
|
||||||
logContext: LogContext
|
logContext: LogContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ElasticLog<T> {
|
export class ElasticLog<T> {
|
||||||
client: ElasticClient
|
client: ElasticClient;
|
||||||
logContext: LogContext
|
logContext: LogContext;
|
||||||
logScheduler = new LogScheduler()
|
logScheduler = new LogScheduler(this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sets up an instance of Elastic log
|
* sets up an instance of Elastic log
|
||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
constructor (optionsArg: IElasticLogConstructorOptions) {
|
constructor(optionsArg: IElasticLogConstructorOptions) {
|
||||||
this.logContext = optionsArg.logContext
|
this.logContext = optionsArg.logContext;
|
||||||
this.client = new ElasticClient({
|
this.client = new ElasticClient({
|
||||||
host: this.computeHostString(optionsArg),
|
host: this.computeHostString(optionsArg),
|
||||||
log: 'trace'
|
log: "trace"
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* computes the host string from the constructor options
|
* computes the host string from the constructor options
|
||||||
@ -48,42 +52,46 @@ export class ElasticLog<T> {
|
|||||||
*/
|
*/
|
||||||
private computeHostString(optionsArg: IElasticLogConstructorOptions): string {
|
private computeHostString(optionsArg: IElasticLogConstructorOptions): string {
|
||||||
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
let hostString = `${optionsArg.domain}:${optionsArg.port}`;
|
||||||
if(optionsArg.user && optionsArg.pass) {
|
if (optionsArg.user && optionsArg.pass) {
|
||||||
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`
|
hostString = `${optionsArg.user}:${optionsArg.pass}@${hostString}`;
|
||||||
}
|
}
|
||||||
if(optionsArg.ssl) {
|
if (optionsArg.ssl) {
|
||||||
hostString = `https://${hostString}`
|
hostString = `https://${hostString}`;
|
||||||
} else {
|
} else {
|
||||||
hostString = `http://${hostString}`
|
hostString = `http://${hostString}`;
|
||||||
}
|
}
|
||||||
return hostString;
|
return hostString;
|
||||||
}
|
}
|
||||||
|
|
||||||
log(logObject: IStandardLogParams) {
|
async log(logObject: IStandardLogParams, scheduleOverwrite = false) {
|
||||||
const now = new Date()
|
const now = new Date();
|
||||||
this.client.index({
|
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
||||||
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(-2)}.${now.getDate()}`,
|
this.logScheduler.scheduleLog(logObject);
|
||||||
type: 'log',
|
return;
|
||||||
body: {
|
}
|
||||||
'@timestamp': now.toISOString(),
|
this.client.index(
|
||||||
container: this.logContext.containerName,
|
{
|
||||||
environment: this.logContext.environment,
|
index: `logs-${now.getFullYear()}.${("0" + (now.getMonth() + 1)).slice(
|
||||||
severity: logObject.severity,
|
-2
|
||||||
message: logObject.message
|
)}.${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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
47
ts/elasticlog.classes.logscheduler.ts
Normal file
47
ts/elasticlog.classes.logscheduler.ts
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
ts/elasticlog.classes.winstontransport.ts
Normal file
27
ts/elasticlog.classes.winstontransport.ts
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,3 @@
|
|||||||
import * as elasticsearch from 'elasticsearch';
|
import * as elasticsearch from "elasticsearch";
|
||||||
import * as smartdelay from 'smartdelay';
|
import * as smartdelay from "smartdelay";
|
||||||
export {
|
export { elasticsearch, smartdelay };
|
||||||
elasticsearch,
|
|
||||||
smartdelay
|
|
||||||
}
|
|
||||||
|
@ -1 +1 @@
|
|||||||
export * from './elasticlog.classes.elasticlog'
|
export * from "./elasticlog.classes.elasticlog";
|
||||||
|
@ -590,6 +590,10 @@ vinyl@^2.0.1:
|
|||||||
remove-trailing-separator "^1.0.1"
|
remove-trailing-separator "^1.0.1"
|
||||||
replace-ext "^1.0.0"
|
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:
|
word-wrap@^1.0.3:
|
||||||
version "1.2.3"
|
version "1.2.3"
|
||||||
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
|
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
|
||||||
|
Loading…
Reference in New Issue
Block a user