addons/isl-server/src/FileLogger.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {Level} from './logger';
b69ab319
b69ab3110import fs from 'node:fs';
b69ab3111import util from 'node:util';
b69ab3112import {Logger} from './logger';
b69ab3113
b69ab3114/** Logger that outputs to a given filename.
b69ab3115 * Typically used for browser ISL's server. */
b69ab3116export class FileLogger extends Logger {
b69ab3117 constructor(public filename: string) {
b69ab3118 super();
b69ab3119 }
b69ab3120
b69ab3121 write(level: Level, timeStr: string, ...args: Parameters<typeof console.log>): void {
b69ab3122 const str = util.format(timeStr, this.levelToString(level), ...args) + '\n';
b69ab3123 void fs.promises.appendFile(this.filename, str);
b69ab3124 }
b69ab3125
b69ab3126 getLogFileContents() {
b69ab3127 return fs.promises.readFile(this.filename, 'utf-8');
b69ab3128 }
b69ab3129}