Skip to content

Commit

Permalink
feat: Add logging to file
Browse files Browse the repository at this point in the history
Use -l option to log to a file.
  • Loading branch information
schw4rzlicht committed Jun 11, 2020
1 parent 34b3164 commit aab4e4e
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}

0 comments on commit aab4e4e

Please sign in to comment.