2020-09-15 18:35:00 +08:00
|
|
|
/**
|
2020-09-18 18:14:47 +08:00
|
|
|
* 简单的日志封装
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2020/09/18 16:07:26
|
2020-09-15 18:35:00 +08:00
|
|
|
*/
|
2020-09-18 18:14:47 +08:00
|
|
|
|
|
|
|
import fs from 'iofs'
|
|
|
|
import path from 'path'
|
2020-09-15 18:35:00 +08:00
|
|
|
|
2020-09-16 14:08:06 +08:00
|
|
|
export default class Log {
|
2020-09-18 18:25:27 +08:00
|
|
|
constructor(file = 'run_time.log', dir = './') {
|
2020-09-15 18:35:00 +08:00
|
|
|
if (!dir) {
|
|
|
|
throw new Error(`agument dir must be a string, but ${typeof dir} given.`)
|
|
|
|
}
|
|
|
|
|
2020-09-18 18:14:47 +08:00
|
|
|
if (!fs.exists(dir)) {
|
|
|
|
fs.mkdir(dir)
|
2020-09-15 18:35:00 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 18:14:47 +08:00
|
|
|
this.file = path.resolve(dir, file)
|
2020-09-15 18:35:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
error(str) {
|
|
|
|
this.save(str, 'error')
|
|
|
|
}
|
|
|
|
|
|
|
|
info(str) {
|
|
|
|
this.save(str, 'info')
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(str) {
|
|
|
|
this.save(str, 'warning')
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(str) {
|
|
|
|
this.save(str, 'debug')
|
|
|
|
}
|
|
|
|
|
|
|
|
//写入日志文件
|
|
|
|
save(str, type) {
|
|
|
|
type = type || 'debug'
|
2020-09-18 18:14:47 +08:00
|
|
|
fs.echo(
|
2020-09-15 18:35:00 +08:00
|
|
|
`[${type}] ${new Date().format('Y-m-d_H:i:s')} ${str} \n`,
|
2020-09-18 18:14:47 +08:00
|
|
|
this.file,
|
|
|
|
true
|
2020-09-15 18:35:00 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|