implement lib

This commit is contained in:
2024-05-14 12:46:32 +02:00
parent 903a315788
commit 8ca1f9e2f2
7 changed files with 254 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
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)
}
}