You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.4 KiB
45 lines
1.4 KiB
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<Writable> {
|
|
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<void> {
|
|
const text = `${message.level} ${this.formatDate(message.date)} (${message.callerName || 'Unknown'}) ${message.output}`
|
|
const stream = await this.getWriteStream()
|
|
stream.write(text + '\n')
|
|
}
|
|
}
|