Compare commits

..

3 Commits

Author SHA1 Message Date
Guillermo Bonet 35c92e43ad feat: refs #7287 Minor change
gitea/mylogger/pipeline/pr-master This commit looks good Details
2024-08-27 09:55:05 +02:00
Guillermo Bonet fda5726f55 feat: refs #7287 Final commit
gitea/mylogger/pipeline/pr-master This commit looks good Details
2024-08-27 09:39:11 +02:00
Guillermo Bonet c0a23a0df8 feat: refs #7287 First commit
gitea/mylogger/pipeline/pr-master This commit looks good Details
2024-08-27 08:07:50 +02:00
4 changed files with 40 additions and 30 deletions

View File

@ -3,8 +3,8 @@ logRelation: true
logMainShowField: false
upperCaseTable: true
userField: editorFk
reasonField: motivation
rowExcludeField: logExclude
ignoreSystem: false
excludeRegex: '__$'
showFields:
- name

View File

@ -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 =

View File

@ -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
]);
}

View File

@ -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",