封装写入文件
在这里插入代码片
const historyLog = (allTables: any,root='/historyTables/',type='ts') => {
const logPath = __dirname + root
let logFile = null
const fname =`${logPath}${TODAY('YYYYMMDD')}.${type}`
logFile = fs.createWriteStream(fname, {
flags: 'w+',
encoding: 'utf8',
})
const whriteData = JSON.stringify(allTables,null,"\t")
logFile.write(whriteData)
}
function TODAY(flag?:string) {
const currentTime = new Date()
const year = currentTime.getFullYear();
const month = ("0" + (currentTime.getMonth() + 1)).slice(-2);
const day = ("0" + currentTime.getDate()).slice(-2);
const hour = currentTime.getHours()
const minute = currentTime.getMinutes()
const seconds = currentTime.getSeconds()
const milliseconds = currentTime.getMilliseconds()
let time = ''
switch (flag) {
case 'YYYYMMDD':
time = `${year}${month}${day}`
break;
default:
time = `${year}${month}${day}_${formatNum(hour)}_${formatNum(minute)}_${formatNum(seconds)}_${formatNum(milliseconds)}`
break;
}
return time
}