Skip to content

Commit 57ee199

Browse files
committed
[logging] AppLogger uses electron-log package
1 parent 2005ccc commit 57ee199

5 files changed

Lines changed: 34 additions & 15 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Added
99
- **[log-creation]:** adds EventLog and EventSource creation functionality inside log-selection modal
10+
- **[Packages]:** adds AppLogger using electron-log package
1011

1112
### Changed
1213

main.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { app, BrowserWindow, screen } from 'electron';
22
import {autoUpdater} from 'electron-updater';
33
import * as path from 'path';
44
import * as url from 'url';
5+
const electronLog = require('electron-log');
56

67
let win, serve;
78
const args = process.argv.slice(1);
89
serve = args.some(val => val === '--serve');
910

1011
function createWindow() {
11-
1212
//const electronScreen = screen;
1313
//const size = electronScreen.getPrimaryDisplay().workAreaSize;
1414
const size = {height: 768,width: 1024};
@@ -64,6 +64,7 @@ try {
6464
// Some APIs can only be used after this event occurs.
6565
app.on('ready', () => {
6666
createWindow();
67+
electronLog.info("app started!!!");
6768
//setupAutoUpdaterLogging();
6869
autoUpdater.checkForUpdatesAndNotify();
6970
});
@@ -93,8 +94,7 @@ try {
9394
//#region autoUpdater
9495
function setupAutoUpdaterLogging() {
9596
//%USERPROFILE%\AppData\Roaming\event-viewer-pp\logs\main.log
96-
const log = require("electron-log");
97-
log.transports.file.level = "debug";
98-
autoUpdater.logger = log;
97+
electronLog.transports.file.level = "debug";
98+
autoUpdater.logger = electronLog;
9999
}
100100
//#endregion

src/app/services/AppLogger.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import * as fs from 'fs';
2-
import * as path from 'path';
1+
// const log = require('electron-log');
2+
import log from 'electron-log';
3+
log.transports.file.fileName = 'main.log';
4+
5+
const debugLog = log.create('debugLog');
6+
debugLog.transports.file.level = 'debug';
7+
debugLog.transports.file.fileName = 'debug.log';
8+
9+
// const log = require("electron-log");
310

411
export class AppLogger {
512
//#region Instance
@@ -15,22 +22,31 @@ export class AppLogger {
1522

1623

1724
//#region Properties & Constructor
18-
private _logsPath :string;
1925
private constructor() {
20-
this._logsPath = path.join(process.env.APPDATA, "event-viewer-pp", "logs.txt");
21-
fs.writeFileSync(this._logsPath, '');
2226
}
2327
//#endregion Properties & Constructor
2428

2529

2630
//#region Public Api
27-
public log(message:string): void {
28-
console.log(message);
29-
this.logToFile(message);
31+
public logInfo(message: string) {
32+
log.info(message);
3033
}
3134

32-
public logToFile(message: string): void {
33-
fs.appendFileSync(this._logsPath,`-----\n${new Date().toString()}\n${message}`);
35+
public logErrorMessage(message: string) {
36+
log.error(message);
3437
}
38+
39+
public logError(e: Error) {
40+
log.error(e);
41+
}
42+
43+
public logWarn(message: string) {
44+
log.warn(message);
45+
}
46+
47+
public logDebug(message: string) {
48+
debugLog.debug(message);
49+
}
50+
3551
//#endregion Public Api
3652
}

src/app/services/powershell/powershell-command-executor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class PsCommandExecutor {
3030
*/
3131
public static executeCommand(command: string, ignoreError?:boolean, timeout?:number): Promise<string> {
3232

33-
//commands.length > 1 ? console.table(commands) : console.log(commands.last()); //TODO REMOVE
3433
const _ps = this._initShell();
3534
const commandPromise: Promise<string> = _ps.addCommand('[Console]::OutputEncoding = [System.Text.Encoding]::UTF8')
3635
.then(() => _ps.addCommand(command))

src/app/services/powershell/powershell-commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Event } from "../../types/Event";
22
import { EventLog } from "../../types/EventLog";
33
import { GlobalUtils } from "../global-utils";
44
import { PsCommandExecutor } from "./powershell-command-executor";
5+
import { AppLogger } from "../AppLogger";
56

67
export class PowershellCommands {
78

@@ -49,6 +50,7 @@ export class PowershellCommands {
4950
if (computerName) command += ` -ComputerName "${computerName}"`;
5051
command += ' -List';
5152

53+
AppLogger.getInstance().logDebug(command);
5254
return PsCommandExecutor.executeCommand(command, false, 90)
5355
.then(output => PowershellCommands._parseEventViewersList(output))
5456
.then(events => {
@@ -58,6 +60,7 @@ export class PowershellCommands {
5860
}
5961

6062
private static _parseEventViewersList(output: string): EventLog[] {
63+
AppLogger.getInstance().logDebug(output);
6164
const regex = /\S*\s+\d+\s+\D+\s+([\d|,]+)\s+(.*)/s;
6265

6366
return GlobalUtils.splitLines(output) //get each line

0 commit comments

Comments
 (0)