javascript - 将 electron devtools 中的日志保存到文件中

标签 javascript logging electron angular5

我正在为渲染过程使用 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/

相关文章:

javascript - 如何使用 jquery 模拟平滑滚动?

c# - 在 Logging 中使用 StackFrame 和 MethodBase 的区别

java - 在 HttpClient 中开启日志记录

javascript - 使用全局变量来防止代码不必要地重复执行

javascript - .app 文件夹是什么?如何将其转换为 .dmg 存档?

electron - 如何在atom-shell中重置渲染器javascript上下文

JavaScript - 从 iframe 访问元素

javascript - 根据输入参数在 JavaScript 中动态生成一个函数

javascript - 返回溢出内容中的子页面 anchor

java - 我可以以编程方式设置 Spring Boot 的日志位置吗?