簡體   English   中英

將電子devtools中的日志保存到文件中

[英]Save the log in electron devtools to a file

我正在使用角度為5的Electron應用程序進行渲染過程,有沒有辦法以編程方式導出控制台?

我需要一種方法來將日志數據同步到文件,所以,我可以隨時查看它而無需打開電子devtools並保存為選項,我需要它以編程方式

我保存自己的日志,但如果有一個模塊記錄錯誤我需要獲取整個控制台日志歷史並將其導出到日志文件

您可以使用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 {

}

導入模塊后,您可以輕松使用角度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"); 在組件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM