-
Notifications
You must be signed in to change notification settings - Fork 16
/
apdx-logging.lyx
1254 lines (914 loc) · 22.4 KB
/
apdx-logging.lyx
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 1.6.7 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 SLF4J library
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://www.slf4j.org/
\end_layout
\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 Backend
\begin_inset CommandInset label
LatexCommand label
name "sec:Logging-Backend"
\end_inset
\end_layout
\begin_layout Standard
The detailed configuration of SLF4J is outside the scope of this book, but
we would like to highlight a few key notes concerning SLF4J.
First, SLF4J splits the API from the implementation into separate JAR files.
This allows you to choose the backing implementation that best suits your
deployment.
For example, SLF4J contains implementations for JDK logging, log4j, and
more.
For example, the JBoss application server version 5 and newer comes bundled
with its own implementation of the SLF4J API, so you can simply deploy
a Lift WAR on JBoss and use the server's configuration and logging.
Similarly, Jetty comes bundled with the SLF4J API.
\end_layout
\begin_layout Standard
Outside of container-provided implementations, the logback logging framework
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://logback.qos.ch/
\end_layout
\end_inset
\end_layout
\end_inset
is a very nice implementation of the SLF4J API.
It's written by the same people who wrote log4j and has much improved performan
ce and functionality over log4j
\begin_inset Foot
status open
\begin_layout Plain Layout
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://logback.qos.ch/reasonsToSwitch.html
\end_layout
\end_inset
\end_layout
\end_inset
.
Because logback-classic implements the SLF4J API, you simply need to drop
the logback-classic jar into your classpath, or add it as a dependency
in your WAR to use it.
One particularly nice feature of logback is that if it can't locate a configura
tion file (which can be written in XML, Groovy, or soon, Scala), it will
default to logging everything to the console.
\end_layout
\begin_layout Section
Basic Logging
\end_layout
\begin_layout Standard
Logging in Lift is performed via the
\family typewriter
net.liftweb.common.Logger
\family default
object and trait.
The
\family typewriter
Logger
\family default
object provides a set of utility methods for configuration and instantiation
of logger instances.
The
\family typewriter
Logger
\family default
trait can be mixed into your snippet, comet, or other classes to provide
direct methods for logging (we'll cover these in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Logging-Methods"
\end_inset
).
\end_layout
\begin_layout Subsection
Logging Setup
\end_layout
\begin_layout Standard
The first step in utilizing Lift's logging is to configure the logger.
As we mentioned in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Logging-Backend"
\end_inset
, Lift uses the SLF4J framework.
The configuration of SLF4J depends on which backing implementation you
use, but Lift comes with helpers for both log4j (
\family typewriter
net.liftweb.common.Log4j
\family default
) and for logback (
\family typewriter
net.liftweb.common.Logback
\family default
).
These helpers are utilized with the
\family typewriter
Logger
\family default
object's
\family typewriter
setup
\family default
var.
The
\family typewriter
Log4j
\family default
helpers provides methods that can be used to load a configuration from
a
\family typewriter
String
\family default
, a file (either XML or properties), or with Lift's default (console) logging.
The
\family typewriter
Logback
\family default
helper provides a single method to load a configuration from an XML file.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Configuring-Logback-via"
\end_inset
shows how you could use the
\family typewriter
Logback
\family default
helper in the
\family typewriter
Boot.boot
\family default
method to configure logging from the
\emph on
logconfig.xml
\emph default
file.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Configuring Logback via Logger.setup
\begin_inset CommandInset label
LatexCommand label
name "lst:Configuring-Logback-via"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
def boot {
\end_layout
\begin_layout Plain Layout
// Get a reference to the config from our classpath
\end_layout
\begin_layout Plain Layout
val logUrl = LiftRules.getResource("logconfig.xml")
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
// Apply the reference, if found
\end_layout
\begin_layout Plain Layout
logUrl.foreach { Logger.setup = Logback.withFile(_) }
\end_layout
\begin_layout Plain Layout
...
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
Obtaining a Logger
\end_layout
\begin_layout Standard
There are two basic means of obtaining a logger instance.
The first is to mix the
\family typewriter
Logger
\family default
trait into your class.
The second is to instantiate a logger using the
\family typewriter
Logger
\family default
object's
\family typewriter
apply
\family default
methods.
\end_layout
\begin_layout Standard
Mixing the
\family typewriter
Logger
\family default
trait into your class is a very simple way to add logging methods in your
code, as shown in Listing.
When you do this, the underlying logger will have a name derived from your
class's dynamic type.
For example, Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Mixing-Logger-into"
\end_inset
shows the definition of our
\family typewriter
Accounts
\family default
snippet object with a
\family typewriter
Logger
\family default
trait mixed in.
When we log in the
\family typewriter
Accounts
\family default
object the name will be the fully-qualified classname, or
\begin_inset Quotes eld
\end_inset
com.pocketchangeapp.snippet.Accounts
\begin_inset Quotes erd
\end_inset
in our case.
When you mix the Logger trait into your class, you can access the logging
methods (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Logging-Methods"
\end_inset
) directly.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Mixing Logger into a Class
\begin_inset CommandInset label
LatexCommand label
name "lst:Mixing-Logger-into"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
object Accounts extends DispatchSnippet with Logger {
\end_layout
\begin_layout Plain Layout
...
\end_layout
\begin_layout Plain Layout
warn("This is a warning")
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The second basic way to obtain a logger instance is to construct one directly
via the
\family typewriter
Logger
\family default
object's
\family typewriter
apply
\family default
method, as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Constructing-a-Logger"
\end_inset
.
The
\family typewriter
apply
\family default
method takes either a
\family typewriter
String
\family default
or
\family typewriter
Class
\family default
that will be used to determine the constructed logger's name.
In our example, we use the
\family typewriter
classOf[Boot]
\family default
, so our logger will be named
\begin_inset Quotes eld
\end_inset
bootstrap.liftweb.Boot
\begin_inset Quotes erd
\end_inset
.
When you obtain a logger via construction you need to access the logging
methods via the logger instance.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Constructing a Logger instance
\begin_inset CommandInset label
LatexCommand label
name "lst:Constructing-a-Logger"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class Boot {
\end_layout
\begin_layout Plain Layout
// Set up a logger to use for startup messages
\end_layout
\begin_layout Plain Layout
val logger = Logger(classOf[Boot])
\end_layout
\begin_layout Plain Layout
...
\end_layout
\begin_layout Plain Layout
logger.warn("This is a warning")
\end_layout
\end_inset
\end_layout
\begin_layout Standard
There is a third, hybrid, approach to obtaining a
\family typewriter
Logger
\family default
that allows you to mix in the
\family typewriter
Logger
\family default
trait while controlling the logger name.
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Mixing-in-a-named-logger"
\end_inset
shows how we can mix in th trait and then override the underlying SLF4J
logger with our own named instance.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Mixing in a named Logger
\begin_inset CommandInset label
LatexCommand label
name "lst:Mixing-in-a-named-logger"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class AddEntry extends StatefulSnippet with Logger {
\end_layout
\begin_layout Plain Layout
// Use a different name for our logger
\end_layout
\begin_layout Plain Layout
import org.slf4j.LoggerFactory
\end_layout
\begin_layout Plain Layout
override val _logger = LoggerFactory.getLogger("EntryEdit")
\end_layout
\end_inset
\end_layout
\begin_layout Subsection
Logging Methods
\begin_inset CommandInset label
LatexCommand label
name "sub:Logging-Methods"
\end_inset
\end_layout
\begin_layout Standard
The
\family typewriter
Logger
\family default
trait provides some basic log methods which we'll summarize here.
Each log method comes in three forms: one with just a
\begin_inset Formula $\Rightarrow Object$
\end_inset
argument, one with a
\begin_inset Formula $\Rightarrow Object$
\end_inset
and
\family typewriter
Throwable
\family default
argument, and one with a
\begin_inset Formula $\Rightarrow Object$
\end_inset
,
\family typewriter
Throwable
\family default
, and
\family typewriter
Marker
\family default
argument.
These correspond roughly to the SLF4J log methods, although the order of
the parameters is different and the parameters are passed by-name.
Pass-by-name arguments are used 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
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 our REST API authentication hook, which uses a few different Logging
methods within the handler method.
\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
LiftRules.authentication = HttpBasicAuthentication("PocketChange") {
\end_layout
\begin_layout Plain Layout
case (userEmail, userPass, _) => {
\end_layout
\begin_layout Plain Layout
logger.debug("Authenticating: " + userEmail)
\end_layout
\begin_layout Plain Layout
User.find(By(User.email, userEmail)).map { user =>
\end_layout
\begin_layout Plain Layout
if (user.password.match_?(userPass)) {
\end_layout
\begin_layout Plain Layout
logger.info("Auth succeeded for " + userEmail)
\end_layout
\begin_layout Plain Layout
User.logUserIn(user)
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
// Set an MDC for logging purposes
\end_layout
\begin_layout Plain Layout
MDC.put("user", user.shortName)
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
// Compute all of the user roles
\end_layout
\begin_layout Plain Layout
userRoles(user.editable.map(acct => AuthRole("editAcct:" + acct.id))
++
\end_layout
\begin_layout Plain Layout
user.allAccounts.map(acct => AuthRole("viewAcct:" + acct.id)))
\end_layout
\begin_layout Plain Layout
true
\end_layout
\begin_layout Plain Layout
} else {
\end_layout
\begin_layout Plain Layout
logger.warn("Auth failed for " + userEmail)
\end_layout
\begin_layout Plain Layout
false
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
} openOr false
\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
Log guards are fairly common in logging frameworks to avoid expensive computatio
n 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 level is 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 uses logging heavily that would likely lead
to a performance impact.
\end_layout
\begin_layout Section
Logging Mapper Queries
\begin_inset CommandInset label
LatexCommand label
name "sec:Logging-Mapper-Queries"
\end_inset
\end_layout
\begin_layout Standard
If you want to log Mapper query activity, there are two main approaches.
The first is to utilize the
\family typewriter
net.liftweb.mapper.DB.addLogFunc
\family default
method to add your own logging function.
A logging function is of the type
\begin_inset Formula $(DBLog,Long)\Rightarrow Any$
\end_inset
.
The
\family typewriter
DBLog
\family default
trait contains two separate lists of log entries, one for meta operations
(such as
\family typewriter
getFetchSize
\family default
) and one for actual work statements (such as
\family typewriter
executeQuery
\family default
).
You can access these two log lists via either the
\family typewriter
metaEntries