import { schema } from '@osd/config-schema' ; import { LogRecord , Layout , DisposableAppender } from '@osd/logging' ; import { createWriteStream , WriteStream } from 'fs' ; import { Layouts , LayoutConfigType } from '../../layouts/layouts' ; export interface FileAppenderConfig { kind : 'file' ; layout : LayoutConfigType ; path : string ; } internal export class FileAppender implements DisposableAppender { public static configSchema = schema . object ( { kind : schema . literal ( 'file' ) , layout : Layouts . configSchema , path : schema . string ( ) , } ) ; /**
     * Writable file stream to write formatted `LogRecord` to.
     */ private outputStream ? : WriteStream ; param layout param path constructor ( private readonly layout , private readonly path ) { } param record public append ( record ) { if ( this . outputStream === undefined ) { this . outputStream = createWriteStream ( this . path , { encoding : 'utf8' , flags : 'a' , } ) ; } this . outputStream . write ( `${ this . layout . format ( record ) }\n` ) ; } /**
     * Disposes `FileAppender`. Waits for the underlying file stream to be completely flushed and closed.
     */ public async dispose ( ) { await new Promise ( ( resolve ) => { if ( this . outputStream === undefined ) { return resolve ( ) ; } this . outputStream . end ( ( ) => { this . outputStream = undefined ; resolve ( ) ; } ) ; } ) ; } }