Init logging

This commit is contained in:
Manav Rathi 2024-03-27 15:34:29 +05:30
parent 27047da08b
commit c38542dbfb
No known key found for this signature in database
2 changed files with 29 additions and 13 deletions

View file

@ -14,7 +14,6 @@ import serveNextAt from "next-electron-server";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { isDev } from "./main/general";
import {
addAllowOriginHeader,
createWindow,
@ -28,7 +27,7 @@ import {
setupTrayItem,
} from "./main/init";
import { attachFSWatchIPCHandlers, attachIPCHandlers } from "./main/ipc";
import { logErrorSentry, setupLogging } from "./main/log";
import { initLogging, logErrorSentry } from "./main/log";
import { initWatcher } from "./services/chokidar";
let appIsQuitting = false;
@ -135,8 +134,6 @@ function setupAppEventEmitter(mainWindow: BrowserWindow) {
}
const main = () => {
setupLogging(isDev);
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
@ -145,6 +142,7 @@ const main = () => {
let mainWindow: BrowserWindow;
initLogging();
setupRendererServer();
handleDockIconHideOnAutoLaunch();
increaseDiskCache();
@ -161,9 +159,9 @@ const main = () => {
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
// Emitted once, when Electron has finished initializing.
//
// Note that some Electron APIs can only be used after this event occurs.
app.on("ready", async () => {
logSystemInfo();
mainWindow = await createWindow();

View file

@ -1,15 +1,25 @@
import log from "electron-log";
import { isDev } from "./general";
export function setupLogging(isDev?: boolean) {
/**
* Initialize logging in the main process.
*
* This will set our underlying logger up to log to a file named `ente.log`,
*
* - on Linux at ~/.config/ente/logs/main.log
* - on macOS at ~/Library/Logs/ente/main.log
* - on Windows at %USERPROFILE%\AppData\Roaming\ente\logs\main.log
*
* On dev builds, it will also log to the console.
*/
const initLogging = () => {
log.transports.file.fileName = "ente.log";
log.transports.file.maxSize = 50 * 1024 * 1024; // 50MB;
if (!isDev) {
log.transports.console.level = false;
}
log.transports.file.format =
"[{y}-{m}-{d}T{h}:{i}:{s}{z}] [{level}]{scope} {text}";
}
"[{y}-{m}-{d}T{h}:{i}:{s}{z}] [{level}] {text}";
if (!isDev) log.transports.console.level = false;
};
export const logToDisk = (message: string) => {
log.info(message);
@ -60,6 +70,14 @@ const logDebug = (message: () => string) => {
if (isDev) log.debug(() => message);
};
/**
* Ente's logger.
*
* This is an object that provides three functions to log at the corresponding
* levels - error, info or debug.
*
* {@link initLogging} needs to be called once before using any of these.
*/
export default {
/**
* Log an error message with an optional associated error object.