1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-06 09:19:22 +02:00

Chore: Trying to fix logger (#8610)

This commit is contained in:
Laurent Cozic
2023-08-04 10:57:54 +01:00
committed by GitHub
parent ea7c7f6447
commit 887c271853
12 changed files with 45 additions and 26 deletions

View File

@@ -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: {},
};
},
};
});

View File

@@ -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');

View File

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

View File

@@ -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<any> {
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 = () => {};

9
packages/lib/initLib.ts Normal file
View File

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

View File

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

View File

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

View File

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