Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
6ed2b6e993 | |||
4f1df305ed | |||
44fccc252e | |||
0854207a04 | |||
64b71dfb42 | |||
d846b100d2 |
16
package-lock.json
generated
16
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/elasticlog",
|
"name": "@mojoio/elasticsearch",
|
||||||
"version": "1.0.14",
|
"version": "1.0.17",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -137,12 +137,20 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartlog-interfaces": "^1.0.9"
|
"@pushrocks/smartlog-interfaces": "^1.0.9"
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
"dependencies": {
|
||||||
"@pushrocks/smartlog-interfaces": {
|
"@pushrocks/smartlog-interfaces": {
|
||||||
"version": "1.0.15",
|
"version": "1.0.15",
|
||||||
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-1.0.15.tgz",
|
"resolved": "https://registry.npmjs.org/@pushrocks/smartlog-interfaces/-/smartlog-interfaces-1.0.15.tgz",
|
||||||
"integrity": "sha512-dn9a+IhneukhtVGQG031oodOITmmQ5s5hcMThd+cMUQL3XYTbLPnZVuJfTDeWCT0iqLqrBD/qp2d1RRc3W/qIQ=="
|
"integrity": "sha512-dn9a+IhneukhtVGQG031oodOITmmQ5s5hcMThd+cMUQL3XYTbLPnZVuJfTDeWCT0iqLqrBD/qp2d1RRc3W/qIQ==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"@pushrocks/smartlog-interfaces": {
|
||||||
|
"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": {
|
"@pushrocks/smartpath": {
|
||||||
"version": "4.0.1",
|
"version": "4.0.1",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/elasticlog",
|
"name": "@mojoio/elasticsearch",
|
||||||
"version": "1.0.14",
|
"version": "1.0.17",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "log to elasticsearch in a kibana compatible format",
|
"description": "log to elasticsearch in a kibana compatible format",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -24,7 +24,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.2",
|
"@pushrocks/smartdelay": "^2.0.2",
|
||||||
"@pushrocks/smartlog-interfaces": "^1.0.15",
|
"@pushrocks/smartlog-interfaces": "^2.0.2",
|
||||||
"@types/elasticsearch": "^5.0.28",
|
"@types/elasticsearch": "^5.0.28",
|
||||||
"elasticsearch": "^15.2.0"
|
"elasticsearch": "^15.2.0"
|
||||||
}
|
}
|
||||||
|
28
test/test.ts
28
test/test.ts
@ -1,31 +1,35 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import { Qenv } from '@pushrocks/qenv';
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
import * as elasticlog from '../ts/index';
|
import * as elasticsearch from '../ts/index';
|
||||||
|
|
||||||
const testQenv = new Qenv('./', './.nogit/');
|
const testQenv = new Qenv('./', './.nogit/');
|
||||||
|
|
||||||
let testElasticLog: elasticlog.ElasticLog<any>;
|
let testElasticLog: elasticsearch.ElasticSearch<any>;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
testElasticLog = new elasticlog.ElasticLog({
|
testElasticLog = new elasticsearch.ElasticSearch({
|
||||||
domain: process.env.ELK_DOMAIN,
|
domain: process.env.ELK_DOMAIN,
|
||||||
port: parseInt(process.env.ELK_PORT, 10),
|
port: parseInt(process.env.ELK_PORT, 10),
|
||||||
ssl: true,
|
ssl: true,
|
||||||
user: process.env.ELK_USER,
|
user: process.env.ELK_USER,
|
||||||
pass: process.env.ELK_PASS,
|
pass: process.env.ELK_PASS
|
||||||
logContext: {
|
|
||||||
company: 'Lossless GmbH',
|
|
||||||
runtime: 'node',
|
|
||||||
containerName: 'testContainer',
|
|
||||||
environment: 'test'
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
expect(testElasticLog).to.be.instanceOf(elasticlog.ElasticLog);
|
expect(testElasticLog).to.be.instanceOf(elasticsearch.ElasticSearch);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should send a message to Elasticsearch', async () => {
|
tap.test('should send a message to Elasticsearch', async () => {
|
||||||
testElasticLog.log({
|
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'
|
message: 'hi, this is a testMessage'
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
// interfaces
|
// interfaces
|
||||||
import { Client as ElasticClient } from 'elasticsearch';
|
import { Client as ElasticClient } from 'elasticsearch';
|
||||||
import { ILogContext } from '@pushrocks/smartlog-interfaces';
|
import { ILogContext, ILogPackage, ILogDestination } from '@pushrocks/smartlog-interfaces';
|
||||||
|
|
||||||
// other classes
|
// other classes
|
||||||
import { LogScheduler } from './elasticlog.classes.logscheduler';
|
import { LogScheduler } from './elasticsearch.classes.logscheduler';
|
||||||
|
|
||||||
export interface IStandardLogParams {
|
export interface IStandardLogParams {
|
||||||
message: string;
|
message: string;
|
||||||
@ -16,12 +16,10 @@ export interface IElasticLogConstructorOptions {
|
|||||||
ssl: boolean;
|
ssl: boolean;
|
||||||
user?: string;
|
user?: string;
|
||||||
pass?: string;
|
pass?: string;
|
||||||
logContext: ILogContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ElasticLog<T> {
|
export class ElasticSearch<T> {
|
||||||
client: ElasticClient;
|
client: ElasticClient;
|
||||||
logContext: ILogContext;
|
|
||||||
logScheduler = new LogScheduler(this);
|
logScheduler = new LogScheduler(this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +27,6 @@ export class ElasticLog<T> {
|
|||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
constructor(optionsArg: IElasticLogConstructorOptions) {
|
constructor(optionsArg: IElasticLogConstructorOptions) {
|
||||||
this.logContext = optionsArg.logContext;
|
|
||||||
this.client = new ElasticClient({
|
this.client = new ElasticClient({
|
||||||
host: this.computeHostString(optionsArg),
|
host: this.computeHostString(optionsArg),
|
||||||
log: 'trace'
|
log: 'trace'
|
||||||
@ -53,36 +50,40 @@ export class ElasticLog<T> {
|
|||||||
return hostString;
|
return hostString;
|
||||||
}
|
}
|
||||||
|
|
||||||
async log(logObject: IStandardLogParams, scheduleOverwrite = false) {
|
public async log(logPackageArg: ILogPackage, scheduleOverwrite = false) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
if (this.logScheduler.logsScheduled && !scheduleOverwrite) {
|
||||||
this.logScheduler.scheduleLog(logObject);
|
this.logScheduler.scheduleLog(logPackageArg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.client.index(
|
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()
|
'0' + now.getDate()
|
||||||
).slice(-2)}`,
|
).slice(-2)}`,
|
||||||
type: 'log',
|
type: 'log',
|
||||||
body: {
|
body: {
|
||||||
'@timestamp': now.toISOString(),
|
'@timestamp': new Date(logPackageArg.timestamp).toISOString(),
|
||||||
zone: this.logContext.zone,
|
...logPackageArg
|
||||||
container: this.logContext.containerName,
|
|
||||||
environment: this.logContext.environment,
|
|
||||||
severity: logObject.severity,
|
|
||||||
message: logObject.message
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(error, response) => {
|
(error, response) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.log('ElasticLog encountered an error:');
|
console.log('ElasticLog encountered an error:');
|
||||||
console.log(error);
|
console.log(error);
|
||||||
this.logScheduler.addFailedLog(logObject);
|
this.logScheduler.addFailedLog(logPackageArg);
|
||||||
} else {
|
} else {
|
||||||
console.log(`ElasticLog: ${logObject.message}`);
|
console.log(`ElasticLog: ${logPackageArg.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get logDestination (): ILogDestination {
|
||||||
|
return {
|
||||||
|
handleLog: (smartlogPackageArg: ILogPackage) => {
|
||||||
|
this.log(smartlogPackageArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,11 +1,11 @@
|
|||||||
import { ElasticLog, IStandardLogParams } from './elasticlog.classes.elasticlog';
|
import { ElasticSearch, IStandardLogParams } from './elasticsearch.classes.elasticsearch';
|
||||||
|
|
||||||
export class LogScheduler {
|
export class LogScheduler {
|
||||||
elasticLogRef: ElasticLog<any>;
|
elasticLogRef: ElasticSearch<any>;
|
||||||
logsScheduled = false;
|
logsScheduled = false;
|
||||||
logStorage: any[] = [];
|
logStorage: any[] = [];
|
||||||
|
|
||||||
constructor(elasticLogRefArg: ElasticLog<any>) {
|
constructor(elasticLogRefArg: ElasticSearch<any>) {
|
||||||
this.elasticLogRef = elasticLogRefArg;
|
this.elasticLogRef = elasticLogRefArg;
|
||||||
}
|
}
|
||||||
|
|
@ -1 +1 @@
|
|||||||
export * from './elasticlog.classes.elasticlog';
|
export * from './elasticsearch.classes.elasticsearch';
|
||||||
|
Reference in New Issue
Block a user