-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (74 loc) · 1.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs')
const os = require('os')
const path = require('path')
require('./lib/lib_shb_date')
let rootPath = path.join(process.cwd(),'logs')
let subPath = ''
let fileName = '-YYYY-MM-DD.log'
function log2file(text,file){
var date = (new Date()).format() ;
var log = `${date}-->${text}${os.EOL}` ;
mkDir(file);
if(!fs.existsSync(file)){
fs.writeFileSync(file,log,'utf-8');
return ;
}
fs.appendFile(file,log,'utf-8',(err)=>{})
}
function mkDir(file){
var list = file.split(path.sep);
for(var i = 0 ; i < list.length ; i ++){
var curPath = path.join.apply(path,list.slice(0,i+1));
if(!fs.existsSync(curPath)){
if(i != list.length-1){
fs.mkdirSync(curPath);
}else{
fs.writeFileSync(curPath,'','utf-8');
}
}
}
}
function config(options){
if(options.path !== undefined){
rootPath = options.path ;
}
if(options.subPath !== undefined){
subPath = options.subPath ;
}
if(options.fileName !== undefined){
fileName = options.fileName ;
}
console.log(`logs file in rootPath: ${rootPath}`)
}
function getFileName(type){
var date = new Date();
var list = [rootPath];
if(subPath){
let tempSubPath = date.format(subPath);
list.push(tempSubPath);
}
if(fileName){
let tempFileName = date.format(fileName);
list.push(type + tempFileName);
}
return path.join.apply(path,list);
}
function access(info){
info = JSON.stringify(info);
log2file(info,getFileName('access'));
}
function error(info){
info = JSON.stringify(info);
log2file(info,getFileName('error'));
}
function info(info){
console.log(info)
info = JSON.stringify(info);
log2file(info,getFileName('info'));
}
module.exports = {
access,
error,
info,
config
}