needle 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/doc/faq/faq.yml +396 -14
- data/doc/manual-html/chapter-1.html +33 -4
- data/doc/manual-html/chapter-2.html +44 -6
- data/doc/manual-html/chapter-3.html +34 -5
- data/doc/manual-html/chapter-4.html +33 -4
- data/doc/manual-html/chapter-5.html +208 -4
- data/doc/manual-html/chapter-6.html +226 -4
- data/doc/manual-html/chapter-7.html +165 -4
- data/doc/manual-html/chapter-8.html +138 -6
- data/doc/manual-html/index.html +36 -9
- data/doc/manual/manual.rb +1 -1
- data/doc/manual/manual.yml +17 -4
- data/doc/manual/page.erb +2 -1
- data/doc/manual/parts/02_namespaces.txt +11 -2
- data/doc/manual/parts/03_overview.txt +1 -1
- data/doc/manual/parts/interceptors_architecture.txt +5 -0
- data/doc/manual/parts/interceptors_attaching.txt +64 -0
- data/doc/manual/parts/interceptors_custom.txt +25 -0
- data/doc/manual/parts/interceptors_ordering.txt +13 -0
- data/doc/manual/parts/interceptors_overview.txt +5 -0
- data/doc/manual/parts/libraries_creating.txt +30 -0
- data/doc/manual/parts/libraries_overview.txt +3 -0
- data/doc/manual/parts/libraries_using.txt +31 -0
- data/doc/manual/parts/logging_configuration.txt +30 -0
- data/doc/manual/parts/logging_logfactory.txt +31 -0
- data/doc/manual/parts/logging_overview.txt +5 -0
- data/doc/manual/parts/models_models.txt +35 -0
- data/doc/manual/parts/models_overview.txt +3 -0
- data/doc/manual/parts/models_pipelines.txt +63 -0
- data/lib/needle/container.rb +52 -7
- data/lib/needle/lifecycle/initialize.rb +1 -1
- data/lib/needle/log-factory.rb +46 -8
- data/lib/needle/thread.rb +6 -0
- data/lib/needle/version.rb +2 -2
- data/test/pipeline/tc_collection.rb +1 -1
- data/test/pipeline/tc_element.rb +2 -2
- data/test/services.rb +21 -0
- data/test/tc_container.rb +18 -0
- data/test/tc_logger.rb +35 -1
- metadata +17 -2
@@ -14,8 +14,8 @@
|
|
14
14
|
</div>
|
15
15
|
</td><td valign='middle' align='right'>
|
16
16
|
<div class="info">
|
17
|
-
Needle Version: <strong>0.
|
18
|
-
Manual Last Updated: <strong>2004-
|
17
|
+
Needle Version: <strong>1.0.0</strong><br />
|
18
|
+
Manual Last Updated: <strong>2004-11-04 14:24 GMT</strong>
|
19
19
|
</div>
|
20
20
|
</td></tr>
|
21
21
|
</table>
|
@@ -105,6 +105,16 @@
|
|
105
105
|
|
106
106
|
<ol type="1">
|
107
107
|
|
108
|
+
<li><a href="chapter-5.html#s1">Overview</a></li>
|
109
|
+
|
110
|
+
<li><a href="chapter-5.html#s2">Architecture</a></li>
|
111
|
+
|
112
|
+
<li><a href="chapter-5.html#s3">Attaching</a></li>
|
113
|
+
|
114
|
+
<li><a href="chapter-5.html#s4">Ordering</a></li>
|
115
|
+
|
116
|
+
<li><a href="chapter-5.html#s5">Custom</a></li>
|
117
|
+
|
108
118
|
</ol>
|
109
119
|
</li>
|
110
120
|
|
@@ -115,6 +125,12 @@
|
|
115
125
|
</strong> <big>←</big>
|
116
126
|
<ol type="1">
|
117
127
|
|
128
|
+
<li><a href="chapter-6.html#s1">Overview</a></li>
|
129
|
+
|
130
|
+
<li><a href="chapter-6.html#s2">Pipelines</a></li>
|
131
|
+
|
132
|
+
<li><a href="chapter-6.html#s3">Models</a></li>
|
133
|
+
|
118
134
|
</ol>
|
119
135
|
</li>
|
120
136
|
|
@@ -125,25 +141,38 @@
|
|
125
141
|
|
126
142
|
<ol type="1">
|
127
143
|
|
144
|
+
<li><a href="chapter-7.html#s1">Overview</a></li>
|
145
|
+
|
146
|
+
<li><a href="chapter-7.html#s2">LogFactory</a></li>
|
147
|
+
|
148
|
+
<li><a href="chapter-7.html#s3">Configuration</a></li>
|
149
|
+
|
128
150
|
</ol>
|
129
151
|
</li>
|
130
152
|
|
131
153
|
<li>
|
132
154
|
<a href="chapter-8.html">
|
133
|
-
|
155
|
+
Service Libraries
|
134
156
|
</a>
|
135
157
|
|
136
158
|
<ol type="1">
|
137
159
|
|
160
|
+
<li><a href="chapter-8.html#s1">Overview</a></li>
|
161
|
+
|
162
|
+
<li><a href="chapter-8.html#s2">Creating Libraries</a></li>
|
163
|
+
|
164
|
+
<li><a href="chapter-8.html#s3">Using Libraries</a></li>
|
165
|
+
|
138
166
|
</ol>
|
139
167
|
</li>
|
140
168
|
|
141
169
|
</ol>
|
142
170
|
|
143
|
-
<h2>
|
171
|
+
<h2>Other Documentation</h2>
|
144
172
|
|
145
173
|
<ul>
|
146
174
|
<li><a href="http://needle.rubyforge.org/api/index.html">Needle API</a></li>
|
175
|
+
<li><a href="http://needle.rubyforge.org/faq.html">Needle FAQ</a></li>
|
147
176
|
</ul>
|
148
177
|
|
149
178
|
<h2>Tutorials</h2>
|
@@ -167,6 +196,199 @@
|
|
167
196
|
|
168
197
|
|
169
198
|
|
199
|
+
<h2>
|
200
|
+
<a name="s1"></a>
|
201
|
+
6.1. Overview
|
202
|
+
</h2>
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
<div class="section">
|
207
|
+
<p>Service models are the mechanism by which a client can specify how the lifecycle of a particular service should be managed. By default, all services are managed as <em>singletons</em>, but it is a simple matter to choose a different behavior when a service is registered.</p>
|
208
|
+
|
209
|
+
<p>Underneath, service models are implemented using an <em>instantiation pipeline</em>.<br />
|
210
|
+
</p>
|
211
|
+
</div>
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
<h2>
|
216
|
+
<a name="s2"></a>
|
217
|
+
6.2. Pipelines
|
218
|
+
</h2>
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
<div class="section">
|
223
|
+
<p>An <em>instantiation pipeline</em> is a sequence of elements, each of which knows how to perform some aspect of the instantiation of a service.</p>
|
224
|
+
|
225
|
+
<p>Every service consists of at least one instantiation element—the block that was given when the service was registered. Other elements may be combined with this block to enforce various aspects of lifecycle management, such as <em>multiplicity</em> (singleton vs. prototype) and <em>laziness</em> (deferred vs. immediate instantiation).</p>
|
226
|
+
|
227
|
+
<h3>Standard Pipeline Elements</h3>
|
228
|
+
|
229
|
+
<p>There are five standard pipeline elements available in Needle (although you may certainly create your own):</p>
|
230
|
+
|
231
|
+
<ul>
|
232
|
+
<li><code>deferred</code>: this will always return a proxy that wraps subsequent pipeline elements, causing the subsequent elements to be executed only when a method is invoked on the proxy (at which point the method is then delegated to the resulting service).</li>
|
233
|
+
<li><code>initialize</code>: this will invoke a method on the resulting service (defaults to <code>initialize_service</code>, though it can be changed). It is used for doing final initialization of services (for services that need it).</li>
|
234
|
+
<li><code>interceptor</code>: this element is used to implement the proxy that wraps the interceptors around the service. It is only attached to the pipeline when an interceptor is attached to a service.</li>
|
235
|
+
<li><code>singleton</code>: this is a multiplicity guard that ensures a service is instantiated only once per process.</li>
|
236
|
+
<li><code>threaded</code>: this is like the <code>singleton</code> element, but it ensures that a service is instantiated no more than once <em>per thread</em>.</li>
|
237
|
+
</ul>
|
238
|
+
|
239
|
+
<h3>Priorities</h3>
|
240
|
+
|
241
|
+
<p>Just like interceptors, pipeline elements have priorities as well. These priorities determine the order in which the elements are executed in the pipeline.</p>
|
242
|
+
|
243
|
+
<p>Each element type has a default priority, although that priority can be overridden when the element is added to the pipeline.</p>
|
244
|
+
|
245
|
+
<h3>Custom Pipeline Elements</h3>
|
246
|
+
|
247
|
+
<p>Creating new pipeline elements simple. Just create a new class that extends <code>Needle::Pipeline::Element</code>. Set the default pipeline priority (using the <code>#set_default_priority</code> class method), and then implement the <code>#call</code> method (accepting at least two parameters: the container and the service point).</p>
|
248
|
+
|
249
|
+
<pre>
|
250
|
+
require 'needle/pipeline/element'
|
251
|
+
|
252
|
+
class MyPipelineElement < Needle::Pipeline::Element
|
253
|
+
set_default_priority 50
|
254
|
+
|
255
|
+
def call( container, point, *args )
|
256
|
+
...
|
257
|
+
result = succ.call( container, point, *args )
|
258
|
+
...
|
259
|
+
return result
|
260
|
+
end
|
261
|
+
end
|
262
|
+
</pre>
|
263
|
+
|
264
|
+
<p>To invoke the next element of the pipeline, just invoke <code>#succ.call(...)</code>.</p>
|
265
|
+
|
266
|
+
<p>If needed, you can also implement <code>#initialize_element</code> (with no arguments), which you may invoke to perform initialization of the element. From there, you can access the options that were given to the element via the <code>#options</code> accessor.</p>
|
267
|
+
|
268
|
+
<p>See the implementations of the existing elements in <code>needle/lifecycle</code> for examples.</p>
|
269
|
+
|
270
|
+
<h3>Added Pipelines Elements to Services</h3>
|
271
|
+
|
272
|
+
<p>You can specify the pipeline elements to use for a service via the <code>:pipeline</code> option. This must refer to an array, each element of which must be either a symbol (in which case it references an element of the <code>:pipeline_elements</code> service), or a class (in which case it must implement the interface required by @Needle::Pipeline::Element).</p>
|
273
|
+
|
274
|
+
<pre>
|
275
|
+
reg.register( :foo, :pipeline => [ :singleton, MyPipelineElement ] ) { ... }
|
276
|
+
</pre>
|
277
|
+
|
278
|
+
<p>The elements will be sorted based on their priorities, with lower priorities sorting closer to the instantiation block, and higher priorities sorting closer to the client.</p>
|
279
|
+
|
280
|
+
<h3>Making Custom Pipeline Elements Available</h3>
|
281
|
+
|
282
|
+
<p>You can make your custom pipeline elements available (so they can be referenced by symbol, instead of class name) by adding them to the <code>:pipeline_elements</code> service:</p>
|
283
|
+
|
284
|
+
<pre>
|
285
|
+
reg.pipeline_elements[ :my_pipeline_element ] = MyPipelineElement
|
286
|
+
reg.register( :foo, :pipeline => [ :singleton, :my_pipeline_element ] ) { ... }
|
287
|
+
</pre>
|
288
|
+
</div>
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
<h2>
|
293
|
+
<a name="s3"></a>
|
294
|
+
6.3. Models
|
295
|
+
</h2>
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
<div class="section">
|
300
|
+
<p>Specifying an entire pipeline for every service point can be tedious. For that reason, there are <em>service models</em>. A service model is a kind of template that names a pre-configured sequence of pipeline elements. Needle comes preconfigured with many service models.</p>
|
301
|
+
|
302
|
+
<h3>Standard Service Models:</h3>
|
303
|
+
|
304
|
+
<table style="border: 1px solid black;">
|
305
|
+
<tr>
|
306
|
+
<th style="text-align:left;">Name</th>
|
307
|
+
<th style="text-align:left;">Pipeline</th>
|
308
|
+
<th style="text-align:left;">Effect</th>
|
309
|
+
</tr>
|
310
|
+
<tr>
|
311
|
+
<td style="vertical-align:top;"><code>:prototype</code></td>
|
312
|
+
<td style="vertical-align:top;">(empty)</td>
|
313
|
+
<td>Immediately instantiate the service, at every request.</td>
|
314
|
+
</tr>
|
315
|
+
<tr>
|
316
|
+
<td style="vertical-align:top;"><code>:prorotype_deferred</code></td>
|
317
|
+
<td style="vertical-align:top;"><code>:deferred</code></td>
|
318
|
+
<td>Return a proxy, that will instantiate the service the first time a method is invoked on the proxy. A new proxy instance will be returned for each request.</td>
|
319
|
+
</tr>
|
320
|
+
<tr>
|
321
|
+
<td style="vertical-align:top;"><code>:prototype_initialize</code></td>
|
322
|
+
<td style="vertical-align:top;"><code>:initialize</code></td>
|
323
|
+
<td>Immediately instantiate the service, and invoke an initialization method, at every request.</td>
|
324
|
+
</tr>
|
325
|
+
<tr>
|
326
|
+
<td style="vertical-align:top;"><code>:prototype_deferred_initialize</code></td>
|
327
|
+
<td style="vertical-align:top;"><code>:deferred</code>, <code>:initialize</code></td>
|
328
|
+
<td>Return a proxy, that will instantiate the service and invoke an initialization method the first time a method is invoked on the proxy. A new proxy instance will be returned for each request.</td>
|
329
|
+
</tr>
|
330
|
+
<tr>
|
331
|
+
<td style="vertical-align:top;"><code>:singleton</code></td>
|
332
|
+
<td style="vertical-align:top;"><code>:singleton</code></td>
|
333
|
+
<td>Immediately instantiate the service the first time it is requested, returning a cached instance subsequently.</td>
|
334
|
+
</tr>
|
335
|
+
<tr>
|
336
|
+
<td style="vertical-align:top;"><code>:singleton_deferred</code></td>
|
337
|
+
<td style="vertical-align:top;"><code>:singleton</code>, <code>:deferred</code></td>
|
338
|
+
<td>Return a proxy that will instantiate the service the first time a method is requested on the proxy. Subsequent requests for this service will return the same proxy instance.</td>
|
339
|
+
</tr>
|
340
|
+
<tr>
|
341
|
+
<td style="vertical-align:top;"><code>:singleton_initialize</code></td>
|
342
|
+
<td style="vertical-align:top;"><code>:singleton</code>, <code>:initialize</code></td>
|
343
|
+
<td>Immediately instantiate a service and invoke an initialization method on it. Subsequent requests for this service will return a cached instance.</td>
|
344
|
+
</tr>
|
345
|
+
<tr>
|
346
|
+
<td style="vertical-align:top;"><code>:singleton_deferred_initialize</code></td>
|
347
|
+
<td style="vertical-align:top;"><code>:singleton</code>, <code>:deferred</code>, <code>:initialize</code></td>
|
348
|
+
<td>Return a proxy that will instantiate the service and invoke an initialization method on it the first time a method is requested on the proxy. Subsequent requests for this service will return the same proxy instance.</td>
|
349
|
+
</tr>
|
350
|
+
<tr>
|
351
|
+
<td style="vertical-align:top;"><code>:threaded</code></td>
|
352
|
+
<td style="vertical-align:top;"><code>:threaded</code></td>
|
353
|
+
<td>Immediately instantiate the service the first time it is requested from the current thread, returning a cached instance for every subsequent request from the same thread.</td>
|
354
|
+
</tr>
|
355
|
+
<tr>
|
356
|
+
<td style="vertical-align:top;"><code>:threaded_deferred</code></td>
|
357
|
+
<td style="vertical-align:top;"><code>:threaded</code>, <code>:deferred</code></td>
|
358
|
+
<td>Return a proxy object that will instantiate the service the first time a method is invoked on the proxy. Subsequent requests for this service from a given thread will return the thread’s cached instance.</td>
|
359
|
+
</tr>
|
360
|
+
<tr>
|
361
|
+
<td style="vertical-align:top;"><code>:threaded_initialize</code></td>
|
362
|
+
<td style="vertical-align:top;"><code>:threaded</code>, <code>:initialize</code></td>
|
363
|
+
<td>Immediately instantiate the service the first time it is requested from the current thread and invoke an initialization method on it. Subsequent requests for this service from the same thread will return the cached instance.</td>
|
364
|
+
</tr>
|
365
|
+
<tr>
|
366
|
+
<td style="vertical-align:top;"><code>:threaded_deferred_initialize</code></td>
|
367
|
+
<td style="vertical-align:top;"><code>:threaded</code>, <code>:deferred</code>, <code>:initialize</code></td>
|
368
|
+
<td>Return a proxy object that will instantiate the service and invoke an initialization method on it the first time a method is invoked on the proxy. Subsequent requests for this service from a given thread will return the thread’s cached instance.</td>
|
369
|
+
</tr>
|
370
|
+
</table>
|
371
|
+
|
372
|
+
<h3>Specifying a Service Model</h3>
|
373
|
+
|
374
|
+
<p>You specify the service model by passing the <code>:model</code> option when you register a service. (You must only specify <em>either</em> the model, <em>or</em> the pipeline, but not both.)</p>
|
375
|
+
|
376
|
+
<pre>
|
377
|
+
reg.register( :foo, :model => :singleton_deferred ) {...}
|
378
|
+
</pre>
|
379
|
+
|
380
|
+
<h3>Defining New Models</h3>
|
381
|
+
|
382
|
+
<p>You can create your own service models by adding the corresponding pipelines to the <code>:service_models</code> service:</p>
|
383
|
+
|
384
|
+
<pre>
|
385
|
+
reg.service_models[ :my_service_model ] = [ :singleton, :my_pipeline_element ]
|
386
|
+
reg.register( :foo, :model => :my_service_model ) {...}
|
387
|
+
</pre>
|
388
|
+
</div>
|
389
|
+
|
390
|
+
|
391
|
+
|
170
392
|
|
171
393
|
</div>
|
172
394
|
|
@@ -14,8 +14,8 @@
|
|
14
14
|
</div>
|
15
15
|
</td><td valign='middle' align='right'>
|
16
16
|
<div class="info">
|
17
|
-
Needle Version: <strong>0.
|
18
|
-
Manual Last Updated: <strong>2004-
|
17
|
+
Needle Version: <strong>1.0.0</strong><br />
|
18
|
+
Manual Last Updated: <strong>2004-11-04 14:24 GMT</strong>
|
19
19
|
</div>
|
20
20
|
</td></tr>
|
21
21
|
</table>
|
@@ -105,6 +105,16 @@
|
|
105
105
|
|
106
106
|
<ol type="1">
|
107
107
|
|
108
|
+
<li><a href="chapter-5.html#s1">Overview</a></li>
|
109
|
+
|
110
|
+
<li><a href="chapter-5.html#s2">Architecture</a></li>
|
111
|
+
|
112
|
+
<li><a href="chapter-5.html#s3">Attaching</a></li>
|
113
|
+
|
114
|
+
<li><a href="chapter-5.html#s4">Ordering</a></li>
|
115
|
+
|
116
|
+
<li><a href="chapter-5.html#s5">Custom</a></li>
|
117
|
+
|
108
118
|
</ol>
|
109
119
|
</li>
|
110
120
|
|
@@ -115,6 +125,12 @@
|
|
115
125
|
|
116
126
|
<ol type="1">
|
117
127
|
|
128
|
+
<li><a href="chapter-6.html#s1">Overview</a></li>
|
129
|
+
|
130
|
+
<li><a href="chapter-6.html#s2">Pipelines</a></li>
|
131
|
+
|
132
|
+
<li><a href="chapter-6.html#s3">Models</a></li>
|
133
|
+
|
118
134
|
</ol>
|
119
135
|
</li>
|
120
136
|
|
@@ -125,25 +141,38 @@
|
|
125
141
|
</strong> <big>←</big>
|
126
142
|
<ol type="1">
|
127
143
|
|
144
|
+
<li><a href="chapter-7.html#s1">Overview</a></li>
|
145
|
+
|
146
|
+
<li><a href="chapter-7.html#s2">LogFactory</a></li>
|
147
|
+
|
148
|
+
<li><a href="chapter-7.html#s3">Configuration</a></li>
|
149
|
+
|
128
150
|
</ol>
|
129
151
|
</li>
|
130
152
|
|
131
153
|
<li>
|
132
154
|
<a href="chapter-8.html">
|
133
|
-
|
155
|
+
Service Libraries
|
134
156
|
</a>
|
135
157
|
|
136
158
|
<ol type="1">
|
137
159
|
|
160
|
+
<li><a href="chapter-8.html#s1">Overview</a></li>
|
161
|
+
|
162
|
+
<li><a href="chapter-8.html#s2">Creating Libraries</a></li>
|
163
|
+
|
164
|
+
<li><a href="chapter-8.html#s3">Using Libraries</a></li>
|
165
|
+
|
138
166
|
</ol>
|
139
167
|
</li>
|
140
168
|
|
141
169
|
</ol>
|
142
170
|
|
143
|
-
<h2>
|
171
|
+
<h2>Other Documentation</h2>
|
144
172
|
|
145
173
|
<ul>
|
146
174
|
<li><a href="http://needle.rubyforge.org/api/index.html">Needle API</a></li>
|
175
|
+
<li><a href="http://needle.rubyforge.org/faq.html">Needle FAQ</a></li>
|
147
176
|
</ul>
|
148
177
|
|
149
178
|
<h2>Tutorials</h2>
|
@@ -167,6 +196,138 @@
|
|
167
196
|
|
168
197
|
|
169
198
|
|
199
|
+
<h2>
|
200
|
+
<a name="s1"></a>
|
201
|
+
7.1. Overview
|
202
|
+
</h2>
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
<div class="section">
|
207
|
+
<p>Logging is a pretty common activity in many applications. Because it is so common, Needle provides a powerful framework for logging messages in a consistent manner.</p>
|
208
|
+
|
209
|
+
<p>Needle’s logging framework is built on top of the standard Ruby <code>Logger</code> library. However, it extends that library to add additional functionality, including customizable message formats and a centralized location for obtaining logger instances.</p>
|
210
|
+
|
211
|
+
<p>This centralized location is the LogFactory.<br />
|
212
|
+
</p>
|
213
|
+
</div>
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
<h2>
|
218
|
+
<a name="s2"></a>
|
219
|
+
7.2. LogFactory
|
220
|
+
</h2>
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
<div class="section">
|
225
|
+
<p>The LogFactory is available as a service, so that any component that needs a logger instance can gain easy access to one. The factory’s service name is <code>:logs</code>.</p>
|
226
|
+
|
227
|
+
<pre>
|
228
|
+
factory = reg.logs
|
229
|
+
</pre>
|
230
|
+
|
231
|
+
<p>You obtain logger instances from the factory by passing a logger name to <code>#get</code>:</p>
|
232
|
+
|
233
|
+
<pre>
|
234
|
+
logger = reg.logs.get "a logger name"
|
235
|
+
</pre>
|
236
|
+
|
237
|
+
<p>Subsequent calls to <code>#get</code> will return the same logger instance for the given logger name.</p>
|
238
|
+
|
239
|
+
<h3>Loggers for Services</h3>
|
240
|
+
|
241
|
+
<p>Typically, you’ll use this to assign a logger instance to a service when it is constructed. In that case, the name of the logger is the fully-qualified name of the service:</p>
|
242
|
+
|
243
|
+
<pre>
|
244
|
+
reg.register( :foo ) do |c,p|
|
245
|
+
Foo.new( c.logs.get( p.fullname ) )
|
246
|
+
end
|
247
|
+
</pre>
|
248
|
+
|
249
|
+
<p>As a convenience, if the value passed to <code>#get</code> responds to either <code>fullname</code> or <code>name</code>, the return value of that message will be used as the name. Thus, you can do the following:</p>
|
250
|
+
|
251
|
+
<pre>
|
252
|
+
reg.register( :foo ) do |c,p|
|
253
|
+
Foo.new( c.logs.get( p ) )
|
254
|
+
end
|
255
|
+
</pre>
|
256
|
+
</div>
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
<h2>
|
261
|
+
<a name="s3"></a>
|
262
|
+
7.3. Configuration
|
263
|
+
</h2>
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
<div class="section">
|
268
|
+
<p>You can configure the LogFactory when you create the registry by passing a hash as the <code>:logs</code> option to <code>Registry.new</code>. The hash may have the following properties:</p>
|
269
|
+
|
270
|
+
<table>
|
271
|
+
<tr>
|
272
|
+
<td style="vertical-align:top;"><code>:device</code></td>
|
273
|
+
<td>Should refer to an IO or pseudo-IO object that will receive <code>#write</code> and <code>#close</code> messages.</td>
|
274
|
+
</tr>
|
275
|
+
<tr>
|
276
|
+
<td style="vertical-align:top;"><code>:filename</code></td>
|
277
|
+
<td>The name of the file to write log messages to.</td>
|
278
|
+
</tr>
|
279
|
+
<tr>
|
280
|
+
<td style="vertical-align:top;"><code>:roll_age</code></td>
|
281
|
+
<td>The number of days before the log should be rolled.</td>
|
282
|
+
</tr>
|
283
|
+
<tr>
|
284
|
+
<td style="vertical-align:top;"><code>:roll_frequency</code></td>
|
285
|
+
<td>Either ‘daily’, ‘weekly’, or ‘monthly’, indicating how often the log should be rolled.</td>
|
286
|
+
</tr>
|
287
|
+
<tr>
|
288
|
+
<td style="vertical-align:top;"><code>:roll_size</code></td>
|
289
|
+
<td>The maximum size that a log file may grow to before it is rolled.</td>
|
290
|
+
</tr>
|
291
|
+
<tr>
|
292
|
+
<td style="vertical-align:top;"><code>:default_date_format</code></td>
|
293
|
+
<td>A <code>strftime</code>-formatted string to use for formatting message dates and times.</td>
|
294
|
+
</tr>
|
295
|
+
<tr>
|
296
|
+
<td style="vertical-align:top;"><code>:default_message_format</code></td>
|
297
|
+
<td>A printf-styled format string to use when formatting messages. It’s format is reminiscent of Log4r, but differs in some options. See the <span class="caps">API</span> documentation for Needle::Logger for details.</td>
|
298
|
+
</tr>
|
299
|
+
<tr>
|
300
|
+
<td style="vertical-align:top;"><code>:default_level</code></td>
|
301
|
+
<td>One of <code>:debug</code>, <code>:info</code>, <code>:warn</code>, <code>:error</code>, or <code>:fatal</code>, indicating the default level of logging detail to use.</td>
|
302
|
+
</tr>
|
303
|
+
<tr>
|
304
|
+
<td style="vertical-align:top;"><code>:levels</code></td>
|
305
|
+
<td>Another hash of pattern/level or pattern/hash pairs, where the pattern is a string describing the logger name (or names) that it matches. If the corresponding value of a key is a symbol or a string, then it describes the logging level to use for loggers that match it. If the value is a hash, then it may contain <code>:level</code>, <code>:date_format</code>, or <code>:message_format</code> keys that apply to matching logs.</td>
|
306
|
+
</tr>
|
307
|
+
</table>
|
308
|
+
|
309
|
+
<p>By default, the filename is ”./needle.log”, and the roll_size is one megabyte. The default level is <code>:debug</code>. If you wanted to specify a different filename and default level of <code>:warn</code>, you could do:</p>
|
310
|
+
|
311
|
+
<pre>
|
312
|
+
reg = Needle::Registry.new(
|
313
|
+
:logs => {
|
314
|
+
:filename => "./my-app.log",
|
315
|
+
:default_level => :warn }
|
316
|
+
)
|
317
|
+
</pre>
|
318
|
+
|
319
|
+
<p>Alternatively, you can easily put the logging configuration in a <span class="caps">YAML</span> file and read it in, like so:</p>
|
320
|
+
|
321
|
+
<pre>
|
322
|
+
require 'yaml'
|
323
|
+
reg = Needle::Registry.new(
|
324
|
+
:logs => YAML.load( File.read( "log-conf.yml" ) )
|
325
|
+
)
|
326
|
+
</pre>
|
327
|
+
</div>
|
328
|
+
|
329
|
+
|
330
|
+
|
170
331
|
|
171
332
|
</div>
|
172
333
|
|