This repository has been archived by the owner on Mar 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogs.js
60 lines (56 loc) · 1.67 KB
/
logs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) 2018 QLC Chain Team
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import { logger } from './log';
export default class Logs {
constructor(host, port, user, password, database) {
this.tableName = 'logs';
this.knex = require('knex')({
client: 'pg',
connection: {
host: host,
port: port,
user: user,
password: password ? password : '',
database: database
}
});
this.knex.schema
.hasTable(this.tableName)
.then(ifExist => {
if (!ifExist) {
return this.knex.schema.createTable(this.tableName, table => {
table.increments('id');
table.string('message');
table.timestamp('timestamp');
table.string('fileName');
table.string('lineNumber');
table.string('level');
logger.info(`table [${this.tableName}] not exist, create it...`);
});
} else {
logger.info(`[${this.tableName}] already exist !!!`);
}
})
.catch(err => {
logger.error(`create table [${this.tableName}] error, ${err.message}, ${err.stack}`);
});
}
async saveLog2Db(log) {
if (log && log.message) {
delete log.additional;
await this.knex
.insert(log)
.into(this.tableName)
.then(() => {
logger.info(`insert log ${JSON.stringify(log)} into ${this.tableName}`);
return { inserted: true };
})
.catch(error => {
logger.error(`insert log ${JSON.stringify(log)} error: ${error.message} >>> ${error.stack}`);
});
}
return false;
}
}