Skip to content

Commit

Permalink
limit file loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Dec 21, 2023
1 parent 7dfbd39 commit 543f823
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,10 @@ export const LevelNames = {
[LogLevel.Error]: "error",
};

function getFileWriter() {
// TODO: electron
return undefined;

class FileWriter {
public write(str: string) {}

public getPath() {
return undefined;
}
}

return new FileWriter();
}

export class Log {
public static level = LogLevel.Debug;
public static history: string[] = [];

private static file = getFileWriter();

public static filePath() {
return Log.file ? Log.file.getPath() : undefined;
}

public static trace(prefix: string, ...data: any[]) {
Log.log(LogLevel.Trace, prefix, ...data);
}
Expand Down Expand Up @@ -78,7 +58,11 @@ export class Log {
return str;
}

private static logToFile(fmt: string, ...data: any[]) {
private static logToFile(level: LogLevel, fmt: string, ...data: any[]) {
if (level < Log.level) {
return;
}

for (const d of data) {
fmt += " ";

Expand All @@ -88,10 +72,6 @@ export class Log {
fmt += JSON.stringify(d);
}
}

if (Log.file) {
Log.file.write(fmt);
}
Log.history.push(fmt);
}

Expand All @@ -100,26 +80,26 @@ export class Log {
case LogLevel.Trace: {
const str = this.logFmtStr(level, prefix, data);
console.debug(str, ...data);
this.logToFile(str, ...data);
this.logToFile(level, str, ...data);
break;
}
case LogLevel.Debug:
case LogLevel.Info: {
const str = this.logFmtStr(level, prefix, data);
console.log(str, ...data);
this.logToFile(str, ...data);
this.logToFile(level, str, ...data);
break;
}
case LogLevel.Warning: {
const str = this.logFmtStr(level, prefix, data);
console.warn(str, ...data);
this.logToFile(str, ...data);
this.logToFile(level, str, ...data);
break;
}
case LogLevel.Error: {
const str = this.logFmtStr(level, prefix, data);
console.error(str, ...data);
this.logToFile(str, ...data);
this.logToFile(level, str, ...data);
break;
}
default:
Expand Down

0 comments on commit 543f823

Please sign in to comment.