Compare commits
3 Commits
master
...
7287-reaso
Author | SHA1 | Date |
---|---|---|
|
35c92e43ad | |
|
fda5726f55 | |
|
c0a23a0df8 |
|
@ -3,8 +3,8 @@ logRelation: true
|
|||
logMainShowField: false
|
||||
upperCaseTable: true
|
||||
userField: editorFk
|
||||
reasonField: motivation
|
||||
rowExcludeField: logExclude
|
||||
ignoreSystem: false
|
||||
excludeRegex: '__$'
|
||||
showFields:
|
||||
- name
|
||||
|
|
|
@ -95,9 +95,9 @@ module.exports = class ModelLoader {
|
|||
const globalProps = [
|
||||
'userField',
|
||||
'rowExcludeField',
|
||||
'ignoreSystem'
|
||||
'reasonField'
|
||||
];
|
||||
|
||||
|
||||
for (const [schema, table, tableInfo] of schemaMap) {
|
||||
const tableConf = tableInfo.conf;
|
||||
|
||||
|
@ -110,20 +110,19 @@ module.exports = class ModelLoader {
|
|||
: conf[prop];
|
||||
|
||||
// Fetch columns & types
|
||||
|
||||
|
||||
const columns = new Set();
|
||||
Object.assign (tableInfo, {
|
||||
castTypes: new Map(),
|
||||
columns
|
||||
});
|
||||
|
||||
|
||||
if (tableConf.types)
|
||||
for (const col in tableConf.types)
|
||||
tableInfo.castTypes.set(col, tableConf.types[col]);
|
||||
|
||||
|
||||
const [dbCols] = await db.query(
|
||||
`SELECT
|
||||
COLUMN_NAME \`col\`,
|
||||
`SELECT COLUMN_NAME \`col\`,
|
||||
DATA_TYPE \`type\`
|
||||
FROM information_schema.\`COLUMNS\`
|
||||
WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?`,
|
||||
|
@ -132,6 +131,7 @@ module.exports = class ModelLoader {
|
|||
|
||||
const exclude = new Set(tableConf.exclude);
|
||||
exclude.add(tableInfo.userField);
|
||||
exclude.add(tableInfo.reasonField);
|
||||
|
||||
for (const {col, type} of dbCols) {
|
||||
const isExcluded =
|
||||
|
|
52
mylogger.js
52
mylogger.js
|
@ -57,9 +57,12 @@ module.exports = class MyLogger {
|
|||
async init() {
|
||||
const {conf} = this;
|
||||
this.debug('MyLogger', 'Initializing.');
|
||||
this.onErrorListener = err => this.onError(err);
|
||||
|
||||
// DB connection
|
||||
|
||||
const db = this.db = await mysql.createConnection(conf.dstDb);
|
||||
db.on('error', this.onErrorListener);
|
||||
|
||||
await this.modelLoader.loadSchema();
|
||||
await this.showDb.loadSchema();
|
||||
|
@ -78,7 +81,8 @@ module.exports = class MyLogger {
|
|||
newInstance = ?,
|
||||
changedModelId = ?,
|
||||
changedModelValue = ?,
|
||||
summaryId = ?`
|
||||
summaryId = ?,
|
||||
reason = ?`
|
||||
);
|
||||
logInfo.fetchStmt = await db.prepare(
|
||||
`SELECT id FROM ${sqlTable}
|
||||
|
@ -94,7 +98,8 @@ module.exports = class MyLogger {
|
|||
creationDate = ?,
|
||||
oldInstance = ?,
|
||||
changedModelValue = ?,
|
||||
summaryId = ?
|
||||
summaryId = ?,
|
||||
reason = ?
|
||||
WHERE id = ?`
|
||||
);
|
||||
}
|
||||
|
@ -131,6 +136,8 @@ module.exports = class MyLogger {
|
|||
if (!this.running) return;
|
||||
this.running = false;
|
||||
this.debug('MyLogger', 'Ending.');
|
||||
|
||||
this.db.off('error', this.onErrorListener);
|
||||
|
||||
clearInterval(this.flushInterval);
|
||||
clearInterval(this.pingInterval);
|
||||
|
@ -201,6 +208,7 @@ module.exports = class MyLogger {
|
|||
zongji.once('error', onError);
|
||||
zongji.start(zongjiOpts);
|
||||
});
|
||||
zongji.on('error', this.onErrorListener);
|
||||
this.zongji = zongji;
|
||||
this.debug('Zongji', 'Started.');
|
||||
}
|
||||
|
@ -214,6 +222,7 @@ module.exports = class MyLogger {
|
|||
this.zongji = null;
|
||||
|
||||
zongji.off('binlog', this.onBinlogListener);
|
||||
zongji.off('error', this.onErrorListener);
|
||||
|
||||
// FIXME: Cannot call Zongji.stop(), it doesn't wait to end connection
|
||||
zongji.connection.destroy(() => {
|
||||
|
@ -243,7 +252,7 @@ module.exports = class MyLogger {
|
|||
}
|
||||
}
|
||||
|
||||
async handleError(err) {
|
||||
async onError(err) {
|
||||
if (!this.isOk) return;
|
||||
this.isOk = false;
|
||||
console.log(`Error: ${err.code}: ${err.message}`);
|
||||
|
@ -252,19 +261,19 @@ module.exports = class MyLogger {
|
|||
await this.end(true);
|
||||
} catch(e) {}
|
||||
|
||||
// FIXME: Error of mysql2/promise
|
||||
if (err.message === `Can't add new command when connection is in closed state`)
|
||||
switch (err.code) {
|
||||
case 'PROTOCOL_CONNECTION_LOST':
|
||||
case 'ECONNRESET':
|
||||
console.log('Trying to restart process.');
|
||||
await this.tryRestart();
|
||||
else
|
||||
switch (err.code) {
|
||||
case 'PROTOCOL_CONNECTION_LOST':
|
||||
case 'ECONNRESET':
|
||||
console.log('Trying to restart process.');
|
||||
await this.tryRestart();
|
||||
break;
|
||||
default:
|
||||
process.exit();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
|
||||
handleError(err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
async onBinlog(evt) {
|
||||
|
@ -305,16 +314,14 @@ module.exports = class MyLogger {
|
|||
const table = evt.tableMap[evt.tableId];
|
||||
const tableName = table.tableName;
|
||||
const tableInfo = this.schemaMap.get(table.parentSchema, tableName);
|
||||
|
||||
if (!tableInfo) return;
|
||||
|
||||
|
||||
const action = actions[eventName];
|
||||
const {rowExcludeField, ignoreSystem} = tableInfo;
|
||||
const {rowExcludeField} = tableInfo;
|
||||
const changes = [];
|
||||
|
||||
function isExcluded(row) {
|
||||
return (rowExcludeField && row[rowExcludeField])
|
||||
|| (ignoreSystem && row.editorFk == null);
|
||||
return rowExcludeField && row[rowExcludeField];
|
||||
}
|
||||
|
||||
function cast(value, type) {
|
||||
|
@ -526,6 +533,7 @@ module.exports = class MyLogger {
|
|||
);
|
||||
|
||||
try {
|
||||
const reasonField = row[tableInfo.reasonField] ?? null;
|
||||
if (isDelete) {
|
||||
[[deleteRow]] = await logInfo.fetchStmt.execute([
|
||||
modelName, modelId, originFk
|
||||
|
@ -537,6 +545,7 @@ module.exports = class MyLogger {
|
|||
oldInstance,
|
||||
modelValue,
|
||||
summaryId,
|
||||
reasonField,
|
||||
deleteRow.id
|
||||
]);
|
||||
}
|
||||
|
@ -553,7 +562,8 @@ module.exports = class MyLogger {
|
|||
newI ? JSON.stringify(newI) : null,
|
||||
modelId,
|
||||
modelValue,
|
||||
summaryId
|
||||
summaryId,
|
||||
reasonField
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "mylogger",
|
||||
"version": "1.1.5",
|
||||
"version": "1.1.4",
|
||||
"author": "Verdnatura Levante SL",
|
||||
"description": "MySQL and MariaDB logger using binary log",
|
||||
"license": "GPL-3.0",
|
||||
|
|
Loading…
Reference in New Issue