UNPKG

3.7 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 xtend = require('xtend');
24var dotty = require('dotty');
25
26var defaultLevels = require('./default-levels.js');
27var makeLogMethod = require('./log-method');
28var errors = require('./errors');
29
30module.exports = ChildLogger;
31
32function ChildLogger(config) {
33 this.mainLogger = config.mainLogger;
34 this.path = config.path;
35 if (config.extendMeta && !(config.meta || config.metaFilter)) {
36 throw errors.MetaRequired;
37 }
38 this.extendMeta = config.extendMeta;
39 this.meta = config.meta || {};
40 this.strict = config.strict;
41 this.metaFilter = config.metaFilter || [];
42
43 this.metaFilter.forEach(function validateFilter(filter) {
44 if (!filter || !filter.object || typeof filter.object !== 'object') {
45 throw errors.FilterObjectRequired();
46 }
47 if (!filter.mappings || typeof filter.mappings !== 'object') {
48 throw errors.FilterMappingsRequired();
49 }
50 Object.keys(filter.mappings).forEach(function validateMappings(srcName) {
51 var dstName = filter.mappings[srcName];
52 if (typeof dstName !== 'string') {
53 throw errors.FilterBadDst();
54 }
55 });
56 });
57
58 var levels = config.levels || defaultLevels;
59 Object.keys(levels).forEach(function (levelName) {
60 if (!this.mainLogger.levels.hasOwnProperty(levelName)) {
61 if (this.strict) {
62 throw errors.LevelRequired({level: levelName});
63 } else {
64 this[levelName] = noop;
65 this.mainLogger.warn('Child Logger Disabled level',
66 {level: levelName});
67 }
68 } else {
69 this[levelName] = makeLogMethod(levelName);
70 }
71 }, this);
72}
73
74ChildLogger.prototype.writeEntry = function writeEntry(entry, callback) {
75 if (this.extendMeta) {
76 var filteredMeta = {};
77 this.metaFilter.forEach(function readFilter(filter) {
78 var obj = filter.object;
79 Object.keys(filter.mappings).forEach(function readMapping(srcName) {
80 var dstName = filter.mappings[srcName];
81 dotty.put(filteredMeta, dstName, dotty.get(obj, srcName));
82 });
83 }, this);
84 // entry meta should always win
85 entry.meta = xtend(this.meta, filteredMeta, entry.meta);
86 }
87 this.mainLogger.writeEntry(entry, callback);
88};
89
90ChildLogger.prototype.createChild = function createChild(subPath, levels, options) {
91 return this.mainLogger.createChild(this.path + '.' + subPath, levels, options);
92};
93
94function noop() {}