-
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
Changes from 3 commits
79606f4
17e3b38
4334f0a
904866e
b46cbd9
72b0b86
281fa20
9c7177a
6278662
8ad8f28
82b3f36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. If you will try to |
||
import fs from 'fs'; | ||
import { EOL } from 'os'; | ||
|
||
declare module 'electron-log' { | ||
interface ElectronLog { | ||
getLastLines: (n: number) => string | ||
} | ||
} | ||
export type Logger = LogFunctions | ||
export type Levels = LogLevel | ||
|
||
export default function CreateLogger(app: App) { | ||
logger.info(`Welcome to ${app.getName()} log`); | ||
logger.info(`Version: ${app.getVersion()}`); | ||
logger.info(`Welcome to ${App.getName()} log`); | ||
logger.info(`Version: ${App.getVersion()}`); | ||
|
||
const onError = (error: Error) => { | ||
logger.error(error.message ? error.message : error); | ||
if (error.stack) logger.debug(error.stack); | ||
}; | ||
logger.catchErrors({ onError }); | ||
const onError = (error: Error) => { | ||
logger.error(error.message || error); | ||
if (error.stack) logger.debug(error.stack); | ||
}; | ||
logger.catchErrors({ onError }); | ||
Comment on lines
+13
to
+17
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. Note, this catching only the 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
logger.getLastLines = (n) => { | ||
const lines = fs.readFileSync(logger.transports.file.getFile().path).toString().split(EOL); | ||
const lastLines = lines.slice(lines.length - n); | ||
return lastLines.join(EOL); | ||
}; | ||
export const getLastLines = (n: number) => { | ||
const lines = fs.readFileSync(logger.transports.file.getFile().path).toString().split(EOL); | ||
const lastLines = lines.slice(lines.length - n); | ||
return lastLines.join(EOL); | ||
}; | ||
|
||
return logger; | ||
} | ||
export default logger as Logger; |
This file was deleted.
This file was deleted.
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 theeventEmitters
, 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
logger
argument supports bothconsole.[level]
andlogger.[level]