【问题标题】:Save the log in electron devtools to a file将电子开发工具中的日志保存到文件
【发布时间】:2018-02-26 12:45:11
【问题描述】:

我正在为渲染过程使用 angular 5 的 Electron 应用程序, 有没有办法以编程方式导出控制台?

我需要一种将日志记录数据同步到文件的方法,以便随时查看 无需打开电子开发工具并另存为选项,我需要它以编程方式

我保存自己的日志,但是如果有一个记录错误的模块,我需要获取整个控制台日志历史并将其导出到日志文件中

【问题讨论】:

  • 我正在为自己的日志使用电子日志,它工作正常,但是如果我使用的插件或模块出现错误怎么办,我需要一种方法来获取所有控制台日志

标签: javascript logging electron angular5


【解决方案1】:

您可以使用 electron-log,它是 Electron 应用程序的日志记录模块。它可以在没有电子的情况下使用。 你应该使用ngx-electron


首先,安装electron-log

npm install electron-log

在电子的主进程中需要它。

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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 2016-11-05
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多