1 | const _ = require('lodash');
|
2 | const bunyan = require('bunyan');
|
3 | const PrettyStream = require('bunyan-prettystream');
|
4 | const prettyStdOut = new PrettyStream({mode: 'dev'});
|
5 | prettyStdOut.pipe(process.stdout);
|
6 |
|
7 | const utils = require('./utils');
|
8 | const MAX_PORT_NUMBER = 65535;
|
9 |
|
10 | const createLogger = () => {
|
11 | return bunyan.createLogger({
|
12 | name: currentConfig.FRAMEWORK_NAME,
|
13 | streams: [
|
14 | {
|
15 | type: 'raw',
|
16 | level: currentConfig.logLevel,
|
17 | stream: prettyStdOut
|
18 | }
|
19 | ]
|
20 | });
|
21 | };
|
22 |
|
23 | const setLogLevel = (level) => {
|
24 | currentConfig.logLevel = level;
|
25 | currentConfig.log = createLogger();
|
26 | };
|
27 |
|
28 | const configureRedis = (host, port) => {
|
29 | if (!utils.isNonZeroString(host)) {
|
30 | throw new Error('redis host must be string with length');
|
31 | }
|
32 |
|
33 | if (!_.isInteger(port) || port < 1 || port > MAX_PORT_NUMBER) {
|
34 | throw new Error('redis port must be integer from 1-65535');
|
35 | }
|
36 |
|
37 | currentConfig.redis.host = host;
|
38 | currentConfig.redis.port = port;
|
39 | };
|
40 |
|
41 | const setPort = (port) => {
|
42 | if (!_.isInteger(port) || port < 1 || port > MAX_PORT_NUMBER) {
|
43 | throw new Error('port must be integer from 1-65535');
|
44 | }
|
45 |
|
46 | currentConfig.port = port;
|
47 | };
|
48 |
|
49 | const DEFAULT_CONFIG = {
|
50 | FRAMEWORK_NAME: 'chillastic',
|
51 | logLevel: 'info',
|
52 | elasticsearch: {
|
53 | logLevel: 'warn'
|
54 | },
|
55 | redis: {
|
56 | host: null,
|
57 | port: null
|
58 | },
|
59 | port: 8080
|
60 | };
|
61 |
|
62 | const currentConfig = {};
|
63 | _.defaultsDeep(currentConfig, DEFAULT_CONFIG);
|
64 |
|
65 | const log = createLogger();
|
66 |
|
67 | currentConfig.setLogLevel = setLogLevel;
|
68 | currentConfig.configureRedis = configureRedis;
|
69 | currentConfig.setPort = setPort;
|
70 | currentConfig.log = log;
|
71 | currentConfig.jsonIndent = 2;
|
72 | currentConfig.numDigits = 2;
|
73 |
|
74 | module.exports = currentConfig; |
\ | No newline at end of file |