public
Description: Book for liftweb: http://www.apress.com/book/view/1430224215
Homepage: http://groups.google.com/group/the-lift-book
Clone URL: git://github.com/tjweir/liftbook.git
liftbook / apdx-logging.lyx
100644 626 lines (467 sloc) 13.036 kb
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#LyX 1.6.0 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\use_default_options false
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
 
\graphics default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
 
\begin_body
 
\begin_layout Chapter
Logging in Lift
\begin_inset CommandInset label
LatexCommand label
name "cha:Logging-in-Lift"
 
\end_inset
 
 
\end_layout
 
\begin_layout Standard
Logging is a useful part of any application, Lift app or otherwise.
 Logging can be used to audit user actions, give insight into runtime performanc
e and operation, and even to troubleshoot and debug issues.
 Lift comes with a thin logging facade that sits on top of the log4j library
\begin_inset Foot
status open
 
\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "http://logging.apache.org/log4j/"
 
\end_inset
 
 
\end_layout
 
\end_inset
 
.
 This facade provides simple access to most common logging functions and
 aims to be easy to use, flexible, and most important, inconspicuous.
 If you do decide that Lift's logging facilities don't meet your needs,
 it's possible to use any Java logging framework you desire, but it's still
 useful to understand Lift's framework since Lift uses it internally for
 logging.
\end_layout
 
\begin_layout Section
Logging Configuration
\end_layout
 
\begin_layout Standard
Out of the box Lift sets up a log4j logging hierarchy using a cascading
 setup as defined in the
\family typewriter
LogBoot._log4jSetup
\family default
 method.
 First, it checks to see if log4j is already configured; this is commonly
 the case when a Lift application is deployed on a J2EE app server that
 uses log4j (JBoss, for example).
 If not, then it attempts to locate a
\family typewriter
log4j.props
\family default
 or
\family typewriter
log4j.xml
\family default
 configuration file in the class path and if it finds either it will use
 them for configuration.
 Failing that, it will fall back to configuring log4j using the
\begin_inset Newline linebreak
\end_inset
 
 
\family typewriter
LogBoot.defaultProps
\family default
 variable.
 Usually it's simplest to just provide a
\family typewriter
log4j.props
\family default
 or
\family typewriter
log4j.xml
\family default
 file in your resources to configure logging.
 If you prefer, you can provide your own
\family typewriter
LogBoot.loggerSetup
\family default
 function to use instead of
\family typewriter
_log4jSetup
\family default
 if you want to do something special, like configureAndWatch.
\end_layout
 
\begin_layout Standard
If you would prefer, Lift's logging framework also supports slf4j
\begin_inset Foot
status open
 
\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "http://www.slf4j.org/"
 
\end_inset
 
 
\end_layout
 
\end_inset
 
.
 Enabling slf4j is as simple as calling Slf4jLogBoot.enable in your boot
 method, as shown in listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Enabling-slf4j"
 
\end_inset
 
.
 Note that you need to add both slf4j and a backend as dependencies in your
 pom.xml, and you should configure slf4j before enabling it.
\end_layout
 
\begin_layout Standard
\begin_inset listings
inline false
status open
 
\begin_layout Plain Layout
 
\begin_inset Caption
 
\begin_layout Plain Layout
Enabling slf4j
\begin_inset CommandInset label
LatexCommand label
name "lst:Enabling-slf4j"
 
\end_inset
 
 
\end_layout
 
\end_inset
 
 
\end_layout
 
\begin_layout Plain Layout
 
class Boot {
\end_layout
 
\begin_layout Plain Layout
 
  def boot {
\end_layout
 
\begin_layout Plain Layout
 
    Slf4jLogBoot.enable()
\end_layout
 
\begin_layout Plain Layout
 
    ...
\end_layout
 
\begin_layout Plain Layout
 
  }
\end_layout
 
\begin_layout Plain Layout
 
}
\end_layout
 
\end_inset
 
 
\end_layout
 
\begin_layout Section
Basic Logging
\end_layout
 
\begin_layout Standard
Logging in Lift is performed via the net.liftweb.util.Log object.
 This object provides some basic log methods which we'll summarize here.
 Each log method comes in two forms: one with just an Object argument, and
 one with Object and Throwable arguments.
 These correspond one-to-one with the log4j log methods, although the parameters
 are passed by-name; this is done so that computation of the log message
 can be deferred.
 This is useful to avoid processing messages for log statements below the
 current logging threshold, a topic we'll cover more in section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Log-Levels"
 
\end_inset
 
.
\end_layout
 
\begin_layout Description
trace This logs a message at trace level.
 Trace level is generally intended for very detailed
\begin_inset Quotes eld
\end_inset
 
tracing
\begin_inset Quotes erd
\end_inset
 
 of processing, even more detailed than debug level.
\end_layout
 
\begin_layout Description
debug Logs a message at debug level.
 This level is usually used to output internal variable values or other
 information that is useful in debugging and troubleshooting an app.
\end_layout
 
\begin_layout Description
info Logs a message at info level.
 This level is appropriate for general information about the app.
\end_layout
 
\begin_layout Description
warn Logs a message at warning level.
 This level should be used for reporting issues that are in error but can
 be handled cleanly, such as someone trying to submit a character string
 for a numeric field value.
\end_layout
 
\begin_layout Description
error Logs a message at error level.
 This level should be used for messages relating to errors that can't be
 handled cleanly, such as a failure to connect to a backing database.
\end_layout
 
\begin_layout Description
fatal Logs a message at the fatal level.
 This level should be used for messages that relate to conditions under
 which the application cannot continue to function, such as an OutOfMemory
 exception.
\end_layout
 
\begin_layout Description
never This essentially throws away the passed message.
 This is useful for some of Lift's functionality that requires a log output
 function (Schemifier.schemify, for example, in section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Schema-Mapping"
 
\end_inset
 
).
\end_layout
 
\begin_layout Description
assertLog This allows you to test an assertion condition and if true, logs
 the assertion as well as a given message.
\end_layout
 
\begin_layout Standard
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Some-example-logging"
 
\end_inset
 
 shows an example of using a few different Logging methods within a form
 processing function that handles logins.
 We start out with some tracing of method entry (and corresponding exit
 at the end), then add an assertion to log the case where someone is logging
 in a second time.
 We add a debug statement that uses a function to generate the message so
 that the string concatenation won't take place if debug logging is disabled.
 Finally, we log an error message if a problem ocurred during authentication.
\end_layout
 
\begin_layout Standard
\begin_inset listings
inline false
status open
 
\begin_layout Plain Layout
 
\begin_inset Caption
 
\begin_layout Plain Layout
Some example logging
\begin_inset CommandInset label
LatexCommand label
name "lst:Some-example-logging"
 
\end_inset
 
 
\end_layout
 
\end_inset
 
 
\end_layout
 
\begin_layout Plain Layout
 
import net.liftweb.util.Log
\end_layout
 
\begin_layout Plain Layout
 
def processLogin(name : String, password : String) = {
\end_layout
 
\begin_layout Plain Layout
 
  Log.trace("Starting login process")
\end_layout
 
\begin_layout Plain Layout
 
  try {
\end_layout
 
\begin_layout Plain Layout
 
    ...
\end_layout
 
\begin_layout Plain Layout
 
    Log.assertLog(User.currentUser.isDefined,
\end_layout
 
\begin_layout Plain Layout
 
                  "Redundant authentication!")
\end_layout
 
\begin_layout Plain Layout
 
    ...
\end_layout
 
\begin_layout Plain Layout
 
    Log.debug(() => ("Retreiving auth data for " + name))
\end_layout
 
\begin_layout Plain Layout
 
    ...
\end_layout
 
\begin_layout Plain Layout
 
    if (!authenticated) {
\end_layout
 
\begin_layout Plain Layout
 
      Log.error("Authentication failed for " + name)
\end_layout
 
\begin_layout Plain Layout
 
    }
\end_layout
 
\begin_layout Plain Layout
 
  } finally {
\end_layout
 
\begin_layout Plain Layout
 
    Log.trace("Login process complete")
\end_layout
 
\begin_layout Plain Layout
 
  }
\end_layout
 
\begin_layout Plain Layout
 
}
\end_layout
 
\end_inset
 
 
\end_layout
 
\begin_layout Section
Log Level Guards
\begin_inset CommandInset label
LatexCommand label
name "sec:Log-Levels"
 
\end_inset
 
 
\end_layout
 
\begin_layout Standard
We want to provide a brief discussion on the use of log guards and why they're
 usually not needed with Lift's log framework.
 A log guard is a simple test to see if a given log statement will actually
 be processed.
 The Log object provides a test method (returning a boolean) for each log
 level:
\end_layout
 
\begin_layout Itemize
isDebugEnabled
\end_layout
 
\begin_layout Itemize
isErrorEnabled
\end_layout
 
\begin_layout Itemize
isInfoEnabled
\end_layout
 
\begin_layout Itemize
isTraceEnabled
\end_layout
 
\begin_layout Itemize
isWarnEnabled
\end_layout
 
\begin_layout Standard
Fatal logging is always enabled, so there is no test for that level.
 Log guards are fairly common in logging frameworks to avoid expensive computati
on of log message that won't actually be used.
 This is particularly relevant with debug logging, since they often cover
 a large section of code and usually aren't enabled in production.
 The Log object can implicitly do log guards for you because of the pass-by-name
 message parameters.
 As we showed in listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Some-example-logging"
 
\end_inset
 
, simply converting your log message into a closure allows the Log object
 decide whether to execute the closure based on the current log level.
 You get the flexibility and simplicity of adding log statements anywhere
 you want without explicit log guards, without losing the performance benefit
 of the guards.
 To explain it a bit more, let's assume for instace that the
\family typewriter
debug
\family default
 method would have been declared as
\family typewriter
def debug(msg:AnyRef): Unit.
 
\family default
When
\family typewriter
debug
\family default
 would be called the parameter will be first evaluated and then passed to
 the method.
 Inside the method we have the test to see if the debug i enabled to know
 if we actaully need to trace that message or not.
 Well in this case even if the debugging level is turned off we still have
 the evaluation of the parameters and that leads to unnecessary computing
 and in an application that heaviliy uses logging that would likely leads
 to relevant performance impact.
 So in this
\begin_inset Quotes eld
\end_inset
 
eagerly
\begin_inset Quotes erd
\end_inset
 
 evaluation situation we have to test if the debug level is on even before
 calling the
\family typewriter
debug
\family default
 method.
 Something like
\family typewriter
if (Log.isDebugEnabled) {debug(
\begin_inset Quotes eld
\end_inset
 
Retreiving auth data
\begin_inset Quotes erd
\end_inset
 
)}
\family default
.
 Not very convenient.
 So because the logging functions take pass-by-name functions as parameter
 they will be evaluated lazily and only if the appropriate debugging level
 is set.
\end_layout
 
\begin_layout Section
Logging Mapper Queries
\end_layout
 
\begin_layout Standard
In addition to application-level logging, the Mapper framework provides
 a simple interface for logging queries.
 The DB.addLogFunc method takes a function
\begin_inset Formula $(String,Long)\Rightarrow Any$
\end_inset
 
 that can log the actual query string along with its execution time in milliseco
nds.
 Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Mapper-Logging"
 
\end_inset
 
 shows a trivial log function example.
\end_layout
 
\begin_layout Standard
\begin_inset listings
inline false
status open
 
\begin_layout Plain Layout
 
\begin_inset Caption
 
\begin_layout Plain Layout
Mapper Logging
\begin_inset CommandInset label
LatexCommand label
name "lst:Mapper-Logging"
 
\end_inset
 
 
\end_layout
 
\end_inset
 
 
\end_layout
 
\begin_layout Plain Layout
 
DB.addLogFunc((query, time) =>
\end_layout
 
\begin_layout Plain Layout
 
  Log.debug(() => (query + ":" + time + "ms")))
\end_layout
 
\end_inset
 
 
\end_layout
 
\end_body
\end_document