From aab4e4e595c645879820ca17239f858855192d1f Mon Sep 17 00:00:00 2001 From: Julian Rabe Date: Thu, 11 Jun 2020 15:53:24 +0200 Subject: [PATCH] feat: Add logging to file Use -l option to log to a file. --- src/lib/Logger.ts | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/lib/Logger.ts b/src/lib/Logger.ts index 1165243..a9dfe38 100644 --- a/src/lib/Logger.ts +++ b/src/lib/Logger.ts @@ -1,60 +1,51 @@ import chalk = require("chalk"); import Fs = require("fs"); -const stripAnsiStream = require("strip-ansi-stream"); +const stripAnsi = require("strip-ansi"); export class Logger { private logfile: Fs.WriteStream; - private stripAnsiStream: any; setLogfile(logfile: string) { this.end(); this.logfile = Fs.createWriteStream(logfile, {flags: "a"}); - this.stripAnsiStream = stripAnsiStream(); - this.stripAnsiStream.on("data", (data: string) => { - this.logfile.write(data); - this.logfile._final(() => {}); - }); } end() { - if(this.stripAnsiStream) { - this.stripAnsiStream.end(); - } - if (this.logfile) { + if(this.logfile) { this.logfile.end(); } } toLogfile(message: string) { - if (this.stripAnsiStream && this.stripAnsiStream.writable) { - this.stripAnsiStream.write(message); + if (this.logfile) { + this.logfile.write(stripAnsi(`${new Date().toISOString()} ${message}\n`)); } } error(message: string) { console.error(chalk`❌ {bold.red ${message}}`); - this.toLogfile(`ERROR: ${message}\n`); + this.toLogfile(`ERROR: ${message}`); } warning(message: string) { console.warn(chalk`⚠️ {yellow ${message}}`); - this.toLogfile(`WARNING: ${message}\n`); + this.toLogfile(`WARNING: ${message}`); } confirm(message: string): void { console.log(chalk`✅ {green ${message}}`); - this.toLogfile(`SUCCESS: ${message}\n`); + this.toLogfile(`SUCCESS: ${message}`); } socketMessage(message: string) { console.log(chalk`{inverse ${message}}`); - this.toLogfile(`=== ${message} ===\n`); + this.toLogfile(`=== ${message} ===`); } channelMessage(channel: string, message: string) { console.log(chalk`{bgGreen.black ${channel} }: ${message}`); - this.toLogfile(`${channel}: ${message}\n`); + this.toLogfile(`${channel}: ${message}`); } }