简体   繁体   中英

Save the log in electron devtools to a file

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM