-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.js
174 lines (141 loc) · 4.91 KB
/
config.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
// Copyright 2017 Telefónica Digital España S.L.
//
// This file is part of UrboCore Processing.
//
// UrboCore Processing is free software: you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// UrboCore Processing is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
// General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with UrboCore Processing. If not, see http://www.gnu.org/licenses/.
//
// For those usages not covered by this license please contact with
// iot_support at tid dot es
'use strict';
var fs = require('fs');
var yaml = require('js-yaml');
var _ = require('underscore');
var walk = require('walk');
var appDir = require('app-root-path').path;
var ospath = require('path');
// Logs params
var LOG_LEVELS = ['ALL', 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'OFF'];
var LOG_OUTPUTS = ['console', 'file', 'dailyRotatingFile', 'sizeRotatingFile'];
// Log folders structure
var LOG_DEFAULT_DIR = './logs';
var LOG_DEFAULT_FILENAME = 'the_log';
var LOG_DEFAULT_MAX_SIZE = 20;
var LOG_DEFAULT_OLD_FILES = 5;
function Config() {
this._data = yaml.safeLoad(fs.readFileSync('config.yml', 'utf8'));
// Set default params
var pgsql = this._data.pgsql;
if (pgsql) {
// Set default params for Carto
if (!pgsql.hasOwnProperty('port') || !pgsql.port) {
pgsql.port = 5432;
}
}
this.getData = function () {
return this._data;
};
this.createFolderSync = function(folder) {
fs.existsSync(folder) || fs.mkdirSync(folder);
};
this.getLogOpt = function() {
// Config vars
var _logging = this._data.logging;
var _file = _logging && _logging.file ? _logging.file : null;
var _access = _logging && _logging.access ? _logging.access : null;
// Filename vars
var fileDir = _file && _file.dir ? _file.dir : LOG_DEFAULT_DIR;
var suffixFilename = _file && _file.name ? _file.name : LOG_DEFAULT_FILENAME;
suffixFilename = `${ fileDir }/${ suffixFilename }`;
var errorFilename = `${ suffixFilename }-errors.log`;
var filename = `${ suffixFilename }.log`;
// Appenders definition
var fileErrorAppender = {
type: 'logLevelFilter',
level: 'ERROR',
appender: {
type: 'file',
filename: errorFilename
}
};
var logAppenderConsole = [
{
type: 'console'
}
];
var logAppenderFile = [
{
type: 'file',
filename: filename
}
];
var logAppenderDailyRotatingFile = [
{
type: 'dateFile',
filename: filename,
pattern: '.yyyy-MM-dd'
}
];
var logAppenderSizeRotatingFile = [
{
type: 'file',
filename: filename,
maxLogSize: Math.pow((_file && _file.maxSize ? _file.maxSize : LOG_DEFAULT_MAX_SIZE) * 1024, 2),
numBackups: _file && _file.oldFiles ? _file.oldFiles : LOG_DEFAULT_OLD_FILES
}
];
if (_file && _file.separateError) {
logAppenderFile.push(fileErrorAppender);
logAppenderDailyRotatingFile.push(fileErrorAppender);
logAppenderSizeRotatingFile.push(fileErrorAppender);
}
// log4js parameters definition
var logParams = {
output: LOG_OUTPUTS[0],
level: LOG_LEVELS[3],
access: { level: LOG_LEVELS[3] },
logappenders: logAppenderConsole
};
// Reading from config file
if (_logging) {
if (_logging.level && _.contains(LOG_LEVELS, _logging.level)) {
logParams.level = _logging.level;
}
if (_access && _access.level && _.contains(LOG_LEVELS, _access.level)) {
logParams.access.level = _access.level;
}
if (_access && _access.format) {
logParams.access.format = _access.format;
}
if (_access && _access.nolog) {
logParams.access.nolog = _access.nolog;
}
if (_logging.output && _logging.output.endsWith('ile')) {
this.createFolderSync(fileDir);
logParams.output = _logging.output;
if (_logging.output === 'file') {
logParams.logappenders = logAppenderFile;
} else if (_logging.output === 'dailyRotatingFile') {
logParams.logappenders = logAppenderDailyRotatingFile;
} else if (_logging.output === 'sizeRotatingFile') {
logParams.logappenders = logAppenderSizeRotatingFile;
}
// Creating a message for the console
errorFilename = _file && _file.separateError ? ' & ' + errorFilename : '';
logParams.consoleMessage = `Logging into files: ${ filename }${ errorFilename }`;
}
}
return logParams;
};
}
module.exports = new Config();