Save the log in electron devtools to a file

只愿长相守 提交于 2019-12-11 09:45:26

问题


I am working on Electron app with angular 5 for the rendering process, is there is a way to export the console programmatically?

I need a way to synchronize the logging data to file so, I can review it anytime without opening electron devtools and save as option, I need it programmatically

I save my own logs, but what if there is a module that logging an error i need to get whole console log history and export it to log file


回答1:


You can use electron-log, which is a logging module for Electron application. It can be used without Electron. And you should use ngx-electron.


Firstly, install electron-log

npm install electron-log

Require it in the electron's main process.

const logger = require('electron-log');

Then install ngx-electron

npm install ngx-electron

ngx-electron is exposing a module called NgxElectronModule which needs to be imported in your 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 {

}

Once the module has been imported, you can easily use angular DI to ask for 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');
    }
}

After that, you should be able to use electron-log in your components, just remember import {ElectronService} from 'ngx-electron';, and this.logger = this._electronService.remote.require("electron-log"); in the components.



来源:https://stackoverflow.com/questions/48988829/save-the-log-in-electron-devtools-to-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!