-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
132 lines (108 loc) · 3.69 KB
/
logger.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Created by wellington on 27/07/2017.
*/
//let access = require('./logger').createLogger(`${process.env.PWD}/logs/access.log`)
let Log = require('log')
let log = new Log('info')
let accesslog = require('access-log')
let fs = require('fs')
let debug = process.env.debug || 0
module.exports =
{
access:function (req, res)
{
let access_token = ''
if(req.oauth)
access_token = req.oauth.bearerToken.accessToken
let log_format = `USERID=":userID"; IP=":ip"; XIP=":Xip"; HOST=":host"; METHOD=":method"; PROTO=":protocol"; URL=":url"; USERAGENT=":userAgent"; PERIOD[FROM=":startDate :startTime" TO=":endDate :endTime"]; CLF=":clfDate"; DELTA=":delta"; HTTP_VERSION=":httpVersion"; REFERER=":referer"; URL_DECODED=":urlDecoded"; LENGTH=":contentLength"; ACCESS_TOKEN="${access_token}"`
accesslog(req, res, log_format, function (data)
{
if(debug > 0 || debug == 'access')
{
console.log('')
console.log('################ Access Log ################')
console.log(data)
console.log('')
}
fs.open(`${LOG_PATH}/access.log`, 'a', 666, function( e, id )
{
fs.write( id, `${data}\n`, null, 'utf8', function()
{
fs.close(id, function ()
{
//console.log('file closed')
})
})
})
})
},
log_zoho_request:function (config)
{
let method = config.method || 'GET'
let url = config.url
let form = config.form || ''
let opp_access_token = config.opp_access_token || ''
let data = `[${new Date()}] METHOD="${method}"; URL="${url}"; DATA="${form}"; ACCESS_TOKEN="${opp_access_token}";`
fs.open(`${LOG_PATH}/zoho.log`, 'a', 666, function( e, id )
{
fs.write( id, `${data}\n`, null, 'utf8', function()
{
if(debug > 0 || debug == 'zoho')
{
console.log('')
console.log('################ Request Log ################')
console.log(data)
console.log('')
}
fs.close(id, function()
{
//console.log('file closed')
})
})
})
},
log_query:function (sql)
{
let data = `[${new Date()}] ${sql}`
if(debug > 0 || debug == 'sql')
{
console.log('')
console.log('################ SQL Log ################')
console.log(data)
console.log('')
}
fs.open(`${LOG_PATH}/query.log`, 'a', 666, function( e, id )
{
fs.write( id, `${data}\n`, null, 'utf8', function()
{
fs.close(id, function()
{
//console.log('file closed')
})
})
})
},
error:function (error)
{
let data = `[${new Date()}] ${error}`
if(debug > 0 || debug == 'error')
{
console.log('')
console.log('################ Error Log ################')
console.log(data)
console.log('')
}
if ( !fs.existsSync(`${LOG_PATH}`) )
fs.mkdirSync(`${LOG_PATH}`)
fs.open(`${LOG_PATH}/error.log`, 'a', 666, function ( e, id )
{
fs.write( id, `${data}\n`, null, 'utf8', function ()
{
fs.close(id, function()
{
//console.log('file closed')
})
})
})
}
}