-
Notifications
You must be signed in to change notification settings - Fork 45
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
Log refactoring #193
Log refactoring #193
Conversation
Use different logger instance between main/renderer Remove EventEmitter builders and keep only one Do not initilize default EventEmitter
import { BudgetTrackingEventEmitter, EventPublisher } from './EventEmitter'; | ||
|
||
export function buildCompositeEventPublisher(eventPublishers: EventPublisher[]): EventPublisher { | ||
const compositeEmitter = new BudgetTrackingEventEmitter(); | ||
|
||
compositeEmitter.onAny(((eventName, eventData) => { | ||
// @ts-ignore | ||
eventPublishers.map((eventPublisher) => eventPublisher.emit(eventName, eventData)); | ||
})); | ||
return compositeEmitter; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a special logic, that should be part of the "eventEmitters" tools. Anyone knows how to write a function like this.
You may remember my conflict with the "EventEmitter", so I removed some codes here, to remove complexity, since all we want from the "backend user" is to give us an object that only implement an interface with emit
.
/* eslint-disable class-methods-use-this */ | ||
import { BudgetTrackingEventEmitter, EventPublisher } from '@/backend/eventEmitters/EventEmitter'; | ||
|
||
export function buildConsoleEmitter(): EventPublisher { | ||
const consoleEmitter = new BudgetTrackingEventEmitter(); | ||
consoleEmitter.onAny((eventName, eventData) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`${eventName}:`, eventData); | ||
}); | ||
return consoleEmitter; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As backend (think about the option to implement CLI in the future, for example), you not sure you will print to the console, you leave it to the "backend user" decision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But see the new loggerEmitter
, it is very similar.
import { BudgetTrackingEventEmitter, EventPublisher } from '@/backend/eventEmitters/EventEmitter'; | ||
|
||
interface Logger { | ||
info: (...params: any[]) => void | ||
} | ||
|
||
export function buildLoggerEmitter(logger: Logger): EventPublisher { | ||
const loggerEmitter = new BudgetTrackingEventEmitter(); | ||
loggerEmitter.onAny((eventName, eventData) => { | ||
logger.info(`${eventName}:`, eventData); | ||
}); | ||
return loggerEmitter; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a very smart method, but it is here for a reason.
Here, in the eventEmitters
folder, in the context of the eventEmitters
, we know the event types, so we can parse them to string (with special care for each event), and direct them to the right level.
So, continuously, we can use this method to
- Print specific message depends on the Event.
- Print to the console or to the file, by level (because the
logger
argument supports bothconsole.[level]
andlogger.[level]
import { App } from 'electron'; | ||
import logger from 'electron-log'; | ||
import { App } from '@/app-globals'; | ||
import logger, { LogFunctions, LogLevel } from 'electron-log'; // eslint-disable-line no-restricted-imports |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you will try to import logger from 'electron-log'
, you will get an ESLint error, because we want you to use our custom-defined logger.
But here we have to import the logger from electron-log
to configure it.
const onError = (error: Error) => { | ||
logger.error(error.message || error); | ||
if (error.stack) logger.debug(error.stack); | ||
}; | ||
logger.catchErrors({ onError }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, this catching only the UnhandledRejection
.
At least for the Vue part, we need to open a new issue to catch exceptions during the render process (I even can't say what exactly happened during such case. For in React, for example, the component tree will crash- if you're not using error boundary)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Report a problem | ||
|
||
We are still in beta, and you may find errors. | ||
Please use the **REPORT A PROBLEM** button in the app to report to us. | ||
|
||
Use this button to find the **logs folder** as well. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please review
<br> | ||
<small>You can find the logs here: {{ logsDir }}</small> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brafdlog Ready for review. I skipping the two checkboxes. This PR is already big. Of course, it is not so complicated, and I left comments about the changes, but when you're coming without context, it is difficult to understand. |
Close #141