-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.js
191 lines (164 loc) · 4.91 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict'
/**
* Node modules.
*/
var fs = require('fs')
, path = require('path')
/**
* NPM modules.
*/
var dateFormat = require('dateformat')
var winston = require('winston')
/**
* Singleton Logger.
*/
var Logger = function Logger(app) {
if (Logger.prototype.logger) {
return Logger.prototype.logger
}
Logger.prototype.logger = this
Logger.prototype.logger.init(app)
}
/**
* Functions to help formatting messages for winston.
*/
function getLogTimestamp() {
return dateFormat(new Date(), 'dd/mmm/yyyy HH:MM:ss')
}
function formatLogMessage(options) {
return '[' + options.timestamp() + ']'
+ ' ' + options.level.toUpperCase()
+ ' ' + options.message
}
/**
* Initialize logging: make sure logs can be written or only setup the console.
*/
Logger.prototype.init = function init(app) {
// configure set of levels
winston.setLevels(
{ error: 0
, warn: 1
, info: 2
, debug: 4
})
// set level according to configuration
var logLevel = process.env.npm_config_log_level ||
process.env.LOG_LEVEL ||
process.env.npm_package_config_log_level
if (!winston.levels.hasOwnProperty(logLevel.toLowerCase())) {
var logLevels = Object.keys(winston.levels)
throw new Error('"' + logLevel + '" is not a valid log level, pick ' +
'from: "' + logLevels.reverse().join(', ') + '"')
}
logLevel = logLevel.toLowerCase()
// configure loggers
var logContainers =
{ error: {}
, server: {}
}
// make sure lgoDir points to a directory - if set at all
var logDir = ''
if (typeof process.env.npm_config_log_dir != typeof void 0) {
logDir = process.env.npm_config_log_dir
} else if (typeof process.env.LOG_DIR != typeof void 0) {
logDir = process.env.LOG_DIR
} else if (typeof process.env.npm_package_config_log_dir != typeof void 0) {
logDir = process.env.npm_package_config_log_dir
}
if (logDir) {
// expand tilde to $HOME
if (logDir.split(path.sep)[0] == '~') {
logDir = path.join(process.env.HOME,
logDir.split(path.sep).slice(1).join(path.sep))
}
fs.existsSync(logDir) || fs.mkdirSync(logDir)
if (!path.isAbsolute(logDir)) {
logDir = path.resolve(logDir)
}
var logDirStats = fs.lstatSync(logDir)
if (!logDirStats.isDirectory()) {
throw new Error('"' + logDir + '" is not a directory.')
}
// make sure files can be either created or rotated
fs.accessSync(logDir, fs.R_OK | fs.W_OK | fs.X_OK)
// make sure there are log files to write to, by checking permissions or
// creating them
var errorLogFile = path.join(logDir, 'error.log')
var globalLogFile = path.join(logDir, 'server.log')
if (!fs.existsSync(errorLogFile) ||
!fs.existsSync(globalLogFile)) {
// create log files
if (!fs.existsSync(errorLogFile)) {
fs.closeSync(fs.openSync(errorLogFile, 'w'))
}
if (!fs.existsSync(globalLogFile)) {
fs.closeSync(fs.openSync(globalLogFile, 'w'))
}
} else {
// if both files already exist, make sure they are writable files
var errorLogFileStats = fs.lstatSync(errorLogFile)
if (!errorLogFileStats.isFile()) {
throw new Error('"' + errorLogFile + '" is not a file.')
}
var globalLogFileStats = fs.lstatSync(globalLogFile)
if (!globalLogFileStats.isFile()) {
throw new Error('"' + globalLogFile + '" is not a file.')
}
fs.accessSync(errorLogFile, fs.W_OK)
fs.accessSync(globalLogFile, fs.W_OK)
}
// add file loggers
logContainers.error['file'] =
{ filename: errorLogFile
, formatter: formatLogMessage
, handleExceptions: true
, humanReadableUnhandledException: true
, json: false
, level: 'error'
, maxFiles: 2
, maxsize: 1024 * 1024 // 1 MB
, tailable: true
, timestamp: getLogTimestamp
}
logContainers.server['file'] =
{ filename: globalLogFile
, formatter: formatLogMessage
, json: false
, level: logLevel
, maxFiles: 2
, maxsize: 1024 * 1024 // 1 MB
, tailable: true
, timestamp: getLogTimestamp
}
}
// add console loggers
logContainers.server['console'] =
{ json: false
, level: logLevel
, timestamp: getLogTimestamp
, formatter: formatLogMessage
}
logContainers.error['console'] =
{ json: false
, level: 'error'
, timestamp: getLogTimestamp
, formatter: formatLogMessage
}
// configure logger containers
winston.loggers.add('server', logContainers.server)
winston.loggers.add('error', logContainers.error)
// add log functions for levels
for (var level in winston.levels) {
function log(level) {
return function log(message) {
var logger = 'server'
if (level == 'error') {
logger = 'error'
}
winston.loggers.get(logger).log(level, message)
}
}
this[level] = log(level)
}
}
module.exports = new Logger()