Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: logger #200

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import log from 'electron-log';

function domReady(
condition: DocumentReadyState[] = ['complete', 'interactive']
) {
Expand Down Expand Up @@ -110,4 +112,26 @@ contextBridge.exposeInMainWorld('electronAPI', {
});
});
},
saveLog: (
message: string,
level: ElectronLogLevel = ElectronLogLevel.INFO,
BeierKevin marked this conversation as resolved.
Show resolved Hide resolved
logFile = 'vigad.log'
BeierKevin marked this conversation as resolved.
Show resolved Hide resolved
) => {
// Set the desired log file name
log.transports.file.fileName = logFile;
// Save log message using electron-log
log[level](message);
},
});

/**
* Electron log levels enum declaration
*/
export enum ElectronLogLevel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just updating the values for these enums to numbers (as in .env) may be a good solution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However you need to make sure that eg. loglevel INFO also includes everything WARN & ERROR (this only makes sense)

Copy link
Contributor

@BeierKevin BeierKevin Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we still need to discuss what we really want out of this

INFO = 'info',
WARN = 'warn',
ERROR = 'error',
VERBOSE = 'verbose',
DEBUG = 'debug',
SILLY = 'silly',
}
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@
"@vueuse/components": "^10.1.2",
"@vueuse/core": "^10.1.2",
"clipboardy": "^3.0.0",
"electron-log": "^4.4.8",
"randexp": "^0.5.3",
"roboto-fontface": "^0.10.0",
"tesseract.js": "^3.0.3",
"tplant": "^3.1.0",
"tslog": "^4.8.2",
"vue-router": "^4.2.2",
"vue3-drag-resize": "^2.0.5",
"vuetify": "^3.3.2"
Expand Down
11 changes: 11 additions & 0 deletions src/composables/useLogger/electron-log-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Electron log levels enum
*/
export enum ElectronLogLevel {
INFO = 'info',
WARN = 'warn',
ERROR = 'error',
VERBOSE = 'verbose',
DEBUG = 'debug',
SILLY = 'silly',
}
92 changes: 92 additions & 0 deletions src/composables/useLogger/useLogger.ts
BeierKevin marked this conversation as resolved.
Show resolved Hide resolved
BeierKevin marked this conversation as resolved.
Show resolved Hide resolved
BeierKevin marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logfile should be created in the root directory of the installation location (and not somewhere in appdata). Maybe in some subfolder logs/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be tested for an actually installed application as well as in dev environment

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { onMounted, onUnmounted, ref } from 'vue';
import { Logger } from 'tslog';
import { ElectronLogLevel } from './electron-log-level';

/**
* Create a logger instance
* @type {Logger}
*/
const logger = new Logger();

/**
* Reference to the electron logger in the main process
* @type {any}
*/
const electronLogger = (window as any).electronAPI;

/**
* Logger composable
*/
export default function useLogger() {
const logMessages = ref<string[]>([]);

/**
* Function to add log messages
* @param {string} message - The log message
* @param {ElectronLogLevel} [logLevel=ElectronLogLevel.INFO] - The log level
*/
const addLog = (message: string, logLevel = ElectronLogLevel.INFO) => {
logger.debug(message);
logMessages.value.push(message);
electronLogger.saveLog(message, logLevel);
};

/**
* Function to add warning log messages
* @param {string} message - The log message
*/
const addWarnLog = (message: string) => {
addLog(message, ElectronLogLevel.WARN);
};

/**
* Function to add error log messages
* @param {string} message - The log message
*/
const addErrorLog = (message: string) => {
addLog(message, ElectronLogLevel.ERROR);
};

/**
* Function to add verbose log messages
* @param {string} message - The log message
*/
const addVerboseLog = (message: string) => {
addLog(message, ElectronLogLevel.VERBOSE);
};

/**
* Function to add debug log messages
* @param {string} message - The log message
*/
const addDebugLog = (message: string) => {
addLog(message, ElectronLogLevel.DEBUG);
};

/**
* Function to add silly log messages
* @param {string} message - The log message
*/
const addSillyLog = (message: string) => {
addLog(message, ElectronLogLevel.SILLY);
};

// Lifecycle hooks
onMounted(() => {
logger.info('Logger initialized');
});

onUnmounted(() => {
logger.info('Logger terminated');
});

return {
log: logMessages,
addLog,
addErrorLog,
addWarnLog,
addVerboseLog,
addDebugLog,
addSillyLog,
};
}