| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Level} from './logger'; |
| 9 | |
| 10 | import fs from 'node:fs'; |
| 11 | import util from 'node:util'; |
| 12 | import {Logger} from './logger'; |
| 13 | |
| 14 | /** Logger that outputs to a given filename. |
| 15 | * Typically used for browser ISL's server. */ |
| 16 | export class FileLogger extends Logger { |
| 17 | constructor(public filename: string) { |
| 18 | super(); |
| 19 | } |
| 20 | |
| 21 | write(level: Level, timeStr: string, ...args: Parameters<typeof console.log>): void { |
| 22 | const str = util.format(timeStr, this.levelToString(level), ...args) + '\n'; |
| 23 | void fs.promises.appendFile(this.filename, str); |
| 24 | } |
| 25 | |
| 26 | getLogFileContents() { |
| 27 | return fs.promises.readFile(this.filename, 'utf-8'); |
| 28 | } |
| 29 | } |
| 30 | |