From 887c271853ad6071705a12d91be0545d7aa2d8d0 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 4 Aug 2023 10:57:54 +0100 Subject: [PATCH] Chore: Trying to fix logger (#8610) --- .eslintignore | 1 + .gitignore | 1 + package.json | 2 +- packages/app-desktop/jest.setup.js | 10 +++++++++- packages/app-desktop/main-html.js | 5 +++++ packages/app-mobile/root.tsx | 2 ++ packages/lib/BaseApplication.ts | 20 +++++++++---------- packages/lib/initLib.ts | 9 +++++++++ packages/lib/testing/test-utils.ts | 4 +++- packages/server/src/app.ts | 2 ++ .../server/src/utils/testing/testUtils.ts | 2 ++ yarn.lock | 13 +----------- 12 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 packages/lib/initLib.ts diff --git a/.eslintignore b/.eslintignore index aa5911836..63e907f1f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -541,6 +541,7 @@ packages/lib/htmlUtils2.test.js packages/lib/import-enex-md-gen.test.js packages/lib/import-enex-md-gen.js packages/lib/import-enex.js +packages/lib/initLib.js packages/lib/locale.test.js packages/lib/locale.js packages/lib/markdownUtils.test.js diff --git a/.gitignore b/.gitignore index 814ee1438..01041fa37 100644 --- a/.gitignore +++ b/.gitignore @@ -526,6 +526,7 @@ packages/lib/htmlUtils2.test.js packages/lib/import-enex-md-gen.test.js packages/lib/import-enex-md-gen.js packages/lib/import-enex.js +packages/lib/initLib.js packages/lib/locale.test.js packages/lib/locale.js packages/lib/markdownUtils.test.js diff --git a/package.json b/package.json index 57683d0f4..6823a2f45 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ } }, "devDependencies": { - "@joplin/utils": "~2.11", + "@joplin/utils": "~2.12", "@seiyab/eslint-plugin-react-hooks": "4.5.1-beta.0", "@typescript-eslint/eslint-plugin": "5.60.0", "@typescript-eslint/parser": "5.60.0", diff --git a/packages/app-desktop/jest.setup.js b/packages/app-desktop/jest.setup.js index 177260d47..16b07e902 100644 --- a/packages/app-desktop/jest.setup.js +++ b/packages/app-desktop/jest.setup.js @@ -1,5 +1,6 @@ const { default: Logger, TargetType } = require('@joplin/utils/Logger'); +const initLib = require('@joplin/lib/initLib').default; // TODO: Some libraries required by test-utils.js seem to fail to import with the // jsdom environment. @@ -11,9 +12,16 @@ const logger = new Logger(); logger.addTarget(TargetType.Console); logger.setLevel(Logger.LEVEL_WARN); Logger.initializeGlobalLogger(logger); +initLib(logger); // @electron/remote requires electron to be running. Mock it. jest.mock('@electron/remote', () => { - return { require }; + return { + require: () => { + return { + default: {}, + }; + }, + }; }); diff --git a/packages/app-desktop/main-html.js b/packages/app-desktop/main-html.js index ceabd26a2..adc0b0190 100644 --- a/packages/app-desktop/main-html.js +++ b/packages/app-desktop/main-html.js @@ -29,6 +29,7 @@ const EncryptionService = require('@joplin/lib/services/e2ee/EncryptionService') const { FileApiDriverLocal } = require('@joplin/lib/file-api-driver-local'); const React = require('react'); const nodeSqlite = require('sqlite3'); +const initLib = require('@joplin/lib/initLib').default; // Security: If we attempt to navigate away from the root HTML page, it's likely because // of an improperly sanitized link. Prevent this by closing the window before we can @@ -132,6 +133,10 @@ document.addEventListener('click', (event) => { event.preventDefault(); }); +const logger = new Logger(); +Logger.initializeGlobalLogger(logger); +initLib(logger); + app().start(bridge().processArgv()).then((result) => { if (!result || !result.action) { require('./gui/Root'); diff --git a/packages/app-mobile/root.tsx b/packages/app-mobile/root.tsx index c9f0380ab..9c6c34f9c 100644 --- a/packages/app-mobile/root.tsx +++ b/packages/app-mobile/root.tsx @@ -85,6 +85,7 @@ const SyncTargetWebDAV = require('@joplin/lib/SyncTargetWebDAV.js'); const SyncTargetDropbox = require('@joplin/lib/SyncTargetDropbox.js'); const SyncTargetAmazonS3 = require('@joplin/lib/SyncTargetAmazonS3.js'); import BiometricPopup from './components/biometrics/BiometricPopup'; +import initLib from '@joplin/lib/initLib'; SyncTargetRegistry.addClass(SyncTargetNone); SyncTargetRegistry.addClass(SyncTargetOneDrive); @@ -474,6 +475,7 @@ async function initialize(dispatch: Function) { } Logger.initializeGlobalLogger(mainLogger); + initLib(mainLogger); reg.setLogger(mainLogger); reg.setShowErrorMessageBoxHandler((message: string) => { alert(message); }); diff --git a/packages/lib/BaseApplication.ts b/packages/lib/BaseApplication.ts index 1b96dc408..3e8e197be 100644 --- a/packages/lib/BaseApplication.ts +++ b/packages/lib/BaseApplication.ts @@ -59,7 +59,6 @@ import Resource from './models/Resource'; import { ProfileConfig } from './services/profileConfig/types'; import initProfile from './services/profileConfig/initProfile'; import { parseShareCache } from './services/share/reducer'; - import RotatingLogs from './RotatingLogs'; const appLogger: LoggerWrapper = Logger.create('App'); @@ -69,6 +68,7 @@ const appLogger: LoggerWrapper = Logger.create('App'); interface StartOptions { keychainEnabled?: boolean; + setupGlobalLogger?: boolean; } export default class BaseApplication { @@ -738,6 +738,7 @@ export default class BaseApplication { public async start(argv: string[], options: StartOptions = null): Promise { options = { keychainEnabled: true, + setupGlobalLogger: true, ...options, }; @@ -800,18 +801,15 @@ export default class BaseApplication { const extraFlags = await this.readFlagsFromFile(`${profileDir}/flags.txt`); initArgs = { ...initArgs, ...extraFlags }; + const globalLogger = Logger.globalLogger; - - - const globalLogger = new Logger(); - globalLogger.addTarget(TargetType.File, { path: `${profileDir}/log.txt` }); - if (Setting.value('appType') === 'desktop') { - globalLogger.addTarget(TargetType.Console); + if (options.setupGlobalLogger) { + globalLogger.addTarget(TargetType.File, { path: `${profileDir}/log.txt` }); + if (Setting.value('appType') === 'desktop') { + globalLogger.addTarget(TargetType.Console); + } + globalLogger.setLevel(initArgs.logLevel); } - globalLogger.setLevel(initArgs.logLevel); - Logger.initializeGlobalLogger(globalLogger); - - reg.setLogger(Logger.create('') as Logger); // reg.dispatch = () => {}; diff --git a/packages/lib/initLib.ts b/packages/lib/initLib.ts new file mode 100644 index 000000000..f52c13d98 --- /dev/null +++ b/packages/lib/initLib.ts @@ -0,0 +1,9 @@ +import Logger from '@joplin/utils/Logger'; + +// @joplin/lib has its own copy of the Logger class, however we want it to be +// initialised with the same logger instance as the calling application, so that +// the lib and app share the same log. This initLib() function is used for this +// and must be called by any "app" package that makes use of the lib package. +export default (globalLogger: Logger) => { + Logger.initializeGlobalLogger(globalLogger); +}; diff --git a/packages/lib/testing/test-utils.ts b/packages/lib/testing/test-utils.ts index 537b0729f..e517293c1 100644 --- a/packages/lib/testing/test-utils.ts +++ b/packages/lib/testing/test-utils.ts @@ -63,6 +63,7 @@ const { S3Client } = require('@aws-sdk/client-s3'); const { Dirnames } = require('../services/synchronizer/utils/types'); import RSA from '../services/e2ee/RSA.node'; import { State as ShareState } from '../services/share/reducer'; +import initLib from '../initLib'; // Each suite has its own separate data and temp directory so that multiple // suites can be run at the same time. suiteName is what is used to @@ -173,6 +174,7 @@ logger.addTarget(TargetType.Console); logger.setLevel(LogLevel.Warn); // Set to DEBUG to display sync process in console Logger.initializeGlobalLogger(logger); +initLib(logger); BaseItem.loadClass('Note', Note); BaseItem.loadClass('Folder', Folder); @@ -916,7 +918,7 @@ class TestApp extends BaseApplication { if (!argv.includes('--profile')) { argv = argv.concat(['--profile', `tests-build/profile/${uuid.create()}`]); } - argv = await super.start(['', ''].concat(argv)); + argv = await super.start(['', ''].concat(argv), { setupGlobalLogger: false }); // For now, disable sync and encryption to avoid spurious intermittent failures // caused by them interupting processing and causing delays. diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 1768cf35c..2d33d1dfc 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -24,6 +24,7 @@ import { RouteResponseFormat, routeResponseFormat } from './utils/routeUtils'; import { parseEnv } from './env'; import storageConnectionCheck from './utils/storageConnectionCheck'; import { setLocale } from '@joplin/lib/locale'; +import initLib from '@joplin/lib/initLib'; import checkAdminHandler from './middleware/checkAdminHandler'; interface Argv { @@ -215,6 +216,7 @@ async function main() { formatInfo: `%(date_time)s: ${instancePrefix}%(prefix)s: %(message)s`, }); Logger.initializeGlobalLogger(globalLogger); + initLib(globalLogger); if (envFilePath) appLogger().info(`Env variables were loaded from: ${envFilePath}`); diff --git a/packages/server/src/utils/testing/testUtils.ts b/packages/server/src/utils/testing/testUtils.ts index c83d7a7d7..c3b025dd7 100644 --- a/packages/server/src/utils/testing/testUtils.ts +++ b/packages/server/src/utils/testing/testUtils.ts @@ -25,6 +25,7 @@ import { createCsrfToken } from '../csrf'; import { cookieSet } from '../cookies'; import { parseEnv } from '../../env'; import { URL } from 'url'; +import initLib from '@joplin/lib/initLib'; // Takes into account the fact that this file will be inside the /dist directory // when it runs. @@ -64,6 +65,7 @@ export async function makeTempFileWithContent(content: string | Buffer): Promise function initGlobalLogger() { const globalLogger = new Logger(); Logger.initializeGlobalLogger(globalLogger); + initLib(globalLogger); } let createdDbPath_: string = null; diff --git a/yarn.lock b/yarn.lock index 1bd15d44e..2f13a1edc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5007,17 +5007,6 @@ __metadata: languageName: unknown linkType: soft -"@joplin/utils@npm:~2.11": - version: 2.11.2 - resolution: "@joplin/utils@npm:2.11.2" - dependencies: - execa: 5.1.1 - fs-extra: 11.1.1 - node-fetch: 2.6.7 - checksum: 95ba577e6e5f7fb2c10e6bdb2694fdaaef31d6f650b5917b70ec35cb720b1357e182458cce894395f7109298847c21ad78932df6d3394fdbc315ecb5fde28630 - languageName: node - linkType: hard - "@joplin/utils@workspace:packages/utils, @joplin/utils@~2.12": version: 0.0.0-use.local resolution: "@joplin/utils@workspace:packages/utils" @@ -29455,7 +29444,7 @@ __metadata: version: 0.0.0-use.local resolution: "root@workspace:." dependencies: - "@joplin/utils": ~2.11 + "@joplin/utils": ~2.12 "@seiyab/eslint-plugin-react-hooks": 4.5.1-beta.0 "@types/fs-extra": 11.0.1 "@typescript-eslint/eslint-plugin": 5.60.0