Compare commits

...

9 Commits

Author SHA1 Message Date
6fde0544f5 1.0.18 2018-11-07 11:53:36 +01:00
6ed2b6e993 1.0.17 2018-11-07 11:38:53 +01:00
4f1df305ed fix(core): update 2018-11-07 11:38:53 +01:00
44fccc252e 1.0.16 2018-11-04 02:42:08 +01:00
0854207a04 fix(core): update 2018-11-04 02:42:07 +01:00
64b71dfb42 1.0.15 2018-11-03 23:45:22 +01:00
d846b100d2 fix(core): update 2018-11-03 23:45:21 +01:00
cb4cfaca09 1.0.14 2018-11-03 22:34:49 +01:00
3232114c5b fix(core): add snyk 2018-11-03 22:34:49 +01:00
8 changed files with 58 additions and 41 deletions

4
.snyk Normal file
View File

@ -0,0 +1,4 @@
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
version: v1.12.0
ignore: {}
patch: {}

18
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/elasticlog",
"version": "1.0.13",
"name": "@mojoio/elasticsearch",
"version": "1.0.18",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -137,12 +137,20 @@
"dev": true,
"requires": {
"@pushrocks/smartlog-interfaces": "^1.0.9"
},
"dependencies": {
"@pushrocks/smartlog-interfaces": {
"version": "1.0.15",
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-1.0.15.tgz",
"integrity": "sha512-dn9a+IhneukhtVGQG031oodOITmmQ5s5hcMThd+cMUQL3XYTbLPnZVuJfTDeWCT0iqLqrBD/qp2d1RRc3W/qIQ==",
"dev": true
}
}
},
"@pushrocks/smartlog-interfaces": {
"version": "1.0.15",
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-1.0.15.tgz",
"integrity": "sha512-dn9a+IhneukhtVGQG031oodOITmmQ5s5hcMThd+cMUQL3XYTbLPnZVuJfTDeWCT0iqLqrBD/qp2d1RRc3W/qIQ=="
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-2.0.2.tgz",
"integrity": "sha512-kJNQ/6kfljgtwebhoiD8WtRWfdVhOoE1nr8FoUJLlOjLphU8SPa42Hg6/yPkSTaGxWwDhk6PkMJl64O7HNjRUQ=="
},
"@pushrocks/smartpath": {
"version": "4.0.1",

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/elasticlog",
"version": "1.0.13",
"name": "@mojoio/elasticsearch",
"version": "1.0.18",
"private": false,
"description": "log to elasticsearch in a kibana compatible format",
"main": "dist/index.js",
@ -24,7 +24,7 @@
},
"dependencies": {
"@pushrocks/smartdelay": "^2.0.2",
"@pushrocks/smartlog-interfaces": "^1.0.15",
"@pushrocks/smartlog-interfaces": "^2.0.2",
"@types/elasticsearch": "^5.0.28",
"elasticsearch": "^15.2.0"
}

View File

@ -1,31 +1,35 @@
import { expect, tap } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
import * as elasticlog from '../ts/index';
import * as elasticsearch from '../ts/index';
const testQenv = new Qenv('./', './.nogit/');
let testElasticLog: elasticlog.ElasticLog<any>;
let testElasticLog: elasticsearch.ElasticSearch<any>;
tap.test('first test', async () => {
testElasticLog = new elasticlog.ElasticLog({
testElasticLog = new elasticsearch.ElasticSearch({
domain: process.env.ELK_DOMAIN,
port: parseInt(process.env.ELK_PORT, 10),
ssl: true,
user: process.env.ELK_USER,
pass: process.env.ELK_PASS,
logContext: {
company: 'Lossless GmbH',
runtime: 'node',
containerName: 'testContainer',
environment: 'test'
}
pass: process.env.ELK_PASS
});
expect(testElasticLog).to.be.instanceOf(elasticlog.ElasticLog);
expect(testElasticLog).to.be.instanceOf(elasticsearch.ElasticSearch);
});
tap.test('should send a message to Elasticsearch', async () => {
testElasticLog.log({
severity: 'log',
timestamp: Date.now(),
type: 'increment',
level: 'info',
context: {
company: 'Lossless GmbH',
companyunit: 'lossless.cloud',
containerName: 'testcontainer',
environment: 'test',
runtime: 'node',
zone: 'ship.zone'
},
message: 'hi, this is a testMessage'
});
});

View File

@ -1,9 +1,9 @@
// interfaces
import { Client as ElasticClient } from 'elasticsearch';
import { ILogContext } from '@pushrocks/smartlog-interfaces';
import { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
// other classes
import { LogScheduler } from './elasticlog.classes.logscheduler';
import { LogScheduler } from './elasticsearch.classes.logscheduler';
export interface IStandardLogParams {
message: string;
@ -16,12 +16,10 @@ export interface IElasticLogConstructorOptions {
ssl: boolean;
user?: string;
pass?: string;
logContext: ILogContext;
}
export class ElasticLog<T> {
export class ElasticSearch<T> {
client: ElasticClient;
logContext: ILogContext;
logScheduler = new LogScheduler(this);
/**
@ -29,7 +27,6 @@ export class ElasticLog<T> {
* @param optionsArg
*/
constructor(optionsArg: IElasticLogConstructorOptions) {
this.logContext = optionsArg.logContext;
this.client = new ElasticClient({
host: this.computeHostString(optionsArg),
log: 'trace'
@ -53,36 +50,40 @@ export class ElasticLog<T> {
return hostString;
}
async log(logObject: IStandardLogParams, scheduleOverwrite = false) {
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
const now = new Date();
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
this.logScheduler.scheduleLog(logObject);
this.logScheduler.scheduleLog(logPackageArg);
return;
}
this.client.index(
{
index: `logstash-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${(
index: `smartlog-${now.getFullYear()}.${('0' + (now.getMonth() + 1)).slice(-2)}.${(
'0' + now.getDate()
).slice(-2)}`,
type: 'log',
body: {
'@timestamp': now.toISOString(),
zone: this.logContext.zone,
container: this.logContext.containerName,
environment: this.logContext.environment,
severity: logObject.severity,
message: logObject.message
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
...logPackageArg
}
},
(error, response) => {
if (error) {
console.log('ElasticLog encountered an error:');
console.log(error);
this.logScheduler.addFailedLog(logObject);
this.logScheduler.addFailedLog(logPackageArg);
} else {
console.log(`ElasticLog: ${logObject.message}`);
console.log(`ElasticLog: ${logPackageArg.message}`);
}
}
);
}
get logDestination (): ILogDestination {
return {
handleLog: (smartlogPackageArg: ILogPackage) => {
this.log(smartlogPackageArg);
}
}
}
}

View File

@ -1,11 +1,11 @@
import { ElasticLog, IStandardLogParams } from './elasticlog.classes.elasticlog';
import { ElasticSearch, IStandardLogParams } from './elasticsearch.classes.elasticsearch';
export class LogScheduler {
elasticLogRef: ElasticLog<any>;
elasticLogRef: ElasticSearch<any>;
logsScheduled = false;
logStorage: any[] = [];
constructor(elasticLogRefArg: ElasticLog<any>) {
constructor(elasticLogRefArg: ElasticSearch<any>) {
this.elasticLogRef = elasticLogRefArg;
}

View File

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