Files
log/src/writer/file.ts
T
2024-05-14 12:46:32 +02:00

44 lines
1.2 KiB
TypeScript

import {Writer} from 'writer'
import {type WriterOptions, Level} from 'types'
export class File extends Writer {
public constructor(options: WriterOptions & { path: string }) {
const {path, ...rest} = options
super(rest)
}
protected write(level: Level, ...data: any[]) : void {
// TODO
}
}
export class DailyFile extends Writer {
private _writer: File
private readonly _folder: string
private now: Date = new Date()
public constructor(options: WriterOptions & {folder: string}) {
const {folder, ...rest} = options
super(rest)
this._folder = folder
this._writer = DailyFile.start(this.options, this._folder, this.now)
}
private static start(options: WriterOptions, folder: string, now: Date) : File {
const date = now.toISOString().split('T')[0]
const path = `${folder}/${date}.log`
return new File({...options, path})
}
protected write(level: Level, ...data: any[]): void {
if (new Date().getDay() !== this.now.getDay()) {
this.now = new Date()
this._writer = DailyFile.start(this.options, this._folder, this.now)
}
return this._writer.log(level, data)
}
}