UNPKG

3.63 kBJavaScriptView Raw
1// Copyright (c) 2015 Uber Technologies, Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21'use strict';
22
23var Logger = require('./logger.js');
24var File = require('./backends/file.js');
25var Disk = require('./backends/disk.js');
26var Kafka = require('./backends/kafka.js');
27var Sentry = require('./backends/sentry.js');
28var Console = require('./backends/console.js');
29
30createLogger.defaultBackends = defaultBackends;
31createLogger.File = File;
32createLogger.Disk = Disk;
33createLogger.Kafka = Kafka;
34createLogger.Sentry = Sentry;
35createLogger.Console = Console;
36createLogger.Access = Disk;
37
38module.exports = createLogger;
39
40function createLogger(opts) {
41 var backends = opts && opts.backends;
42 var logger;
43
44 var _isDefaultBackends = backends &&
45 backends._isDefaultBackends;
46
47 if (_isDefaultBackends) {
48 delete backends._isDefaultBackends;
49 }
50
51 logger = Logger(opts);
52
53 if (_isDefaultBackends) {
54 Object.keys(backends).forEach(function hookEvents(key) {
55 var backend = backends[key];
56
57 if (!backend || typeof backend.on !== 'function') {
58 return;
59 }
60
61 backend.on('info', logger.info.bind(logger));
62 backend.on('warn', logger.warn.bind(logger));
63 });
64 }
65
66 return logger;
67}
68
69function defaultBackends(config, clients) {
70 config = config || {};
71 clients = clients || {};
72
73 return {
74 _isDefaultBackends: true,
75 file: config.logFile ? File({
76 fileName: config.logFile
77 }) : null,
78 disk: config.logFolder ? Disk({
79 folder: config.logFolder,
80 json: config.json || false
81 }) : null,
82 kafka: config.kafka ? Kafka({
83 leafHost: config.kafka.leafHost,
84 leafPort: config.kafka.leafPort,
85 batching: config.kafka.batching,
86 proxyHost: config.kafka.proxyHost,
87 proxyPort: config.kafka.proxyPort,
88 blacklistMigrator: config.kafka.blacklistMigrator,
89 blacklistMigratorUrl: config.kafka.blacklistMigratorUrl,
90 isDisabled: clients.isKafkaDisabled,
91 statsd: config.kafka.statsd,
92 kafkaClient: clients.kafkaClient
93 }) : null,
94 console: config.console ? Console({
95 raw: config.raw || false
96 }) : null,
97 sentry: config.sentry ? Sentry({
98 dsn: config.sentry.id,
99 statsd: clients.statsd
100 }) : null,
101 access: config.access ? Disk({
102 folder: config.access.logFolder,
103 json: config.json || false
104 }) : null
105 };
106}