-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (74 loc) · 2.24 KB
/
index.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
'use strict';
/**
* @name byteskode-logger
* @description byteskode logger utility
* @singleton
*/
//set environment to development by default
if (!(process.env || {}).NODE_ENV) {
process.env.NODE_ENV = 'development';
}
//suppress configuration warning
process.env.SUPPRESS_NO_CONFIG_WARNING = true;
//dependencies
var config = require('config');
var path = require('path');
var winston = require('winston');
var _ = require('lodash');
var mongoose = require('mongoose');
var environment = require('execution-environment');
var LogSchema = require(path.join(__dirname, 'lib', 'schema'));
var MongooseTransport = require(path.join(__dirname, 'lib', 'mongoose_transport'));
var transports = [];
//NOTE: all logger configuration are loaded from config
//NOTE: environment variables are loaded from execution-environment
//prepare default configurations
exports.defaults = {
console: {
timestamp: true,
level: 'silly',
color: true
},
mongoose: {
timestamp: true,
level: 'silly',
model: 'Log'
}
};
//prepare logger configuration
var _config = config.has('logger') ? config.get('logger') : {};
exports.config = _.merge({}, exports.defaults, _config);
//configure execution-environment
if (!environment.isLocal) {
environment.registerEnvironments({
isLocal: ['test', 'dev', 'development']
});
}
//development, test environment logger(s)
if (environment.isLocal()) {
transports.push(new(winston.transports.Console)(exports.config.console));
}
//production logger(s)
MongooseTransport.model = exports.config.mongoose.model;
exports.MongooseTransport = MongooseTransport;
// initialize mongoose log model
var modelName = exports.config.mongoose.model;
try {
if (!mongoose.model(modelName)) {
exports.Log = mongoose.model(modelName, LogSchema);
} else {
exports.Log = mongoose.model(modelName);
}
} catch (e) {
exports.Log = mongoose.model(modelName, LogSchema);
}
//mongoose logger transports
if (!environment.isLocal() && exports.config.mongoose) {
transports.push(new(winston.transports.Mongoose)(exports.config.mongoose));
}
//export logger
if (!exports.logger) {
exports.logger = new(winston.Logger)({
transports: transports
});
}