-
Notifications
You must be signed in to change notification settings - Fork 32
/
configurator.lisp
1288 lines (1205 loc) · 59.4 KB
/
configurator.lisp
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
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;
;;; Copyright (c) 2012, Max Mikhanosha. All rights reserved.
;;;
;;; This file is licensed to You under the Apache License, Version 2.0
;;; (the "License"); you may not use this file except in compliance
;;; with the License. You may obtain a copy of the License at
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;;;
;;; Contains (log-config) function and default logging initialization
;;;
(in-package #:log4cl)
(defun log-config (&rest args)
"Very DWIM oriented friendly way of configuring loggers and appenders.
(LOG:CONFIG [LOGGER-IDENTIFIER] OPTION1 OPTION2...)
LOGGER-IDENTIFIER is optional and defaults to the root logger. It can be
one of the following:
- Logger instance ie result of (LOG:CATEGORY) expansion, or any other form
that returns a logger.
- A list of logger categories, basically a shortcut for (LOG:CATEGORY
'(CAT1 CAT2 CAT3)). An error will be given if logger does not exist. If you want
to ensure logger is created, even if it did not exist before, use
(LOG:CONFIG (LOG:CATEGORY ...) ...)
Without any options (LOG:CONFIG) displays current configuration
---------------|---------------------------------------------------------------
Option | Description
---------------|---------------------------------------------------------------
MAIN OPTIONS.
---------------|---------------------------------------------------------------
:INFO | Or any other keyword identifying a log level, which can be
:DEBUG | shortened to its shortest unambiguous prefix, such as :D.
| Changes the logger level to that level.
---------------|---------------------------------------------------------------
:SANE | Removes logger appenders, then arranges for the logging output
| to be always logged to dynamic value of *TERMINAL-IO*
|
| If used with :DAILY then console appender is not added, unless
| :CONSOLE, :THIS-CONSOLE or :TRICKY-CONSOLE is also specified.
|
| The pattern layout added is affected by many of the pattern
| options below.
---------------|---------------------------------------------------------------
APPENDER OPTIONS
---------------|---------------------------------------------------------------
:CONSOLE | Adds CONSOLE-APPENDER to the logger. Console appender logs
| into current console as indicated by *TERMINAL-IO* stream
---------------|---------------------------------------------------------------
:THIS-CONSOLE | Adds THIS-CONSOLE-APPENDER to the logger. It captures the
(or :THIS) | current value of *TERMINAL-IO* by recursively resolving any
| synonym or two-way streams, and continues to log the
| remembered value, even if it goes out dynamic scope.
|
| On any stream errors it will auto-remove itself.
---------------|---------------------------------------------------------------
:TRICKY-CONSOLE| Adds TRICKY-CONSOLE-APPENDER which acts exactly like
(or :TRICKY) | THIS-CONSOLE appender above, but it checks if dynamic value
| of *TERMINAL-IO* resolves to the same stream and ignores the
| log message if it does.
|
| When debugging multi-threaded code from REPL, this results in
| REPL thread logging to REPL, while threads with some other
| value of *TERMINAL-IO* stream will output to both.
|
| As a shortcut, if THIS-CONSOLE is specified and global console
| appender already exists, it will add TRICKY-CONSOLE instead.
---------------|---------------------------------------------------------------
:SANE2 | Shortcut for :SANE :TRICKY (mnemonic two -> multiple threads)
---------------|---------------------------------------------------------------
:STREAM stream| Changes the stream used for above two dedicated stream
| appenders.
---------------|---------------------------------------------------------------
:DAILY FILE | Adds file appender logging to the named file, which will be
| re-opened every day, with old log file renamed to FILE.%Y%m%d.
|
| Removes any other file based appenders from the logger.
|
| FILE can also contain %Y%m%d like pattern, expansion of which
| will determine when new log file would be opened.
---------------|---------------------------------------------------------------
:FILTER level | Makes all appenders added by above keywords drop messages
| below specified level.
---------------|---------------------------------------------------------------
LAYOUT OPTIONS
General note about layout options, if any appender options are specified
the layout options only affect PATTERN-LAYOUT for new appenders created
by LOG:CONFIG command
But if no appender options are specified, but layout options are, LOG:CONFIG
will change the pattern layout on all console based appenders that output
current *TERMINAL-IO*
---------------|---------------------------------------------------------------
:PATTERN | For any new appenders added, specifies the conversion pattern
| for the PATTERN-LAYOUT. If not given, default pattern will be
| used, modified by below options
---------------|---------------------------------------------------------------
:PRETTY | Add {pretty} option to the pattern to force pretty printing
:NOPRETTY | Add {nopretty} option to the pattern to force no pretty printing
| without one of these, global value is in effect
---------------|---------------------------------------------------------------
:TIME/:NOTIME | Include time into default pattern, default is :TIME
---------------|---------------------------------------------------------------
:FILE or | Include file name into default pattern, :FILE2 uses alternate
:FILE2 | position for the file (in front of the package).
:NOFILE | :NOFILE does not show the file
---------------|---------------------------------------------------------------
:THREAD | Include thread name into default pattern, it will be after the
[<n>[<n2>]] | time, in [%t] format. If :THERAD argument is followed by one
| or two numbers, they will be used as min/max field width.
---------------|---------------------------------------------------------------
:NDC | Include NDC context into default pattern, with optional min/max
[<n>[<n2>]] | field width flags. When used together with :THREAD NDC will be
| printed after the thread name, separated by dash, like this
| example: \"[threadname-ndc]\"; it will not be shown if unbound
|
| Without :THREAD, its shown in its own square brackets, with
| entire construct not shown if unbound.
---------------|---------------------------------------------------------------
:NOPACKAGE | Add {nopackage} option to the pattern (binds orig package at
:PACKAGE | the site of the log statement. PACKAGE binds keyword package,
| so everything is printed with package prefix
---------------|---------------------------------------------------------------
:TWOLINE | Changes pattern layout to print hard newline before actual log
(or :2LINE) | message. Only makes sense with NOPRETTY or when logging into
| files.
|
| Pretty printing does better job at line splitting then forced
| two line layout, with short log statements placed on a single
| line and longer ones wrapping.
---------------|---------------------------------------------------------------
ASSORTED OTHER OPTIONS
---------------|---------------------------------------------------------------
:PROPERTIES | Configure with PROPERTY-CONFIGURATOR by parsing specified
FILE | properties file
---------------|---------------------------------------------------------------
:WATCH | Used with :PROPERTIES, uses watcher thread to check
| properties file modification time, and reloads if it changes
---------------|---------------------------------------------------------------
:IMMEDIATE- | Used with :SANE, :DAILY or :CONSOLE to create new appenders
FLUSH | with :IMMEDIATE-FLUSH T option, which prevents automatic
| startup of hierarchy watcher thread, which is used for
| auto-flushing.
---------------|---------------------------------------------------------------
:NOADDITIVE | Makes logger non-additive (does not propagate to parent)
:OWN | Shortcut for non-additive (usually has appenders on its own)
:ADDITIVE | Sets additive flag back to T.
---------------|---------------------------------------------------------------
:CLEAR | Reset the child in the hierarchy, by unsetting their log level
| and/or removing appenders from them. Without any other flags
| unset the log levels.
|
:LEVELS | Clears the log levels, this is the default
:APPENDERS | Removes any appenders, levels will not be cleared unless
| :LEVELS is also specified
|
:ALL | Normally :CLEAR does not touch non-additive loggers, that is
| the ones that don't pass messages to parents. This flag forces
| clearing of non-additive loggers
|
| Note that this option does not change the logger being acted
| upon, just its children. See next option
---------------|---------------------------------------------------------------
:REMOVE <num> | Removes specific appender from the logger. Numbers are 1-based,
| and are same ones displayed by LOG:CONFIG without arguments
---------------|---------------------------------------------------------------
:SELF | Configures the LOG4CL logger, which can be used to debug
| Log4CL itself. Normally all other LOG:CONFIG hides the
| it from view.
---------------|---------------------------------------------------------------
:FORCE-ADD | Normally if you specify :CONSOLE :THIS-CONSOLE or
| :TRICKY-CONSOLE without :SANE (which clears out existing
| appenders), an error will be given if there are any standard
| console appenders that already log to *TERMINAL-IO* or :STREAM
| argument.
|
| This is to prevent from creating duplicate output.
|
| Adding :FORCE-ADD flag skips the above check, and allows you
| to add new console appender regardless.
---------------|---------------------------------------------------------------
:BACKUP | Used together with :DAILY, specifies the :BACKUP-NAME-FORMAT,
| see docstring for the DAILY-FILE-APPENDER class.
|
| For example specifying a DAILY <file> :BACKUP NIL will always
| log to statically named FILE without rolling.
|
| Defaults to NIL if FILE contains percent character or
| FILE.%Y%m%d otherwise.
---------------|---------------------------------------------------------------
Examples:
* (LOG:CONFIG :D) -- Changes root logger level to debug
* (LOG:CONFIG :SANE) -- Drops all console appenders, change level
to INFO, makes all output appear on current console
* (LOG:CONFIG :SANE2) -- As above, but copy all other threads output
to current terminal.
* (LOG:CONFIG :PRETTY :THREAD) - changes active console appenders
layout to force pretty printing and add thread info.
* (LOG:CONFIG :NOPRETTY :PACKAGE) - changes layout to force no
pretty printing, and restoring original *PACKAGE* when printing
* (LOG:CONFIG :WARN :CLEAR) -- Changes root logger level to warnings,
and unset child logger levels.
* (LOG:CONFIG '(PACKAGE) :OWN :DEBUG :DAILY \"package-log.%Y%m%d\")
Configures the logger PACKAGE with debug log level, logging into
the file \"package-log.20130510\" which will be rolled over daily;
makes logger non-additive so messages will not be propagated to
logger parents. (To see them on console, remove the :OWN flag, or
add :CONSOLE to the command)
Note: in above example if package name is dotted, you need to
specify the split category list, so if your package name is
COM.EXAMPLE.FOO logger categories will be '(COM EXAMPLE FOO)
"
(let ((logger nil)
sane clear all own noown daily pattern
backup had-backup
file file2 nofile
time notime
level layout console
oneline twoline
orig-args
self appenders
filter
immediate-flush
properties watch
this-console tricky-console global-console
pretty nopretty
package nopackage
thread ndc
stream
pattern-specified-p
appender-specified-p
force-add
clear-levels clear-appenders
remove
stream-by-itself
logger-specified-how)
(declare (type (or null stream) stream))
(cond ((logger-p (car args))
(setq logger (pop args)
logger-specified-how 'logger))
((consp (car args))
(setq logger
(let ((cats (pop args)))
(setq logger-specified-how 'cats)
(or (instantiate-logger *package* cats t nil)
(log4cl-error "~@<Logger named ~S not found. If you want to create it, ~
use (~A:~A (~A:~A ~S) ...) instead~:@>"
cats
'#:log '#:config
'#:log '#:logger
cats)))))
((member :self args)
(setq logger +self-logger+ self t
logger-specified-how 'self
args (remove :self args))))
(or logger (setq logger *root-logger*
logger-specified-how nil))
(unless (setq orig-args args)
(return-from log-config (show-logger-settings logger)))
(loop
(let ((arg (or (pop args) (return))))
(case arg
(:self ; still in the arglist means 1st arg was a logger
(log4cl-error "Specifying a logger is incompatible with :SELF"))
(:sane (setq sane t global-console t))
(:sane2 (setq sane t global-console t tricky-console t))
((:clear :reset) (setq clear t))
(:levels (setq clear-levels t))
(:appenders (setq clear-appenders t))
(:all (setq all t))
((:own :nonadditive :noadditive) (setq own t noown nil))
(:additive (setq own nil noown t))
(:file (setq file t file2 nil nofile nil))
(:file2 (setq file nil file2 t nofile nil))
(:nofile (setq file nil file2 nil nofile t))
(:time (setq time t notime nil))
(:notime (setq time nil notime t))
(:immediate-flush (setq immediate-flush t))
((:twoline :two-line :2line) (setq oneline nil twoline t))
((:oneline :one-line :1line) (setq oneline t twoline nil))
(:console (setq console t global-console t))
(:thread
(setq thread
(let* ((min (when (integerp (car args)) (pop args)))
(max (when (integerp (car args)) (pop args))))
(declare (type (or null integer) min)
(type (or null (integer 1)) max))
(if (or min max) (list min max) t))))
(:ndc
(setq ndc
(let* ((min (when (integerp (car args)) (pop args)))
(max (when (integerp (car args)) (pop args))))
(declare (type (or null integer) min)
(type (or null (integer 1)) max))
(if (or min max) (list min max) t))))
(:pretty (setq pretty t nopretty nil))
(:nopretty (setq nopretty t pretty nil))
(:package (setq package t nopackage nil))
(:nopackage (setq nopackage t package nil))
((:this-console :this)
(setq console t
this-console t
tricky-console nil
global-console nil))
((:tricky-console :tricky)
(setq console t
tricky-console t
this-console nil))
(:force-add (setq force-add t))
(:watch (setq watch t))
(:backup
(setq backup (if args (pop args)
(log4cl-error ":BACKUP missing argument"))
had-backup t))
(:daily
(setq daily (or (pop args)
(log4cl-error ":DAILY missing argument"))))
(:remove
(setq remove (or (pop args)
(log4cl-error ":REMOVE missing argument"))))
(:properties
(setq properties (or (pop args)
(log4cl-error ":PROPERTIES missing argument"))))
(:stream
(setq stream (or (pop args)
(log4cl-error ":STREAM missing argument"))
console t))
(:pattern
(setq pattern (or (pop args)
(log4cl-error ":PATTERN missing argument"))))
(:filter
(setq filter (or (pop args)
(log4cl-error ":FILTER missing argument")))
(when filter
(setq filter (make-log-level filter))))
(t (let ((lvl (handler-case (log-level-from-object arg *package*)
(log4cl-error ()))))
(cond ((and level lvl)
(log4cl-error "Only one log level can be specified"))
(lvl (setq level lvl))
((keywordp arg)
(log4cl-error "Invalid LOG:CONFIG keyword ~s" arg))
(t (log4cl-error
"Don't know what do with argument ~S" arg))))))))
(when stream
(if (and (not this-console) (not tricky-console))
(setq tricky-console t))
(when (and (not sane) (not daily))
(setq stream-by-itself t)))
(setq
pattern-specified-p (or pattern oneline twoline thread
ndc file file2 nofile time notime
pretty nopretty
package nopackage)
appender-specified-p (or daily sane console))
(or level remove sane clear daily properties own noown console pattern-specified-p
(log4cl-error "Bad combination of options"))
(or (not properties)
(not (or sane daily console level pattern-specified-p))
(log4cl-error ":PROPERTIES can't be used with other options"))
(or (not remove)
(not (or sane daily console level pattern-specified-p))
(log4cl-error ":REMOVE can't be used with other options"))
(if (and pattern
(or twoline oneline file file2 nofile time notime
thread ndc pretty nopretty
package nopackage))
(error ":PATTERN isn't compatible with built-in pattern selection flags"))
;; If we are asked to screw with appenders, and given an source-file logger
;; object, do it on the parent.. Reason is that without automatic naming
;; support, or at toplevel (log:category) will return not package logger,
;; but package.<source> one
(when (and (eq logger-specified-how 'logger)
(typep logger 'source-file-logger)
(or pattern-specified-p appender-specified-p remove))
(setq logger (logger-parent logger))
(assert (not (typep logger 'source-file-logger))))
(when remove
(let ((list (logger-appenders logger)))
(if (<= 1 remove (length list))
(remove-appender logger (nth (1- remove) list))
(log4cl-error "Bad appender number ~d" remove)))
(return-from log-config))
(when (or appender-specified-p pattern-specified-p)
(setq layout (make-instance 'pattern-layout
:conversion-pattern
(or pattern (figure-out-pattern
:oneline (if (or oneline twoline) oneline t)
:twoline (if (or oneline twoline) twoline nil)
:time (if (or time notime) time t)
:file (if (or file file2 nofile) file t)
:file2 (if (or file file2 nofile) file2 nil)
:pretty pretty
:nopretty nopretty
:package package
:nopackage nopackage
:thread thread
:ndc ndc)))))
(when (and own (eq logger *root-logger*))
(error "Can't set root logger non-additive"))
(when level
(set-log-level logger level nil))
(when clear
(labels ((map-descendants-ignoring-self (function logger)
(let ((child-hash (%logger-child-hash logger)))
(when child-hash
(maphash (lambda (name logger)
(declare (ignore name))
(when (or self
(not (eq logger +self-logger+)))
(funcall function logger)
(unless (typep logger 'source-file-logger)
(map-descendants-ignoring-self function logger))))
child-hash)))))
(map-descendants-ignoring-self
(lambda (l)
(when (or (logger-additivity l) all)
(when (or clear-levels (not clear-appenders))
(set-log-level l +log-level-unset+ nil))
(when clear-appenders
(unless (eq l +self-meta-logger+)
(remove-all-appenders-internal l nil)))))
logger)))
;; kind of separate from appender stuff
(when (or own noown)
(set-additivity logger (not own) nil))
(when (or daily sane console)
(if sane
;; Only remove all appenders if :clear was also given,
;; otherwise don't touch file appenders
(if (and clear clear-appenders)
(remove-all-appenders-internal logger nil)
(dolist (a (logger-appenders logger))
(unless (typep a 'file-appender-base)
(remove-appender-internal logger a nil)))))
;; create daily appender
(when daily
(dolist (a (logger-appenders logger))
(when (typep a 'file-appender-base)
(remove-appender-internal logger a nil)))
(push (make-instance 'daily-file-appender
:name-format daily
:backup-name-format
(if had-backup backup
(unless (position #\% (if (pathnamep daily) (format nil "~a" daily)
daily))
(format nil "~a.%Y%m%d" daily)))
:filter filter
:layout layout)
appenders))
;; Add new console appender, only in these situations
;;
;; a) :console or :this-console is explicitly used
;; b) :sane is specified and :daily not
(when (or (and sane (not daily))
console)
(let* (have-this-console-p
have-global-console-p
have-tricky-console-p
(stream (resolve-stream (or stream *global-console*)))
(global-stream (resolve-stream *global-console*))
(global-stream-same-p (eq stream global-stream))
dups
s1 s2 s3)
(dolist (a (logger-appenders logger))
(when (appender-enabled-p a)
(typecase a
(console-appender (setq have-global-console-p t)
(push a dups))
(tricky-console-appender
(when (eq stream (appender-stream a))
(setq have-tricky-console-p t)
(push a dups)))
(this-console-appender
(when (eq stream (appender-stream a))
(setq have-this-console-p t)
(push a dups))))))
(when (not force-add)
;; if user tries to add this-console, and we have global
;; one add tricky one instead
(when (and (or have-global-console-p global-console)
this-console
global-stream-same-p)
(setq this-console nil
tricky-console t))
;; duplicate check
(when (or (and global-console
(or have-global-console-p
(and have-this-console-p
global-stream-same-p))
(setq s1 t))
(and this-console
(or (and have-global-console-p global-stream-same-p)
have-this-console-p)
(setq s2 t))
(and tricky-console
(or have-tricky-console-p
have-this-console-p)
(setq s3 t)))
;; Allow :stream *somewhere* :thread
;; to change pattern just on that stream
(if (and stream-by-itself
pattern-specified-p
s3 (not s1) (not s2))
(setq appender-specified-p nil)
(error "~@<Already logging to the same stream with ~4I~_~S ~%Specify either :SANE or :FORCE-ADD~:>"
dups))))
(unless s3
(when global-console
(push (make-instance 'console-appender
:filter filter
:layout layout)
appenders))
(when (or tricky-console this-console)
(push
(if tricky-console
(make-instance 'tricky-console-appender
:stream stream
:filter filter
:layout layout)
(make-instance 'this-console-appender
:stream stream
:filter filter
:layout layout))
appenders)))))
;; now add all of them to the logger
(dolist (a appenders)
(when immediate-flush
(setf (slot-value a 'immediate-flush) t))
(add-appender-internal logger a nil)))
(when properties
(configure (make-instance 'property-configurator) properties
:auto-reload watch))
;; When pattern options are specified, but not the option to add
;; new appender, we assume user wants to change layout for
;; existing console appenders
(when (and pattern-specified-p (or (not appender-specified-p)
stream-by-itself))
(let* (didit
(stream (resolve-stream (or stream *global-console*)))
(global-stream (resolve-stream *global-console*))
(global-stream-same-p (eq stream global-stream)))
(dolist (a (logger-appenders logger))
;; Only change global console appenders, or specific console
;; appender that log to console at log site
(when (appender-enabled-p a)
(when (or (and global-stream-same-p (typep a 'console-appender))
(and (typep a '(or this-console-appender
tricky-console-appender))
(eq (appender-stream a) stream)))
;; Assumes slot assignments atomic, but so is everything
;; else in log4cl
(setf (appender-layout a) layout)
(setq didit t))))
(unless didit
(error "Did not find any appenders to change."))))
(when self
;; This is special adhoc case of configuring the LOG4CL-iMPL:SELF. We need
;; special processing, because we want self-logging to survive
;; the (clear-logging-configuration), which is done doing tests
(let ((config
(cons
:own
;; we don't remember these
(remove-if (lambda (x)
(member x '(:own :self :clear :all :twoline :two-line
:2line :oneline :one-line :1line
:file :file2 :nofile
:time :notime
:pretty :nopretty
:package :nopackage
:thread :ndc)))
orig-args))))
;; If anything non-default was specified, remember
(let ((lst '(:sane :daily :console :this-console :tricky-console)))
(if (null (intersection lst config))
(dolist (elem (reverse lst))
(when (member elem *self-log-config*)
(push elem config)))))
(and twoline (push :twoline config))
(and file (push :file config))
(and file2 (push :file2 config))
(and nofile (push :nofile config))
(and notime (push :notime config))
(and time (push :time config))
(and pretty (push :pretty config))
(and nopretty (push :nopretty config))
(and package (push :package config))
(and nopackage (push :nopackage config))
(and thread (push :thread config))
(and ndc (push :ndc config))
(when (and (null level)
(logger-log-level logger))
(push (aref +log-level-to-keyword+
(logger-log-level logger))
config))
(setq *self-log-config* config)))
;; finally recalculate reach-ability
(adjust-logger logger)))
(defun clear-logging-configuration ()
"Delete all loggers configuration, leaving only LOG4CL-IMPL"
(labels ((reset (logger)
(remove-all-appenders logger)
(setf (svref (%logger-state logger) *hierarchy*)
(make-logger-state))
(map-logger-children #'reset logger)))
(reset *root-logger*)
(when *self-log-config*
(apply 'log-config +self-logger+ *self-log-config*))
(add-appender +self-meta-logger+ (make-instance 'console-appender
:layout (make-instance 'simple-layout)
:immediate-flush t))
(log-config +self-meta-logger+ :own))
(values))
(defun reset-logging-configuration ()
"Clear the logging configuration in the current hierarchy, and
configure root logger with INFO log level and a simple console
appender"
(clear-logging-configuration)
(add-appender *root-logger* (make-instance 'console-appender))
(setf (logger-log-level *root-logger*) +log-level-warn+)
(log-info "Logging configuration was reset to sane defaults"))
(defparameter *default-patterns*
'((:oneline t :time t :file t :pattern
"%&%<%I%;<;;>;-5p [%D{%H:%M:%S}]%t %g{}{}{:downcase}%:; ;F (%C{}{ }{:downcase})%2.2N - %:_%m%>%n")
(:oneline t :time t :file2 t :pattern
"%&%<%I%;<;;>;-5p [%D{%H:%M:%S}]%t %:;;; / ;F%g{}{}{:downcase}::(%C{}{ }{:downcase})%2.2N - %:_%m%>%n")
(:oneline t :time nil :file t :pattern
"%&%<%I%;<;;>;-5p%t %g{}{}{:downcase}%:; ;F (%C{}{ }{:downcase})%2.2N - %:_%m%>%n")
(:oneline t :time nil :file2 t :pattern
"%&%<%I%;<;;>;-5p%t %:;;; / ;F%g{}{}{:downcase}::(%C{}{ }{:downcase})%2.2N - %:_%m%>%n")
(:oneline t :time t :pattern
"%&%<%I%;<;;>;-5p [%D{%H:%M:%S}]%t %g{}{}{:downcase} (%C{}{ }{:downcase})%2.2N - %:_%m%>%n")
(:oneline t :time nil :pattern
"%&%<%I%;<;;>;-5p%t %g{}{}{:downcase} (%C{}{ }{:downcase})%2.2N - %:_%m%>%n")
(:twoline t :time t :file t :pattern
"%&%<%I%;<;;>;-5p [%D{%H:%M:%S}]%t %g{}{}{:downcase}%:; ;F (%C{}{ }{:downcase})%2.2N%:n* %m%>%n")
(:twoline t :time t :file2 t :pattern
"%&%<%I%;<;;>;-5p [%D{%H:%M:%S}]%t %:;;; / ;F%g{}{}{:downcase}::(%C{}{ }{:downcase})%2.2N%:n* %m%>%n")
(:twoline t :time nil :file t :pattern
"%&%<%I%;<;;>;-5p%t %g{}{}{:downcase}%:; ;F (%C{}{ }{:downcase})%2.2N%:n* %m%>%n")
(:twoline t :time nil :file2 t :pattern
"%&%<%I%;<;;>;-5p%t %:;;; / ;F%g{}{}{:downcase}::(%C{}{ }{:downcase})%2.2N%:n* %m%>%n")
(:twoline t :time t :pattern
"%&%<%I%;<;;>;-5p [%D{%H:%M:%S}]%t %g{}{}{:downcase} (%C{}{ }{:downcase})%2.2N%:n* %m%>%n")
(:twoline t :time nil :pattern
"%&%<%I%;<;;>;-5p%t %g{}{}{:downcase} (%C{}{ }{:downcase})%2.2N%:n* %m%>%n")))
(defun replace-in-string (s x y)
(let ((n (search x s)))
(if (not n) s
(concatenate
'string
(subseq s 0 n)
y
(subseq s (+ n (length x)))))))
(defun figure-out-pattern (&rest args)
(let ((pat (find-if (lambda (elem)
(every (lambda (prop)
(eql (getf args prop)
(getf elem prop)))
'(:oneline :twoline :time :file :file2)))
*default-patterns*)))
(let ((pat (getf (or pat (first *default-patterns*)) :pattern))
w1 w2
thread-width-fmt
ndc-width-fmt)
(flet (
;; search replace once in pattern
(s/ (x y)
(setq pat (replace-in-string pat x y)))
;; generate %<min.max> part of pattern if field width was
;; used with :thread or :ndc
(figure-width (x)
(if (consp x)
(destructuring-bind (&optional min max) x
(format nil "~@[~d~]~@[.~d~]" min max))
"")))
(cond ((getf args :pretty)
(s/ "%<" "%<{pretty}"))
((getf args :nopretty)
(s/ "%<" "%<{nopretty}")))
(cond ((getf args :package)
(s/ "%<" "%<{package}"))
((getf args :nopackage)
(s/ "%<" "%<{nopackage}")))
(cond
;; figure out min/max
((and (setq w1 (getf args :thread))
(setq w2 (getf args :ndc)))
(setq thread-width-fmt (figure-width w1)
ndc-width-fmt (figure-width w2))
(s/ "%t"
(format nil "%; [;~At%:;-;~Ax]"
thread-width-fmt
ndc-width-fmt)))
((setq w1 (getf args :thread))
(s/ "%t" (format nil "%:; [;;];~At"
(figure-width w1))))
((setq w1 (getf args :ndc))
(s/ "%t" (format nil "%:; [;;];~Ax"
(figure-width w1))))
(t (s/ "%t" "")))
pat))))
(defun appender-extra-print-properties (a)
"Return a list of (PROPNAME VALUE) extra display properties to print when
displaying an appender. Some of the properties are included only
conditionally, such as last error or error count"
(with-slots (message-count
error-count
ignored-error-count
filter
last-error
last-ignored-error) a
(append
`((:message-count ,message-count))
(when (plusp error-count) `((:error-count ,error-count)))
(when (plusp ignored-error-count) `((:ignored-error-count ,ignored-error-count)))
(when filter `((:filter ,(aref +log-level-to-keyword+ filter))))
(when last-error `((:last-error ,last-error)))
(when last-ignored-error `((:last-ignored-error ,last-ignored-error))))))
(defun show-logger-settings (logger)
"Print logger settings and its children to *STANDARD-OUTPUT*
Example output:
+ROOT, WARN
|
+-#<CONSOLE-APPENDER>
| with #<PATTERN-LAYOUT>
| :pattern [%P] %c %m%n
| :immediate-flush: nil
| :flush-interval: 1
+-LOG4CL
|
+-SELF (non-additive), DEBUG
| | :config (:SANE, :OWN :TWOLINE :D)
| |
| +-#<CONSOLE-APPENDER 0x123123>,
| | with #<PATTERN-LAYOUT>
| | :pattern [%P] %c %m%n
| | :immediate-flush: nil
| | :flush-interval: 1
| +-#<DAILY-ROLLING-APPENDER 0x12345>
| with #<SIMPLE-LAYOUT 1234>
| :immediate-flush NIL
| :flush-interval: 1
|
+-OTHER, DEBUG
"
(let ((indents '())
(interesting-cache (make-hash-table)))
(labels ((interesting-logger-p (l)
;; return if logger is worth mentioning, its only
;; interesting if its a root logger, is non-additive or
;; has custom log level or appenders
(multiple-value-bind (result present)
(gethash l interesting-cache)
(if present result
(setf (gethash l interesting-cache)
(or (eq l logger)
(and (not (eq l +self-logger+))
(or
(logger-appenders l)
(not (logger-additivity l))
(logger-log-level l)
(and (not (typep l 'source-file-logger))
(some #'interesting-logger-p (logger-children l))))))))))
(print-indent (&optional node-p)
(dolist (elem (reverse indents))
(let* ((lastp (eq elem (car indents)))
(width (car elem))
(nodes-left (cdr elem)))
(loop repeat (1- width)
do (write-char #\Space))
(write-char (if (and lastp node-p)
#\+
;; only continue drawing vertical line down
;; if there are more child nodes to be printed
;; at this level
(if (plusp nodes-left)
#\|
#\Space)))))
;; Each time we print a child node, we decrement
;; number of child nodes left at this level, so that
;; we know when to stop drawing the vertical line linking
;; to more nodes of the same level
(when node-p
(when indents
(assert (plusp (cdar indents)))
(decf (cdar indents)))))
(print-one-logger (l)
(let* ((lvl (logger-log-level l))
(appenders (logger-appenders l))
(additivity (logger-additivity l))
(children
(unless (typep l 'source-file-logger)
(remove-if-not #'interesting-logger-p (logger-children l))))
(cnt-nodes (+ (length appenders)
(length children))))
(print-indent t)
(cond (indents
(format t "-~A" (logger-name l))
(push (cons 2 cnt-nodes) indents))
;; start vertical line at column 0 for root node
(t (format t "~A" (logger-name l))
(push (cons 1 cnt-nodes) indents)))
(unless additivity
(write-string " non-additive"))
(when lvl
(format t ", ~A" (log-level-to-string lvl)))
(terpri)
(when appenders
(loop for num from 1
for a in appenders
do (print-one-appender a num)))
(when (some #'interesting-logger-p children)
(dolist (l children)
(when (interesting-logger-p l)
(print-indent) (terpri)
(print-one-logger l))))
(pop indents)))
(print-properties (obj &optional extra-props)
(let* ((prop-alist (property-alist obj))
(print-list (append
(loop for (initarg slot nil) in prop-alist
collect (list initarg (slot-value obj slot)))
extra-props))
(name-width (loop for prop in print-list maximize
(length (format nil "~s" (first prop)))))
(indent (with-output-to-string (*standard-output*)
(print-indent)))
(*print-pretty* t))
(loop
for (propname value) in print-list
do (pprint-logical-block (*standard-output* nil :per-line-prefix indent)
(pprint-indent :block 0)
(write propname :case :downcase)
(pprint-tab :section-relative 1 (1+ name-width))
(pprint-indent :block 2)
(pprint-newline :fill)
(write value))
do (terpri))))
(print-one-appender (a num)
;; empty line for spacing
(print-indent) (terpri)
;; now the appender node
(print-indent t) (format t "-(~d)-~A" num a)
(unless (appender-enabled-p a)
(write-string " disabled"))
(terpri)
;; indent appender attributes and layout under the appender,
;; don't draw the tree for them
(push (cons 5 0) indents)
(print-layout (slot-value a 'layout))
(print-properties
a (appender-extra-print-properties a))
(pop indents))
(print-layout (layout)
(print-indent)
(format t "with ~A~%" layout)
(push (cons 5 0) indents)
(print-properties layout)
(pop indents)))
(print-one-logger logger)
(let* ((global-stream (resolve-stream *debug-io*))
(appenders (effective-appenders logger)))
(unless (some (lambda (a)
(typecase a
(console-appender t)
((or this-console-appender tricky-console-appender)
(eq (appender-stream a) global-stream))))
appenders)
(format t "~%~@<Warning: No appenders can reach current ~9I~_~<~A: ~:_~A~:>~:>~%"
(list '*debug-io* global-stream)))))
(values)))
;; do default configuration
(let ((done nil))
(defun perform-default-init ()
(unless done
(setq done t)
(clear-logging-configuration)
(log-config :i :sane2 :immediate-flush))))
(perform-default-init)
;;;
;;; Logging configuration quick save / restore
;;;
(defclass configuration-element ()
((logger :initarg :logger :type logger
:initform (error "Required argument ~s missing" :logger)
:reader logger-of)
(level :initarg :level
:type keyword
:initform (error "Required argument ~s missing" :level)
:reader level-of))
(:documentation "Holds logger and its log level"))
(defclass configuration ()
((name :type atom :initarg :name
:initform (error "Required argument ~s missing" :name)
:accessor name-of)
(elements :initarg :elements
:accessor elements-of
:initform (remember-logging-configuration)))
(:documentation "Used to remember log levels for a set of loggers"))
(defun remember-logging-configuration (&optional (logger *root-logger*))
"Utility method to make a list of logging configuration starting from LOGGER.
Returns a list of CONFIGURATION-ELEMENT objects"
(flet ((make-element (logger level)
(make-instance 'configuration-element
:logger logger
:level (if level (aref +log-level-to-keyword+ level) :unset))))
(cons (make-element logger (logger-log-level logger))
(loop for logger in (logger-descendants logger)
for level = (logger-log-level logger)
if level collect (make-element logger level)))))
(defun make-logger-configuration-load-form (logger)
"Different version of loggers load-form, that does not
remember the file name. This allows saved logging configuration
to be restored, even if loggers had moved to a different file,
without overwriting their file with its value when configuration
was saved."
(let ((pkg-start (logger-pkg-idx-start logger))
(pkg-end (logger-pkg-idx-end logger)))
(if (typep logger 'source-file-logger)
;; for source-file-loggers, that represent the entire file we
;; still remember the file
`(%get-logger ',(logger-categories logger)
,(%logger-category-separator logger)
nil nil t
,(logger-file logger)
,(when (plusp pkg-start) (1- pkg-start))
,(when (plusp pkg-end) (1- pkg-end)) t)
;; but not for regular loggers
`(%get-logger ',(logger-categories logger)
,(%logger-category-separator logger)
nil nil t
nil
,(when (plusp pkg-start) (1- pkg-start))
,(when (plusp pkg-end) (1- pkg-end))
,(typep logger 'source-file-logger)))))
(defmethod print-object ((elem configuration-element) stream)
(with-slots (logger level) elem
(if (not *print-readably*)
(print-unreadable-object (elem stream :type t)
(princ (if (%logger-parent logger) (%logger-category logger)
"+ROOT+") stream)
(princ #\Space stream)
(prin1 level stream))
(format stream "#.~S"
`(make-instance 'configuration-element
:logger
,(make-logger-configuration-load-form logger)
:level ,level)))))
(defmethod print-object ((cnf configuration) stream)
(with-slots (name elements) cnf
(if (not *print-readably*)
(print-unreadable-object (cnf stream :type t)