-
Notifications
You must be signed in to change notification settings - Fork 0
feat: logger #200
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
base: main
Are you sure you want to change the base?
feat: logger #200
Changes from all commits
6a0c83f
f0cfbb5
2cf80b2
290d51d
97eb8fb
871f04f
9f8d9f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| VITE_BASEURL=https://api.example.com | ||
| VITE_BEARERTOKEN=YOUR_BEARER_TOKEN | ||
| VITE_BEARERTOKEN=YOUR_BEARER_TOKEN | ||
| VITE_LOG_FILE_NAME = vigad.log | ||
| VITE_LOGLEVEL = error |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| import { app } from 'electron'; | ||
| import log from 'electron-log'; | ||
| import path from 'node:path'; | ||
| import fs from 'fs'; | ||
|
|
||
| function domReady( | ||
| condition: DocumentReadyState[] = ['complete', 'interactive'] | ||
| ) { | ||
|
|
@@ -110,4 +115,66 @@ contextBridge.exposeInMainWorld('electronAPI', { | |
| }); | ||
| }); | ||
| }, | ||
| saveLog: ( | ||
| message: string, | ||
| level: ElectronLogLevel = ElectronLogLevel.INFO, | ||
| logFileName: string | ||
| ) => { | ||
| //github.com/finos/SymphonyElectron/blob/0431a9f5add13cd16c19006775f1e907f3c3b2ce/src/common/logger.ts#L66 | ||
| const logDirectoryPath = path.join(app.getPath('exe'), 'logs'); | ||
|
|
||
| // Create the log directory if it doesn't exist | ||
| if (!fs.existsSync(logDirectoryPath)) { | ||
| fs.mkdirSync(logDirectoryPath); | ||
| } | ||
|
|
||
| const logFilePath = path.join(logDirectoryPath, logFileName); | ||
|
|
||
| try { | ||
| // Save log message using electron-log | ||
| log.transports.file.resolvePath = () => logFilePath; | ||
| log[level](message); | ||
|
|
||
| return { success: true, message: message }; | ||
| } catch (error) { | ||
| console.error(error); | ||
| return { success: false, message: 'Error saving log file.' }; | ||
| } | ||
|
|
||
| // const logDirecotryName = 'logs'; | ||
|
|
||
| // console.log(process.env.NODE_ENV); | ||
|
|
||
| // log.transports.file.format = '[{h}:{i}:{s}:{ms}] [{level}] {text}'; | ||
| // log.transports.file.maxSize = 5 * 1024 * 1024; // 5 MB | ||
|
|
||
| // if (process.env.NODE_ENV === 'development') { | ||
| // // Set the desired log file name | ||
| // log.transports.file.resolvePath = () => | ||
| // path.join(logDirecotryName, logFileName); | ||
|
|
||
| // // Save log message using electron-log | ||
| // log[level](message); | ||
| // } else { | ||
| // // TODO: cant save log file in the root directory of the installed application yet | ||
| // // Set the desired log file name in the root directory of the installed application | ||
| // const logFilePath = path.join(app.getPath('exe'), logFileName); | ||
| // log.transports.file.resolvePath = () => logFilePath; | ||
|
|
||
| // // Save log message using electron-log | ||
| // log[level](message); | ||
| // } | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Electron log levels enum declaration | ||
| */ | ||
| export enum ElectronLogLevel { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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', | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { 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, | ||
| fileName = import.meta.env.VITE_LOG_FILE_NAME | ||
| ) => { | ||
| logger.debug(message); | ||
| logMessages.value.push(message); | ||
| electronLogger.saveLog(message, logLevel, fileName); | ||
| }; | ||
|
|
||
| /** | ||
| * 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); | ||
| }; | ||
|
|
||
| return { | ||
| log: logMessages, | ||
| addLog, | ||
| addErrorLog, | ||
| addWarnLog, | ||
| addVerboseLog, | ||
| addDebugLog, | ||
| addSillyLog, | ||
| }; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.