/* * @Author: fuyu * @Date: 2021-03-23 16:25:59 * @LastEditors: fuyu * @LastEditTime: 2021-03-25 17:28:20 * @FilePath: /202103/code/sql_merge/src/service/log.js */ const fs = require('fs') const path = require('path'); const rootPath = path.join(__dirname, '..', '..', 'logs') import {getNowDay, getNowTime} from './time' export async function logSum(institution_id, count) { const dirPath = path.join(rootPath, 'sum') checkPath(dirPath) const filePath = path.join(dirPath, getNowDay() + '.log') const time = getNowTime() const obj = { time: time, institution_id, count, } fs.appendFileSync(filePath, JSON.stringify(obj) + '\r\n') fs.writeFileSync(path.join(dirPath, institution_id + '-' + getNowDay() + '.log'), count + '') } export async function logSingle(name) { checkPath(rootPath) const filePath = path.join(rootPath, name + '.log') let count = 0 if(fs.existsSync(filePath)) { let str = fs.readFileSync(filePath) if(str) { try { let obj = JSON.parse(fs.readFileSync(filePath)) || {} count = obj['count'] || 0 } catch (error) { console.log('err: JSON.parse logSingle', error) } } } count = parseInt(count) + 1 const time = getNowTime() fs.writeFileSync(filePath, JSON.stringify({time, count})) } const checkPath = filePath => { if(fs.existsSync(filePath)) { return true } return fs.mkdirSync(filePath, { recursive: true }) }