[MM-41999] Add additional logging for debugging, allow users to change log level (#2031)

* Add debug logging switch

* Add tests

* Mock electron-log globally in jest

* New logs for debugging

* Switch to a dropdown to choose log levels

* Fix tests

* Update wording
This commit is contained in:
Devin Binnie
2022-03-31 16:46:57 -04:00
committed by GitHub
parent 7c1fa2c78d
commit 626fea84a5
49 changed files with 261 additions and 108 deletions

View File

@@ -20,12 +20,6 @@ jest.mock('electron', () => ({
},
}));
jest.mock('electron-log', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
}));
jest.mock('main/app/utils', () => ({
getDeeplinkingURL: jest.fn(),
openDeepLink: jest.fn(),

View File

@@ -21,6 +21,8 @@ export const certificateErrorCallbacks = new Map();
// activate first app instance, subsequent instances will quit themselves
export function handleAppSecondInstance(event: Event, argv: string[]) {
log.debug('App.handleAppSecondInstance', argv);
// Protocol handler for win32
// argv: An array of the second instances (command line / deep linked) arguments
const deeplinkingUrl = getDeeplinkingURL(argv);
@@ -28,6 +30,8 @@ export function handleAppSecondInstance(event: Event, argv: string[]) {
}
export function handleAppWindowAllClosed() {
log.debug('App.handleAppWindowAllClosed');
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
@@ -36,6 +40,8 @@ export function handleAppWindowAllClosed() {
}
export function handleAppBrowserWindowCreated(event: Event, newWindow: BrowserWindow) {
log.debug('App.handleAppBrowserWindowCreated');
// Screen cannot be required before app is ready
resizeScreen(newWindow);
}
@@ -57,6 +63,8 @@ export function handleAppWillFinishLaunching() {
}
export function handleAppBeforeQuit() {
log.debug('App.handleAppBeforeQuit');
// Make sure tray icon gets removed if the user exits via CTRL-Q
destroyTray();
global.willAppQuit = true;
@@ -64,6 +72,8 @@ export function handleAppBeforeQuit() {
}
export async function handleAppCertificateError(event: Event, webContents: WebContents, url: string, error: string, certificate: Certificate, callback: (isTrusted: boolean) => void) {
log.verbose('App.handleAppCertificateError', {url, error, certificate});
const parsedURL = urlUtils.parseURL(url);
if (!parsedURL) {
return;

View File

@@ -8,6 +8,8 @@ import Config from 'common/config';
import {handleConfigUpdate} from 'main/app/config';
import {addNewServerModalWhenMainWindowIsShown} from 'main/app/intercom';
import {setLoggingLevel} from 'main/app/utils';
import WindowManager from 'main/windows/windowManager';
import AutoLauncher from 'main/AutoLauncher';
@@ -22,15 +24,12 @@ jest.mock('electron', () => ({
on: jest.fn(),
},
}));
jest.mock('electron-log', () => ({
info: jest.fn(),
error: jest.fn(),
}));
jest.mock('main/app/utils', () => ({
handleUpdateMenuEvent: jest.fn(),
updateSpellCheckerLocales: jest.fn(),
updateServerInfos: jest.fn(),
setLoggingLevel: jest.fn(),
}));
jest.mock('main/app/intercom', () => ({
addNewServerModalWhenMainWindowIsShown: jest.fn(),
@@ -105,5 +104,12 @@ describe('main/app/config', () => {
value: originalPlatform,
});
});
it('should set logging level correctly', () => {
handleConfigUpdate({logLevel: 'info'});
expect(setLoggingLevel).toBeCalledWith('info');
handleConfigUpdate({logLevel: 'debug'});
expect(setLoggingLevel).toBeCalledWith('debug');
});
});
});

View File

@@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import {app, ipcMain} from 'electron';
import log from 'electron-log';
import log, {LogLevel} from 'electron-log';
import {CombinedConfig} from 'types/config';
@@ -15,7 +15,7 @@ import {refreshTrayImages} from 'main/tray/tray';
import WindowManager from 'main/windows/windowManager';
import {addNewServerModalWhenMainWindowIsShown} from './intercom';
import {handleUpdateMenuEvent, updateServerInfos, updateSpellCheckerLocales} from './utils';
import {handleUpdateMenuEvent, setLoggingLevel, updateServerInfos, updateSpellCheckerLocales} from './utils';
let didCheckForAddServerModal = false;
@@ -24,6 +24,9 @@ let didCheckForAddServerModal = false;
//
export function handleConfigUpdate(newConfig: CombinedConfig) {
log.debug('App.Config.handleConfigUpdate');
log.silly('App.Config.handleConfigUpdate', newConfig);
if (!newConfig) {
return;
}
@@ -62,11 +65,16 @@ export function handleConfigUpdate(newConfig: CombinedConfig) {
}
}
log.info('Log level set to:', newConfig.logLevel);
setLoggingLevel(newConfig.logLevel as LogLevel);
handleUpdateMenuEvent();
ipcMain.emit(EMIT_CONFIGURATION, true, newConfig);
}
export function handleDarkModeChange(darkMode: boolean) {
log.debug('App.Config.handleDarkModeChange', darkMode);
refreshTrayImages(Config.trayIconTheme);
WindowManager.sendToRenderer(DARK_MODE_CHANGE, darkMode);
WindowManager.updateLoadingScreenDarkMode(darkMode);

View File

@@ -63,12 +63,6 @@ jest.mock('electron-devtools-installer', () => {
const isDev = false;
jest.mock('electron-is-dev', () => isDev);
jest.mock('electron-log', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
}));
jest.mock('../../../electron-builder.json', () => ([
{
name: 'Mattermost',
@@ -245,7 +239,7 @@ describe('main/app/initialize', () => {
path.resolve.mockImplementation((base, p) => `${base}/${p}`);
session.defaultSession.on.mockImplementation((event, cb) => {
if (event === 'will-download') {
cb(null, item, {id: 0});
cb(null, item, {id: 0, getURL: jest.fn()});
}
});

View File

@@ -295,6 +295,7 @@ function initializeAfterAppReady() {
if (typeof Config.canUpgrade === 'undefined') {
// windows might not be ready, so we have to wait until it is
Config.once('update', () => {
log.debug('Initialize.checkForUpdates');
if (Config.canUpgrade && Config.autoCheckForUpdates) {
setTimeout(() => {
updateManager.checkForUpdates(false);
@@ -342,6 +343,7 @@ function initializeAfterAppReady() {
// listen for status updates and pass on to renderer
UserActivityMonitor.on('status', (status) => {
log.debug('Initialize.UserActivityMonitor.on(status)', status);
WindowManager.sendToMattermostViews(USER_ACTIVITY_UPDATE, status);
});
@@ -354,6 +356,7 @@ function initializeAfterAppReady() {
setupBadge();
defaultSession.on('will-download', (event, item, webContents) => {
log.debug('Initialize.will-download', {item, sourceURL: webContents.getURL()});
const filename = item.getFilename();
const fileElements = filename.split('.');
const filters = [];

View File

@@ -19,6 +19,8 @@ import {handleAppBeforeQuit} from './app';
import {updateServerInfos} from './utils';
export function handleReloadConfig() {
log.debug('Intercom.handleReloadConfig');
Config.reload();
WindowManager.handleUpdateConfig();
}
@@ -38,14 +40,18 @@ export function handleQuit(e: IpcMainEvent, reason: string, stack: string) {
}
export function handleSwitchServer(event: IpcMainEvent, serverName: string) {
log.silly('Intercom.handleSwitchServer', serverName);
WindowManager.switchServer(serverName);
}
export function handleSwitchTab(event: IpcMainEvent, serverName: string, tabName: string) {
log.silly('Intercom.handleSwitchTab', {serverName, tabName});
WindowManager.switchTab(serverName, tabName);
}
export function handleCloseTab(event: IpcMainEvent, serverName: string, tabName: string) {
log.debug('Intercom.handleCloseTab', {serverName, tabName});
const teams = Config.teams;
teams.forEach((team) => {
if (team.name === serverName) {
@@ -62,6 +68,8 @@ export function handleCloseTab(event: IpcMainEvent, serverName: string, tabName:
}
export function handleOpenTab(event: IpcMainEvent, serverName: string, tabName: string) {
log.debug('Intercom.handleOpenTab', {serverName, tabName});
const teams = Config.teams;
teams.forEach((team) => {
if (team.name === serverName) {
@@ -83,6 +91,7 @@ export function addNewServerModalWhenMainWindowIsShown() {
handleNewServerModal();
} else {
mainWindow.once('show', () => {
log.debug('Intercom.addNewServerModalWhenMainWindowIsShown.show');
handleNewServerModal();
});
}
@@ -90,6 +99,8 @@ export function addNewServerModalWhenMainWindowIsShown() {
}
export function handleNewServerModal() {
log.debug('Intercom.handleNewServerModal');
const html = getLocalURLString('newServer.html');
const modalPreload = getLocalPreload('modalPreload.js');
@@ -120,6 +131,8 @@ export function handleNewServerModal() {
}
export function handleEditServerModal(e: IpcMainEvent, name: string) {
log.debug('Intercom.handleEditServerModal', name);
const html = getLocalURLString('editServer.html');
const modalPreload = getLocalPreload('modalPreload.js');
@@ -151,6 +164,8 @@ export function handleEditServerModal(e: IpcMainEvent, name: string) {
}
export function handleRemoveServerModal(e: IpcMainEvent, name: string) {
log.debug('Intercom.handleRemoveServerModal', name);
const html = getLocalURLString('removeServer.html');
const modalPreload = getLocalPreload('modalPreload.js');
@@ -189,10 +204,13 @@ export function handleRemoveServerModal(e: IpcMainEvent, name: string) {
}
export function handleMentionNotification(event: IpcMainEvent, title: string, body: string, channel: {id: string}, teamId: string, url: string, silent: boolean, data: MentionData) {
log.debug('Intercom.handleMentionNotification', {title, body, channel, teamId, url, silent, data});
displayMention(title, body, channel, teamId, url, silent, event.sender, data);
}
export function handleOpenAppMenu() {
log.debug('Intercom.handleOpenAppMenu');
const windowMenu = Menu.getApplicationMenu();
if (!windowMenu) {
log.error('No application menu found');
@@ -206,6 +224,8 @@ export function handleOpenAppMenu() {
}
export async function handleSelectDownload(event: IpcMainInvokeEvent, startFrom: string) {
log.debug('Intercom.handleSelectDownload', startFrom);
const message = 'Specify the folder where files will download';
const result = await dialog.showOpenDialog({defaultPath: startFrom || Config.downloadLocation,
message,
@@ -215,6 +235,8 @@ export async function handleSelectDownload(event: IpcMainInvokeEvent, startFrom:
}
export function handleUpdateLastActive(event: IpcMainEvent, serverName: string, viewName: string) {
log.debug('Intercom.handleUpdateLastActive', {serverName, viewName});
const teams = Config.teams;
teams.forEach((team) => {
if (team.name === serverName) {

View File

@@ -35,11 +35,6 @@ jest.mock('electron', () => ({
},
}));
jest.mock('electron-log', () => ({
info: jest.fn(),
error: jest.fn(),
}));
jest.mock('common/config', () => ({
set: jest.fn(),
}));

View File

@@ -6,7 +6,7 @@ import fs from 'fs';
import path from 'path';
import {app, BrowserWindow, Menu, Rectangle, Session, session, dialog, nativeImage} from 'electron';
import log from 'electron-log';
import log, {LevelOption} from 'electron-log';
import {MigrationInfo, TeamWithTabs} from 'types/config';
import {RemoteInfo} from 'types/server';
@@ -83,6 +83,8 @@ function openExtraTabs(data: Array<RemoteInfo | string | undefined>, team: TeamW
}
export function handleUpdateMenuEvent() {
log.debug('Utils.handleUpdateMenuEvent');
const aMenu = createAppMenu(Config, updateManager);
Menu.setApplicationMenu(aMenu);
aMenu.addListener('menu-will-close', WindowManager.focusBrowserView);
@@ -152,6 +154,7 @@ function getValidWindowPosition(state: Rectangle) {
export function resizeScreen(browserWindow: BrowserWindow) {
function handle() {
log.debug('Utils.resizeScreen.handle');
const position = browserWindow.getPosition();
const size = browserWindow.getSize();
const validPosition = getValidWindowPosition({
@@ -172,6 +175,7 @@ export function resizeScreen(browserWindow: BrowserWindow) {
}
function flushCookiesStore(session: Session) {
log.debug('Utils.flushCookiesStore');
session.cookies.flushStore().catch((err) => {
log.error(`There was a problem flushing cookies:\n${err}`);
});
@@ -248,3 +252,8 @@ export function migrateMacAppStore() {
log.error('MAS: An error occurred importing the existing configuration', e);
}
}
export function setLoggingLevel(level: LevelOption) {
log.transports.console.level = level;
log.transports.file.level = level;
}