我正在为渲染过程使用 angular 5 开发 Electron 应用程序, 有没有办法以编程方式导出控制台?
我需要一种将日志记录数据同步到文件的方法,这样我就可以随时查看它 无需打开 electron devtools 并另存为选项,我需要它以编程方式
我保存了自己的日志,但是如果有一个记录错误的模块怎么办我需要获取整个控制台日志历史并将其导出到日志文件
最佳答案
您可以使用 electron-log ,这是 Electron 应用程序的日志记录模块。它可以在没有 Electron 的情况下使用。 你应该使用 ngx-electron .
首先,安装electron-log
npm install electron-log
在 Electron 的主进程中需要它。
const logger = require('electron-log');
然后安装ngx-electron
npm install ngx-electron
ngx-electron
公开了一个名为 NgxElectronModule
的模块,需要将其导入到您的 AppModule
中。
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {NgxElectronModule} from 'ngx-electron';
import {AppComponent} from './app.component';
@NgModule({
declarations: [],
imports: [
BrowserModule,
NgxElectronModule
],
bootstrap: [AppComponent]
})
export class AppModule {
}
导入模块后,您可以轻松地使用 Angular DI 请求 ElectronService
。
import {Component} from '@angular/core';
import {ElectronService} from 'ngx-electron';
@Component({
selector: 'my-app',
templateUrl: 'app.html'
})
export class AppComponent {
logger
constructor(private _electronService: ElectronService) {
// this should be in init()
if(this._electronService.isElectronApp) {
this.logger = this._electronService.remote.require("electron-log");
}
}
public testLogger() {
this.logger.info('this is a message from angular');
}
}
在那之后,你应该可以在你的组件中使用 electron-log
,只需要记住 import {ElectronService} from 'ngx-electron';
和 this.logger = this._electronService.remote.require("electron-log");
在组件中。
关于javascript - 将 electron devtools 中的日志保存到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48988829/