Compare commits
8 Commits
6449-summa
...
master
Author | SHA1 | Date |
---|---|---|
|
deba9c8b2c | |
|
f75df4e19f | |
|
80ac579a47 | |
|
6b53029c06 | |
|
c501b254d5 | |
|
5857c2294e | |
|
4e709c2091 | |
|
19bcd267c0 |
|
@ -4,6 +4,7 @@ logMainShowField: false
|
||||||
upperCaseTable: true
|
upperCaseTable: true
|
||||||
userField: editorFk
|
userField: editorFk
|
||||||
rowExcludeField: logExclude
|
rowExcludeField: logExclude
|
||||||
|
ignoreSystem: false
|
||||||
excludeRegex: '__$'
|
excludeRegex: '__$'
|
||||||
showFields:
|
showFields:
|
||||||
- name
|
- name
|
||||||
|
|
|
@ -94,7 +94,8 @@ module.exports = class ModelLoader {
|
||||||
];
|
];
|
||||||
const globalProps = [
|
const globalProps = [
|
||||||
'userField',
|
'userField',
|
||||||
'rowExcludeField'
|
'rowExcludeField',
|
||||||
|
'ignoreSystem'
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const [schema, table, tableInfo] of schemaMap) {
|
for (const [schema, table, tableInfo] of schemaMap) {
|
||||||
|
|
44
mylogger.js
44
mylogger.js
|
@ -57,12 +57,9 @@ module.exports = class MyLogger {
|
||||||
async init() {
|
async init() {
|
||||||
const {conf} = this;
|
const {conf} = this;
|
||||||
this.debug('MyLogger', 'Initializing.');
|
this.debug('MyLogger', 'Initializing.');
|
||||||
this.onErrorListener = err => this.onError(err);
|
|
||||||
|
|
||||||
// DB connection
|
// DB connection
|
||||||
|
|
||||||
const db = this.db = await mysql.createConnection(conf.dstDb);
|
const db = this.db = await mysql.createConnection(conf.dstDb);
|
||||||
db.on('error', this.onErrorListener);
|
|
||||||
|
|
||||||
await this.modelLoader.loadSchema();
|
await this.modelLoader.loadSchema();
|
||||||
await this.showDb.loadSchema();
|
await this.showDb.loadSchema();
|
||||||
|
@ -134,8 +131,6 @@ module.exports = class MyLogger {
|
||||||
if (!this.running) return;
|
if (!this.running) return;
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.debug('MyLogger', 'Ending.');
|
this.debug('MyLogger', 'Ending.');
|
||||||
|
|
||||||
this.db.off('error', this.onErrorListener);
|
|
||||||
|
|
||||||
clearInterval(this.flushInterval);
|
clearInterval(this.flushInterval);
|
||||||
clearInterval(this.pingInterval);
|
clearInterval(this.pingInterval);
|
||||||
|
@ -206,7 +201,6 @@ module.exports = class MyLogger {
|
||||||
zongji.once('error', onError);
|
zongji.once('error', onError);
|
||||||
zongji.start(zongjiOpts);
|
zongji.start(zongjiOpts);
|
||||||
});
|
});
|
||||||
zongji.on('error', this.onErrorListener);
|
|
||||||
this.zongji = zongji;
|
this.zongji = zongji;
|
||||||
this.debug('Zongji', 'Started.');
|
this.debug('Zongji', 'Started.');
|
||||||
}
|
}
|
||||||
|
@ -220,7 +214,6 @@ module.exports = class MyLogger {
|
||||||
this.zongji = null;
|
this.zongji = null;
|
||||||
|
|
||||||
zongji.off('binlog', this.onBinlogListener);
|
zongji.off('binlog', this.onBinlogListener);
|
||||||
zongji.off('error', this.onErrorListener);
|
|
||||||
|
|
||||||
// FIXME: Cannot call Zongji.stop(), it doesn't wait to end connection
|
// FIXME: Cannot call Zongji.stop(), it doesn't wait to end connection
|
||||||
zongji.connection.destroy(() => {
|
zongji.connection.destroy(() => {
|
||||||
|
@ -250,7 +243,7 @@ module.exports = class MyLogger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onError(err) {
|
async handleError(err) {
|
||||||
if (!this.isOk) return;
|
if (!this.isOk) return;
|
||||||
this.isOk = false;
|
this.isOk = false;
|
||||||
console.log(`Error: ${err.code}: ${err.message}`);
|
console.log(`Error: ${err.code}: ${err.message}`);
|
||||||
|
@ -259,19 +252,19 @@ module.exports = class MyLogger {
|
||||||
await this.end(true);
|
await this.end(true);
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
|
|
||||||
switch (err.code) {
|
// FIXME: Error of mysql2/promise
|
||||||
case 'PROTOCOL_CONNECTION_LOST':
|
if (err.message === `Can't add new command when connection is in closed state`)
|
||||||
case 'ECONNRESET':
|
|
||||||
console.log('Trying to restart process.');
|
|
||||||
await this.tryRestart();
|
await this.tryRestart();
|
||||||
break;
|
else
|
||||||
default:
|
switch (err.code) {
|
||||||
process.exit();
|
case 'PROTOCOL_CONNECTION_LOST':
|
||||||
}
|
case 'ECONNRESET':
|
||||||
}
|
console.log('Trying to restart process.');
|
||||||
|
await this.tryRestart();
|
||||||
handleError(err) {
|
break;
|
||||||
console.error(err);
|
default:
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async onBinlog(evt) {
|
async onBinlog(evt) {
|
||||||
|
@ -312,14 +305,16 @@ module.exports = class MyLogger {
|
||||||
const table = evt.tableMap[evt.tableId];
|
const table = evt.tableMap[evt.tableId];
|
||||||
const tableName = table.tableName;
|
const tableName = table.tableName;
|
||||||
const tableInfo = this.schemaMap.get(table.parentSchema, tableName);
|
const tableInfo = this.schemaMap.get(table.parentSchema, tableName);
|
||||||
|
|
||||||
if (!tableInfo) return;
|
if (!tableInfo) return;
|
||||||
|
|
||||||
const action = actions[eventName];
|
const action = actions[eventName];
|
||||||
const {rowExcludeField} = tableInfo;
|
const {rowExcludeField, ignoreSystem} = tableInfo;
|
||||||
const changes = [];
|
const changes = [];
|
||||||
|
|
||||||
function isExcluded(row) {
|
function isExcluded(row) {
|
||||||
return rowExcludeField && row[rowExcludeField];
|
return (rowExcludeField && row[rowExcludeField])
|
||||||
|
|| (ignoreSystem && row.editorFk == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function cast(value, type) {
|
function cast(value, type) {
|
||||||
|
@ -492,6 +487,7 @@ module.exports = class MyLogger {
|
||||||
const isDelete = action == 'delete';
|
const isDelete = action == 'delete';
|
||||||
const isUpdate = action == 'update';
|
const isUpdate = action == 'update';
|
||||||
const created = new Date(evt.timestamp);
|
const created = new Date(evt.timestamp);
|
||||||
|
const showId = tableInfo.conf.showId;
|
||||||
|
|
||||||
for (const change of changes) {
|
for (const change of changes) {
|
||||||
let newI, oldI;
|
let newI, oldI;
|
||||||
|
@ -514,7 +510,7 @@ module.exports = class MyLogger {
|
||||||
oldI = change.instance;
|
oldI = change.instance;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const summaryId = row[tableInfo.conf.showId];
|
const summaryId = showId ? row[showId] : null;
|
||||||
const modelId = row[tableInfo.idName];
|
const modelId = row[tableInfo.idName];
|
||||||
const modelValue = change.modelValue ?? null;
|
const modelValue = change.modelValue ?? null;
|
||||||
const oldInstance = oldI ? JSON.stringify(oldI) : null;
|
const oldInstance = oldI ? JSON.stringify(oldI) : null;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "mylogger",
|
"name": "mylogger",
|
||||||
"version": "1.1.4",
|
"version": "1.1.5",
|
||||||
"author": "Verdnatura Levante SL",
|
"author": "Verdnatura Levante SL",
|
||||||
"description": "MySQL and MariaDB logger using binary log",
|
"description": "MySQL and MariaDB logger using binary log",
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
|
|
Loading…
Reference in New Issue