UNPKG

1.73 kBJavaScriptView Raw
1const _ = require('lodash');
2const bunyan = require('bunyan');
3const PrettyStream = require('bunyan-prettystream');
4const prettyStdOut = new PrettyStream({mode: 'dev'});
5prettyStdOut.pipe(process.stdout);
6
7const utils = require('./utils');
8const MAX_PORT_NUMBER = 65535;
9
10const 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
23const setLogLevel = (level) => {
24 currentConfig.logLevel = level;
25 currentConfig.log = createLogger();
26};
27
28const 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
41const 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
49const 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
62const currentConfig = {};
63_.defaultsDeep(currentConfig, DEFAULT_CONFIG);
64
65const log = createLogger();
66
67currentConfig.setLogLevel = setLogLevel;
68currentConfig.configureRedis = configureRedis;
69currentConfig.setPort = setPort;
70currentConfig.log = log;
71currentConfig.jsonIndent = 2;
72currentConfig.numDigits = 2;
73
74module.exports = currentConfig;
\No newline at end of file