import {Logger} from './Logger' import {LogMessage} from './types' import {Injectable} from '../../di' import {universalPath} from '../support/path' import {appPath, env} from '../../lifecycle/Application' import {Writable} from 'stream' /** * A Logger implementation that writes to a UniversalPath. */ @Injectable() export class FileLogger extends Logger { private resolvedPath?: Writable /** * Get the re-usable write stream to the log file. * @protected */ protected async getWriteStream(): Promise { if ( !this.resolvedPath ) { let basePath = env('EXTOLLO_LOGGING_FILE') if ( basePath && !Array.isArray(basePath) ) { basePath = [basePath] } const resolvedPath = basePath ? universalPath(...basePath) : appPath('..', '..', 'extollo.log') if ( !(await resolvedPath.exists()) ) { await resolvedPath.concat('..').mkdir() await resolvedPath.write('') } this.resolvedPath = await resolvedPath.writeStream() } return this.resolvedPath } public async write(message: LogMessage): Promise { const text = `${message.level} ${this.formatDate(message.date)} (${message.callerName || 'Unknown'}) ${message.output}` const stream = await this.getWriteStream() stream.write(text + '\n') } }