feat: logging errors on log file

This commit is contained in:
Fabio Di Stasio 2023-11-27 13:36:56 +01:00
parent c3d96cb35b
commit 315d9d84c2
7 changed files with 80 additions and 75 deletions

17
package-lock.json generated
View File

@ -19,7 +19,7 @@
"@vueuse/core": "~10.4.1", "@vueuse/core": "~10.4.1",
"ace-builds": "~1.24.1", "ace-builds": "~1.24.1",
"better-sqlite3": "~9.1.1", "better-sqlite3": "~9.1.1",
"electron-log": "~4.4.1", "electron-log": "~5.0.1",
"electron-store": "~8.1.0", "electron-store": "~8.1.0",
"electron-updater": "~4.6.5", "electron-updater": "~4.6.5",
"electron-window-state": "~5.0.3", "electron-window-state": "~5.0.3",
@ -6859,9 +6859,12 @@
} }
}, },
"node_modules/electron-log": { "node_modules/electron-log": {
"version": "4.4.8", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/electron-log/-/electron-log-4.4.8.tgz", "resolved": "https://registry.npmjs.org/electron-log/-/electron-log-5.0.1.tgz",
"integrity": "sha512-QQ4GvrXO+HkgqqEOYbi+DHL7hj5JM+nHi/j+qrN9zeeXVKy8ZABgbu4CnG+BBqDZ2+tbeq9tUC4DZfIWFU5AZA==" "integrity": "sha512-x4wnwHg00h/onWQgjmvcdLV7Mrd9TZjxNs8LmXVpqvANDf4FsSs5wLlzOykWLcaFzR3+5hdVEQ8ctmrUxgHlPA==",
"engines": {
"node": ">= 14"
}
}, },
"node_modules/electron-publish": { "node_modules/electron-publish": {
"version": "24.5.0", "version": "24.5.0",
@ -22406,9 +22409,9 @@
} }
}, },
"electron-log": { "electron-log": {
"version": "4.4.8", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/electron-log/-/electron-log-4.4.8.tgz", "resolved": "https://registry.npmjs.org/electron-log/-/electron-log-5.0.1.tgz",
"integrity": "sha512-QQ4GvrXO+HkgqqEOYbi+DHL7hj5JM+nHi/j+qrN9zeeXVKy8ZABgbu4CnG+BBqDZ2+tbeq9tUC4DZfIWFU5AZA==" "integrity": "sha512-x4wnwHg00h/onWQgjmvcdLV7Mrd9TZjxNs8LmXVpqvANDf4FsSs5wLlzOykWLcaFzR3+5hdVEQ8ctmrUxgHlPA=="
}, },
"electron-publish": { "electron-publish": {
"version": "24.5.0", "version": "24.5.0",

View File

@ -127,7 +127,7 @@
"@vueuse/core": "~10.4.1", "@vueuse/core": "~10.4.1",
"ace-builds": "~1.24.1", "ace-builds": "~1.24.1",
"better-sqlite3": "~9.1.1", "better-sqlite3": "~9.1.1",
"electron-log": "~4.4.1", "electron-log": "~5.0.1",
"electron-store": "~8.1.0", "electron-store": "~8.1.0",
"electron-updater": "~4.6.5", "electron-updater": "~4.6.5",
"electron-window-state": "~5.0.3", "electron-window-state": "~5.0.3",

View File

@ -8,7 +8,7 @@ import * as path from 'path';
import { validateSender } from '../libs/misc/validateSender'; import { validateSender } from '../libs/misc/validateSender';
const isDevelopment = process.env.NODE_ENV !== 'production'; const isDevelopment = process.env.NODE_ENV !== 'production';
const isFlatpak = process.platform === 'linux' && Boolean(process.env.FLATPAK_ID?.includes('fabiodistasio') || process.env.FLATPAK_ID?.includes('AntaresSQL')); const isFlatpak = process.platform === 'linux' && process.env.FLATPAK_ID;
export default (connections: {[key: string]: antares.Client}) => { export default (connections: {[key: string]: antares.Client}) => {
let exporter: ChildProcessWithoutNullStreams = null; let exporter: ChildProcessWithoutNullStreams = null;

View File

@ -1,4 +1,5 @@
import { ipcMain } from 'electron'; import { ipcMain } from 'electron';
import * as log from 'electron-log/main';
import * as Store from 'electron-store'; import * as Store from 'electron-store';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
@ -59,7 +60,7 @@ export default () => {
mainWindow.reply('update-downloaded'); mainWindow.reply('update-downloaded');
}); });
// autoUpdater.logger = require('electron-log'); log.transports.file.level = 'info';
// autoUpdater.logger.transports.console.format = '{h}:{i}:{s} {text}'; // log.transports.console.format = '{h}:{i}:{s} {text}';
// autoUpdater.logger.transports.file.level = 'info'; autoUpdater.logger = log;
}; };

View File

@ -1,5 +1,6 @@
import * as remoteMain from '@electron/remote/main'; import * as remoteMain from '@electron/remote/main';
import { app, BrowserWindow, ipcMain, nativeImage, safeStorage } from 'electron'; import { app, BrowserWindow, ipcMain, nativeImage, safeStorage } from 'electron';
import * as log from 'electron-log/main';
import * as Store from 'electron-store'; import * as Store from 'electron-store';
import * as windowStateKeeper from 'electron-window-state'; import * as windowStateKeeper from 'electron-window-state';
import * as path from 'path'; import * as path from 'path';
@ -8,6 +9,7 @@ import ipcHandlers from './ipc-handlers';
import { OsMenu, ShortcutRegister } from './libs/ShortcutRegister'; import { OsMenu, ShortcutRegister } from './libs/ShortcutRegister';
Store.initRenderer(); Store.initRenderer();
log.errorHandler.startCatching();
const settingsStore = new Store({ name: 'settings' }); const settingsStore = new Store({ name: 'settings' });
const appTheme = settingsStore.get('application_theme'); const appTheme = settingsStore.get('application_theme');
const isDevelopment = process.env.NODE_ENV !== 'production'; const isDevelopment = process.env.NODE_ENV !== 'production';

View File

@ -1,4 +1,5 @@
import * as antares from 'common/interfaces/antares'; import * as antares from 'common/interfaces/antares';
import * as log from 'electron-log/main';
import * as fs from 'fs'; import * as fs from 'fs';
import { MySQLClient } from '../libs/clients/MySQLClient'; import { MySQLClient } from '../libs/clients/MySQLClient';
@ -8,64 +9,74 @@ import MysqlExporter from '../libs/exporters/sql/MysqlExporter';
import PostgreSQLExporter from '../libs/exporters/sql/PostgreSQLExporter'; import PostgreSQLExporter from '../libs/exporters/sql/PostgreSQLExporter';
let exporter: antares.Exporter; let exporter: antares.Exporter;
log.transports.file.fileName = 'workers.log';
log.errorHandler.startCatching();
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
process.on('message', async ({ type, client, tables, options }: any) => { process.on('message', async ({ type, client, tables, options }: any) => {
if (type === 'init') { if (type === 'init') {
const connection = await ClientsFactory.getClient({ try {
client: client.name, const connection = await ClientsFactory.getClient({
params: client.config, client: client.name,
poolSize: 5 params: client.config,
}) as MySQLClient | PostgreSQLClient; poolSize: 5
await connection.connect(); }) as MySQLClient | PostgreSQLClient;
await connection.connect();
switch (client.name) { switch (client.name) {
case 'mysql': case 'mysql':
case 'maria': case 'maria':
exporter = new MysqlExporter(connection as MySQLClient, tables, options); exporter = new MysqlExporter(connection as MySQLClient, tables, options);
break; break;
case 'pg': case 'pg':
exporter = new PostgreSQLExporter(connection as PostgreSQLClient, tables, options); exporter = new PostgreSQLExporter(connection as PostgreSQLClient, tables, options);
break; break;
default: default:
process.send({
type: 'error',
payload: `"${client.name}" exporter not aviable`
});
return;
}
exporter.once('error', err => {
log.error(err.toString());
process.send({ process.send({
type: 'error', type: 'error',
payload: `"${client.name}" exporter not aviable` payload: err.toString()
}); });
return; });
}
exporter.once('error', err => { exporter.once('end', () => {
console.error(err); process.send({
type: 'end',
payload: { cancelled: exporter.isCancelled }
});
connection.destroy();
});
exporter.once('cancel', () => {
fs.unlinkSync(exporter.outputFile);
process.send({ type: 'cancel' });
});
exporter.on('progress', state => {
process.send({
type: 'export-progress',
payload: state
});
});
exporter.run();
}
catch (err) {
log.error(err.toString());
process.send({ process.send({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
}); });
}); }
exporter.once('end', () => {
process.send({
type: 'end',
payload: { cancelled: exporter.isCancelled }
});
connection.destroy();
});
exporter.once('cancel', () => {
fs.unlinkSync(exporter.outputFile);
process.send({ type: 'cancel' });
});
exporter.on('progress', state => {
process.send({
type: 'export-progress',
payload: state
});
});
exporter.run();
} }
else if (type === 'cancel') else if (type === 'cancel')
exporter.cancel(); exporter.cancel();
}); });
process.on('beforeExit', console.log);

View File

@ -1,6 +1,7 @@
import SSHConfig from '@fabio286/ssh2-promise/lib/sshConfig'; import SSHConfig from '@fabio286/ssh2-promise/lib/sshConfig';
import * as antares from 'common/interfaces/antares'; import * as antares from 'common/interfaces/antares';
import { ImportOptions } from 'common/interfaces/importer'; import { ImportOptions } from 'common/interfaces/importer';
import * as log from 'electron-log/main';
import * as mysql from 'mysql2'; import * as mysql from 'mysql2';
import * as pg from 'pg'; import * as pg from 'pg';
@ -11,6 +12,9 @@ import MySQLImporter from '../libs/importers/sql/MySQLlImporter';
import PostgreSQLImporter from '../libs/importers/sql/PostgreSQLImporter'; import PostgreSQLImporter from '../libs/importers/sql/PostgreSQLImporter';
let importer: antares.Importer; let importer: antares.Importer;
log.transports.file.fileName = 'workers.log';
log.errorHandler.startCatching();
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
process.on('message', async ({ type, dbConfig, options }: { process.on('message', async ({ type, dbConfig, options }: {
type: string; type: string;
@ -49,7 +53,7 @@ process.on('message', async ({ type, dbConfig, options }: {
} }
importer.once('error', err => { importer.once('error', err => {
console.error(err); log.error(err.toString());
process.send({ process.send({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
@ -84,7 +88,7 @@ process.on('message', async ({ type, dbConfig, options }: {
importer.run(); importer.run();
} }
catch (err) { catch (err) {
console.error(err); log.error(err.toString());
process.send({ process.send({
type: 'error', type: 'error',
payload: err.toString() payload: err.toString()
@ -94,19 +98,3 @@ process.on('message', async ({ type, dbConfig, options }: {
else if (type === 'cancel') else if (type === 'cancel')
importer.cancel(); importer.cancel();
}); });
process.on('uncaughtException', (err) => {
console.error(err);
process.send({
type: 'error',
payload: err.toString()
});
});
process.on('unhandledRejection', (err) => {
console.error(err);
process.send({
type: 'error',
payload: err.toString()
});
});