Skip to content

Commit

Permalink
Fix: logging and empty config (#601)
Browse files Browse the repository at this point in the history
This pull request refactors the logging statements in the codebase to
use the logger instead of console.log. This improves the consistency and
maintainability of the logging system.

The following commits are included in this pull request:

- license package.json

- enable logger

- Refactor logging statements to use logger instead of console.log
  • Loading branch information
baruchiro authored Oct 13, 2024
1 parent 974eca0 commit d41cde2
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 682 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"repository": "https://github.com/brafdlog/caspion",
"discord": "https://discord.gg/XWWg7xvJyS",
"homepage": "https://github.com/brafdlog/caspion#README.md",
"license": "MIT",
"main": "packages/main/dist/index.js",
"scripts": {
"build": "yarn build:main && yarn build:preload && yarn build:renderer",
Expand Down Expand Up @@ -75,7 +76,7 @@
"csv-parse": "^5.5.6",
"csv-stringify": "^6.5.0",
"electron-google-oauth2": "^3.1.0",
"electron-log": "^5.1.5",
"electron-log": "^5.2.0",
"electron-updater": "6.3.0",
"emittery": "^1.0.3",
"eslint-plugin-react-hooks": "^4.6.2",
Expand Down
12 changes: 9 additions & 3 deletions packages/main/src/backend/configManager/configManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type Config } from '@/backend/commonTypes';
import { decrypt, encrypt } from '@/backend/configManager/encryption/crypto';
import { existsSync, promises as fs } from 'fs';
import configExample from './defaultConfig';
import logger from '/@/logging/logger';

export async function getConfig(
configPath: string = configFilePath,
Expand All @@ -11,13 +12,18 @@ export async function getConfig(
if (configFromFile) {
const decrypted = (await decrypt(configFromFile)) as string;
if (!decrypted) {
console.log('No config file found, returning default config');
logger.log('No config file found, returning default config');
return configExample;
}
try {
return JSON.parse(decrypted);
const config = JSON.parse(decrypted);
if (Object.keys(config).length === 0) {
logger.log('Empty config file found, returning default config');
return configExample;
}
return config;
} catch (e) {
console.error('Failed to parse config file, returning default config', e);
logger.error('Failed to parse config file, returning default config', e);
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/backend/export/exportTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@/backend/eventEmitters/EventEmitter';
import outputVendors from '@/backend/export/outputVendors';
import _ from 'lodash';
import logger from '/@/logging/logger';

type ExecutionResult = Partial<
Record<OutputVendorName, ExportTransactionsResult>
Expand Down Expand Up @@ -62,6 +63,7 @@ export async function createTransactionsInExternalVendors(
);
executionResult[outputVendor.name] = exportTransactionsResult;
} catch (e) {
logger.error('Failed to create transactions in external vendors', e);
await eventPublisher.emit(
EventNames.EXPORTER_ERROR,
new ExporterEvent({
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/backend/export/outputVendors/csv/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { parse } from 'csv-parse/sync';
import { stringify } from 'csv-stringify/sync';
import { promises as fs } from 'fs';
import { type TransactionInstallments } from 'israeli-bank-scrapers-core/lib/transactions';
import logger from '/@/logging/logger';

export function parseTransactions(csvText: string) {
return parse(csvText, {
Expand Down Expand Up @@ -111,6 +112,7 @@ const parseTransactionsFile = async (filename: string) => {
if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
return [] as EnrichedTransaction[];
}
logger.error('Failed to parse CSV file', e);
throw e;
}
};
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/backend/export/outputVendors/json/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
sortByDate,
} from '@/backend/transactions/transactions';
import { promises as fs } from 'fs';
import logger from '/@/logging/logger';

const parseTransactionsFile = async (filename: string) => {
try {
Expand All @@ -18,6 +19,7 @@ const parseTransactionsFile = async (filename: string) => {
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
return [] as EnrichedTransaction[];
}
logger.error('Failed to parse JSON file', err);
throw err;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { SaveTransaction, type TransactionDetail } from 'ynab';
import * as ynab from './ynab';
import ClearedEnum = SaveTransaction.ClearedEnum;

// TODO: make tests work again

describe('ynab', () => {
describe('isSameTransaction', () => {
test('Two transactions with different payee names should be considered the same if one of them is a transfer transaction', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const createTransactions: ExportTransactionsFunction = async (
allTransactions: transactionsToCreate,
}),
);
console.error('Failed to create transactions in ynab', e);
throw e;
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/backend/import/downloadChromium.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Browser, install } from '@puppeteer/browsers';
import logger from '/@/logging/logger';

type PuppeteerProgressCallback = (
downloadBytes: number,
Expand Down Expand Up @@ -39,8 +40,7 @@ export default async function downloadChromium(
downloadProgressCallback: progressCallback,
}).then(({ executablePath }) => {
downloadProm = null;
// TODO: eslint to use logger instead of console.log
console.log('Chromium downloaded to', executablePath);
logger.log('Chromium downloaded to', executablePath);
return executablePath;
});

Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/backend/import/importTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../eventEmitters/EventEmitter';
import { calculateTransactionHash } from '../transactions/transactions';
import getChrome from './downloadChromium';
import logger from '/@/logging/logger';

type ScrapingConfig = Config['scraping'];

Expand Down Expand Up @@ -188,6 +189,7 @@ async function fetchTransactions(
status: AccountStatus.ERROR,
}),
);
logger.error('Failed to fetch transactions', e);
throw e;
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/main/src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as configManager from './configManager/configManager';
import * as Events from './eventEmitters/EventEmitter';
import outputVendors from './export/outputVendors';
import * as bankScraper from './import/bankScraper';
import logger from '../logging/logger';

export { CompanyTypes } from 'israeli-bank-scrapers-core';
export { Events, configManager, outputVendors };
Expand Down Expand Up @@ -44,6 +45,7 @@ export async function scrapeAndUpdateOutputVendors(

return executionResult;
} catch (e) {
logger.error('Failed to create transactions in external vendors', e);
await eventPublisher.emit(
Events.EventNames.GENERAL_ERROR,
new Events.BudgetTrackingEvent({
Expand Down
28 changes: 19 additions & 9 deletions packages/main/src/logging/logger.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { App } from '@/app-globals';
import logger, {
type LogFunctions,
type LogLevel,
type MainErrorHandlerOptions,
} from 'electron-log'; // eslint-disable-line no-restricted-imports
import type {
LogFunctions,
LogLevel,
MainErrorHandlerOptions,
} from 'electron-log';
import log from 'electron-log/main';
import fs from 'fs';
import { EOL } from 'os';
import path from 'path';

log.initialize();

// This will transport the logs to the renderer process (DevTools) in production too
log.transports.ipc.level = log.transports.file.level;

Object.assign(console, log.functions);
const logger = log.scope('main');

export type Logger = LogFunctions;
export type Levels = LogLevel;

Expand All @@ -18,19 +27,20 @@ const onError: MainErrorHandlerOptions['onError'] = ({ error }) => {
logger.error(error.message || error);
if (error.stack) logger.debug(error.stack);
};
logger.errorHandler.startCatching({ onError });
logger.catchErrors({ onError });
log.errorHandler.startCatching({ onError });

export const getLastLines = (n: number) => {
const lines = fs
.readFileSync(logger.transports.file.getFile().path)
.readFileSync(log.transports.file.getFile().path)
.toString()
.split(EOL);
const lastLines = lines.slice(lines.length - n);
return lastLines.join(EOL);
};

export const getLogsFolder = () =>
path.dirname(logger.transports.file.getFile().path);
path.dirname(log.transports.file.getFile().path);

logger.info(`Logs folder: ${getLogsFolder()}`);

export default logger as Logger;
3 changes: 3 additions & 0 deletions packages/renderer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { createRoot } from 'react-dom/client';
import App from './components/App';
import './index.module.css';
import reportWebVitals from './reportWebVitals';
import logger from './logging/logger';

logger.info('Frontend started');

configureMobxLinting();

Expand Down
23 changes: 23 additions & 0 deletions packages/renderer/src/logging/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {
LogFunctions,
LogLevel,
MainErrorHandlerOptions,
} from 'electron-log';
import log from 'electron-log/renderer';

const logger = log.scope('renderer');

export type Logger = LogFunctions;
export type Levels = LogLevel;

logger.info('Welcome to Caspion log');
logger.info('Version: <need implementation>');

const onError: MainErrorHandlerOptions['onError'] = ({ error }) => {
logger.error(error.message || error);
if (error.stack) logger.debug(error.stack);
};
log.errorHandler.startCatching({ onError });
Object.assign(console, log.functions);

export default logger as Logger;
Loading

0 comments on commit d41cde2

Please sign in to comment.