Conditions | 3 |
Paths | 4 |
Total Lines | 126 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
17 | function Slf4j (name, context) { |
||
18 | if (!context) { |
||
19 | context = DefaultContext |
||
20 | } |
||
21 | // backward compatibility for 0.1.x/0.2.x |
||
22 | if (!(context instanceof Context)) { |
||
23 | context = new Slf4j.Context(context, arguments[2]) |
||
24 | } |
||
25 | var mdc = {} |
||
26 | var self = this |
||
27 | |||
28 | function render (pattern, substitutions) { |
||
29 | var chunks = pattern.split('{}') |
||
30 | return substitutions.reduce(function (carrier, entry) { |
||
31 | if (chunks.length > 0) { |
||
32 | return carrier + Renderer.any(entry, false) + chunks.shift() |
||
33 | } else { |
||
34 | return carrier + '\r\n' + Renderer.any(entry, true) |
||
35 | } |
||
36 | }, chunks.shift()) |
||
37 | } |
||
38 | |||
39 | function log (level, pattern) { |
||
40 | level = Level.find(level) |
||
41 | if (level.weight < context.getLevel(name).weight) { |
||
42 | return |
||
43 | } |
||
44 | var prefix = '[' + level.id.toUpperCase() + ']' |
||
45 | if (name) { |
||
46 | prefix += ' ' + name |
||
47 | } |
||
48 | var mdcPrefix = Object.keys(mdc).map(function (key) { |
||
49 | return key + '=' + Renderer.any(mdc[key]) |
||
50 | }).join(', ') |
||
51 | if (mdcPrefix) { |
||
52 | prefix += ' [' + mdcPrefix + ']' |
||
53 | } |
||
54 | var subs = [].slice.call(arguments, 2) |
||
55 | context.getWriter(name).write(render(prefix + ': ' + pattern, subs)) |
||
56 | } |
||
57 | |||
58 | // noinspection JSUnusedGlobalSymbols |
||
59 | this.log = log |
||
60 | |||
61 | Level.explicit.forEach(function (level) { |
||
62 | self[level.id.toLowerCase()] = function () { |
||
63 | log.apply(null, [level].concat([].slice.call(arguments))) |
||
64 | } |
||
65 | }) |
||
66 | |||
67 | this.getName = function () { return name } |
||
68 | |||
69 | this.setWriter = function (writer) { |
||
70 | context.setWriter(name, writer) |
||
71 | return this |
||
72 | } |
||
73 | |||
74 | this.getWriter = function () { return context.getWriter(name) } |
||
75 | |||
76 | this.setLevel = function (level) { |
||
77 | context.setLevel(name, level) |
||
78 | return this |
||
79 | } |
||
80 | |||
81 | this.getLevel = function () { return context.getLevel(name) } |
||
82 | |||
83 | /** |
||
84 | * @deprecated |
||
85 | */ |
||
86 | this.setThreshold = this.setLevel |
||
87 | |||
88 | /** |
||
89 | * @deprecated |
||
90 | */ |
||
91 | this.getThreshold = this.getLevel |
||
92 | |||
93 | this.getContext = function () { return context } |
||
94 | |||
95 | this.attach = function (name, value) { mdc[name] = value } |
||
96 | |||
97 | this.detach = function (name) { delete mdc[name] } |
||
98 | |||
99 | this.attachAll = function (values) { mdc = values } |
||
100 | |||
101 | this.detachAll = function () { |
||
102 | var b = mdc |
||
103 | mdc = {} |
||
104 | return b |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @function Slf4j#trace |
||
109 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
110 | * @param {...object} parameters List of pattern parameters |
||
111 | */ |
||
112 | |||
113 | /** |
||
114 | * @function Slf4j#debug |
||
115 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
116 | * @param {...object} parameters List of pattern parameters |
||
117 | */ |
||
118 | |||
119 | /** |
||
120 | * @function Slf4j#notice |
||
121 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
122 | * @param {...object} parameters List of pattern parameters |
||
123 | */ |
||
124 | |||
125 | /** |
||
126 | * @function Slf4j#info |
||
127 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
128 | * @param {...object} parameters List of pattern parameters |
||
129 | */ |
||
130 | |||
131 | /** |
||
132 | * @function Slf4j#warn |
||
133 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
134 | * @param {...object} parameters List of pattern parameters |
||
135 | */ |
||
136 | |||
137 | /** |
||
138 | * @function Slf4j#error |
||
139 | * @param {string} pattern Logging pattern with `{}` as a place for substitution |
||
140 | * @param {...object} parameters List of pattern parameters |
||
141 | */ |
||
142 | } |
||
143 | |||
255 |