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

Log refactoring #193

Merged
merged 11 commits into from
May 6, 2021
Merged

Log refactoring #193

merged 11 commits into from
May 6, 2021

Conversation

baruchiro
Copy link
Collaborator

@baruchiro baruchiro commented Feb 25, 2021

Close #141

A user should be able to send us the logs in case he faces an error.

  • Make the logger write to a file
  • Verify there is a global exception handler that logs exceptions
  • Add to the readme a description of how to send logs (either manual or automatic)
  • Make sure we printing things to the log alongside the UI (scraping events, for example)
  • Make sure we are re-assigning the console.log function, or we linting to not use it and use our custom logger instead.

Use different logger instance between main/renderer

Remove EventEmitter builders and keep only one

Do not initilize default EventEmitter
@baruchiro baruchiro marked this pull request as draft February 25, 2021 19:09
Comment on lines -1 to -11
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;
}
Copy link
Collaborator Author

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.

Comment on lines -1 to -11
/* 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;
}
Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

Comment on lines +1 to +13
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;
}
Copy link
Collaborator Author

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

  1. Print specific message depends on the Event.
  2. Print to the console or to the file, by level (because the logger argument supports both console.[level] and logger.[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
Copy link
Collaborator Author

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.

Comment on lines +12 to +16
const onError = (error: Error) => {
logger.error(error.message || error);
if (error.stack) logger.debug(error.stack);
};
logger.catchErrors({ onError });
Copy link
Collaborator Author

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)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines +34 to +40
### 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Please review

Comment on lines +63 to +64
<br>
<small>You can find the logs here: {{ logsDir }}</small>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

image

@baruchiro baruchiro marked this pull request as ready for review March 19, 2021 13:24
@baruchiro
Copy link
Collaborator Author

baruchiro commented Mar 19, 2021

@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.
But really, I just organized the code, refactor, and changed some functions and on them, I left comments.

@baruchiro baruchiro enabled auto-merge April 29, 2021 19:28
@baruchiro baruchiro disabled auto-merge May 6, 2021 15:20
@baruchiro baruchiro merged commit 47e108a into master May 6, 2021
@baruchiro baruchiro deleted the baruchiro/issue141 branch May 6, 2021 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Define and implement a flow for sending logs to us in case of an error
1 participant