summaryrefslogtreecommitdiffstats
path: root/Doc/lib/liblogging.tex
blob: eeacd5e7fdfb6a4558af680978e21970114e1918 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
\section{\module{logging} ---
         Logging facility for Python}

\declaremodule{standard}{logging}	% standard library, in Python

% These apply to all modules, and may be given more than once:

\moduleauthor{Vinay Sajip}{vinay_sajip@red-dove.com}
\sectionauthor{Skip Montanaro}{skip@pobox.com}

\modulesynopsis{Logging module for Python based on PEP 282.}


There is a need for a standard logging system in Python, as documented in
{}\pep{282} and enthusiastically endorsed by the Guido van Rossum in the
{}\citetitle[http://www.python.org/doc/essays/pepparade.html]{Parade of the
PEPs}.  By a happy coincidence, the package described here was already in
development and fairly close in intent and design to the description in the
aforementioned PEP, borrowing as it did heavily from JSR-47 (now JDK 1.4's
java.util.logging package) and
{}\ulink{log4j}{http://jakarta.apache.org/log4j/}. This section page
describes it in more detail.

This package owes its greatest debt to Apache
{}\ulink{log4j}{http://jakarta.apache.org/log4j/}. Due notice was also taken
of log4j's comprehensive
{}\ulink{critique}{http://jakarta.apache.org/log4j/docs/critique.html} of
JSR-47.  This package bears a close resemblance to log4j, but is not a close
translation (as, for example, {}\ulink{log4p}{http://log4p.sourceforge.net/}
appears to be).



\subsection{Functions}

The \module{logging} module defines the following functions:


\begin{funcdesc}{getLevelName}{level}

Return the textual representation of logging level \var{level}.

If the level is one of the predefined levels (\var{CRITICAL}, \var{ERROR},
{}\var{WARN}, \var{INFO}, \var{DEBUG}) then you get the corresponding
string. If you have associated levels with names using
{}\function{addLevelName} then the name you have associated with \var{level}
is returned. Otherwise, the string "Level \%s" \% level is returned.

\end{funcdesc}


\begin{funcdesc}{addLevelName}{level, levelName}

Associate \var{levelName} with \var{level}.  This is used when converting
levels to text during message formatting.

\end{funcdesc}


\begin{funcdesc}{setLoggerClass}{klass}

Set the class to be used when instantiating a logger. The class should
define \code{__init__()} such that only a name argument is required, and the
{}\code{__init__()} should call Logger.__init__()

\end{funcdesc}


\begin{funcdesc}{basicConfig}{}

Do basic configuration for the logging system by creating a
{}\class{StreamHandler} with a default {}\class{Formatter} and adding it to
the root logger.

\end{funcdesc}


\begin{funcdesc}{getLogger}{\optional{name=None}}

Return a logger with the specified name, creating it if necessary.  If no
name is specified, return the root logger.

\end{funcdesc}


\begin{funcdesc}{critical}{msg, *args, **kwargs}

Log a message with severity \code{CRITICAL} on the root logger.

\end{funcdesc}


\begin{funcdesc}{error}{msg, *args, **kwargs}

Log a message with severity \var{ERROR} on the root logger.

\end{funcdesc}


\begin{funcdesc}{exception}{msg, *args}

Log a message with severity \code{ERROR} on the root logger,
with exception information.

\end{funcdesc}

\begin{funcdesc}{warn}{msg, *args, **kwargs}

Log a message with severity \code{WARN} on the root logger.

\end{funcdesc}


\begin{funcdesc}{info}{msg, *args, **kwargs}

Log a message with severity \code{INFO} on the root logger.

\end{funcdesc}


\begin{funcdesc}{debug}{msg, *args, **kwargs}

Log a message with severity \code{DEBUG} on the root logger.

\end{funcdesc}


\begin{funcdesc}{disable}{level}

Disable all logging calls less severe than \code{level}.

\end{funcdesc}


\begin{funcdesc}{shutdown}{}

Perform any cleanup actions in the logging system (e.g. flushing buffers).
Should be called at application exit.

\end{funcdesc}



\subsection{Data}

The \module{logging} module defines the following data objects:

\begin{datadesc}{root}

The default logger.

\end{datadesc}


\begin{datadesc}{BASIC_FORMAT}

The default message format.

\end{datadesc}


\begin{datadesc}{CRITICAL}

The \code{CRITICAL} level.

\end{datadesc}


\begin{datadesc}{FATAL}

The \code {FATAL} level.  A synonym for \code{CRITICAL}.

\end{datadesc}


\begin{datadesc}{WARN}

The \code {WARN} level.

\end{datadesc}


\begin{datadesc}{INFO}

The \code{INFO} level.

\end{datadesc}


\begin{datadesc}{DEBUG}

The \code{DEBUG} level.

\end{datadesc}


\begin{datadesc}{NOTSET}

The \code{NOTSET} level.

\end{datadesc}


\begin{datadesc}{raiseExceptions}

Indicates whether exceptions during handling should be propagated.  True by
default.

\end{datadesc}



\subsection{Classes}

The \module{logging} module defines the following classes:


\begin{classdesc}{Formatter}{\optional{fmt=None\optional{, datefmt=None}}}

Formatters need to know how a LogRecord is constructed. They are responsible
for converting a LogRecord to (usually) a string which can be interpreted by
either a human or an external system. The base Formatter allows a formatting
string to be specified. If none is supplied, the default value of
\code{"\%s(message)\e n"} is used.
                                                                               
The Formatter can be initialized with a format string which makes use of
knowledge of the LogRecord attributes - e.g. the default value mentioned
above makes use of the fact that the user's message and arguments are pre-
formatted into a LogRecord's message attribute. Currently, the useful
attributes in a LogRecord are described by:
                                                                               
\begin{description}

\item[\%(name)s]{Name of the logger (logging channel)}

\item[\%(levelno)s]{Numeric logging level for the message (DEBUG, INFO,        
WARN, ERROR, CRITICAL)}

\item[\%(levelname)s]{Text logging level for the message ("DEBUG", "INFO",       
"WARN", "ERROR", "CRITICAL")}

\item[\%(pathname)s]{Full pathname of the source file where the logging
call was issued (if available)}

\item[\%(filename)s]{Filename portion of pathname}

\item[\%(module)s]{Module (name portion of filename)}

\item[\%(lineno)d]{Source line number where the logging call was issued
(if available)}

\item[\%(created)f]{Time when the LogRecord was created (time.time()
return value)}

\item[\%(asctime)s]{Textual time when the LogRecord was created}

\item[\%(msecs)d]{Millisecond portion of the creation time}

\item[\%(relativeCreated)d]{Time in milliseconds when the LogRecord was
created, relative to the time the logging module was loaded (typically at
application startup time)}

\item[\%(thread)d]{Thread ID (if available)}

\item[\%(message)s]{The result of record.getMessage(), computed just as the
record is emitted}

\end{description}

\end{classdesc}

\begin{methoddesc}{format}{self, record}

The record's attribute dictionary is used as the operand to a string
formatting operation which yields the returned string.  Before formatting
the dictionary, a couple of preparatory steps are carried out. The message
attribute of the record is computed using \code{LogRecord.getMessage()}. If
the formatting string contains "\%(asctime)", \code{formatTime()} is called
to format the event time.  If there is exception information, it is
formatted using \code{formatException()} and appended to the message.

\end{methoddesc}

\begin{methoddesc}{formatException}{self, ei}

Format the specified exception information as a string. This default
implementation just uses \code{traceback.print_exception()}

\end{methoddesc}

\begin{methoddesc}{formatTime}{self, record\optional{, datefmt=None}}

This method should be called from \code{format()} by a formatter which wants
to make use of a formatted time. This method can be overridden in formatters
to provide for any specific requirement, but the basic behaviour is as
follows: if datefmt (a string) is specified, it is used with time.strftime()
to format the creation time of the record. Otherwise, the ISO8601 format is
used. The resulting string is returned. This function uses a
user-configurable function to convert the creation time to a tuple. By
default, \code{time.localtime()} is used; to change this for a particular
formatter instance, set the 'converter' attribute to a function with the
same signature as \code{time.localtime()} or \code{time.gmtime()}. To change
it for all formatters, for example if you want all logging times to be shown
in GMT, set the 'converter' attribute in the \class{Formatter} class.

\end{methoddesc}


\begin{classdesc}{Filterer}{}

A base class for loggers and handlers which allows them to share common
code.

\end{classdesc}

\begin{methoddesc}{addFilter}{filter}

Add the specified filter to this handler.

\end{methoddesc}

\begin{methoddesc}{filter}{self, record}

Determine if a record is loggable by consulting all the filters. The default
is to allow the record to be logged; any filter can veto this and the record
is then dropped. Returns a boolean value.

\end{methoddesc}

\begin{methoddesc}{removeFilter}{filter}

Remove the specified filter from this handler.                           

\end{methoddesc}


\begin{classdesc}{BufferingFormatter}{\optional{linefmt=None}}

A formatter suitable for formatting a number of records.  Optionally specify
a formatter which will be used to format each individual record.

\end{classdesc}


\begin{methoddesc}{format}{records}

Format the specified records and return the result as a string.

\end{methoddesc}

\begin{methoddesc}{formatFooter}{records}

Return the footer string for the specified records.

\end{methoddesc}

\begin{methoddesc}{formatHeader}{records}

Return the header string for the specified records.

\end{methoddesc}

\begin{classdesc}{BufferingHandler}{capacity}

A handler class which buffers logging records in memory. Whenever each
record is added to the buffer, a check is made to see if the buffer should
be flushed. If it should, then \code{flush()} is expected to do the needful.
The handler is initialized with the buffer size.

\end{classdesc}

\begin{methoddesc}{emit}{record}

Append the record. If \code{shouldFlush()} tells us to, call \code{flush()}
to process the buffer.

\end{methoddesc}

\begin{methoddesc}{flush}{}

Override to implement custom flushing behaviour. This version just zaps the
buffer to empty.

\end{methoddesc}

\begin{methoddesc}{shouldFlush}{record}

Returns true if the buffer is up to capacity. This method can be overridden
to implement custom flushing strategies.

\end{methoddesc}


\begin{classdesc}{DatagramHandler}{host,port}

A handler class which writes logging records, in pickle format, to a
datagram socket. Note that the very simple wire protocol used means that
packet sizes are expected to be encodable within 16 bits (i.e. < 32767
bytes).

Initializes the handler with a specific \code{host} and \code{port}.

\end{classdesc}

\begin{methoddesc}{makeSocket}{}

The factory method of SocketHandler is here overridden to create a UDP
socket (SOCK_DGRAM).

\end{methoddesc}

\begin{methoddesc}{send}{s}

Send a pickled string to a socket. This function allows for partial sends
which can happen when the network is busy.

\end{methoddesc}

\begin{classdesc}{FileHandler}{filename\optional{, mode='a+'}}

A handler class which writes formatted logging records to disk files.  The
specified file is opened and used as the stream for logging.  By default,
the file grows indefinitely. You can call \code{setRollover()} to allow the
file to rollover at a predetermined size.

\end{classdesc}

\begin{methoddesc}{close}{}

Closes the stream.

\end{methoddesc}

\begin{methoddesc}{doRollover}{}

Do a rollover, as described in \code{setRollover()}.

\end{methoddesc}

\begin{methoddesc}{emit}{record}

Output the record to the file, catering for rollover as described
in \code{setRollover()}.

\end{methoddesc}

\begin{methoddesc}{setRollover}{maxBytes, backupCount}

Set the rollover parameters so that rollover occurs whenever the current log
file is nearly \var{maxBytes} in length. If \var{backupCount} is >= 1, the
system will successively create new files with the same pathname as the base
file, but with extensions ".1", ".2" etc. appended to it. For example, with
a \var{backupCount} of 5 and a base file name of "app.log", you would get
"app.log", "app.log.1", "app.log.2", ... through to "app.log.5". When the
last file reaches its size limit, the logging reverts to "app.log" which is
truncated xto zero length. If maxBytes is zero, rollover never occurs.

\end{methoddesc}

\begin{classdesc}{Filter}{\optional{name=''}}

The base filter class. \class{Logger} and \class{Handler} instances can
optionally use \class{Filter} instances to filter records as desired. The
base filter class only allows events which are below a certain point in the
logger hierarchy. For example, a filter initialized with "A.B" will allow
events logged by loggers "A.B", "A.B.C", "A.B.C.D", "A.B.D" etc. but not
"A.BB", "B.A.B" etc. If initialized with the empty string, all events are
passed.

The instance is initialized with the name of the logger which, together with
its children, will have its events allowed through the filter. If no name is
specified, allow every event.

\end{classdesc}

\begin{methoddesc}{filter}{record}

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If
deemed appropriate, the record may be modified in-place.

\end{methoddesc}


\begin{classdesc}{HTTPHandler}{host, url\optional{, method='GET'}}

A class which sends records to a Web server, using either GET or POST
semantics.  The instance is initialized with the \var{host}, the request
\var{url}, and the \var{method} ("GET" or "POST")

\end{classdesc}

\begin{methoddesc}{emit}{record}

Send the \var{record} to the Web server as an URL-encoded dictionary

\end{methoddesc}

\begin{classdesc}{Handler}{\optional{level=0}}

The base handler class. Acts as a placeholder which defines the Handler
interface. \class{Handler} instances can optionally use \class{Formatter}
instances to format records as desired. By default, no formatter is
specified; in this case, the 'raw' message as determined by record.message
is logged.  Initializes the instance - basically setting the formatter to
None and the filter list to empty.

XXX - what does the level do?

\end{classdesc}

\begin{methoddesc}{acquire}{}

Acquire the I/O thread lock.

\end{methoddesc}

\begin{methoddesc}{close}{}

Tidy up any resources used by the handler. This version does nothing and is
intended to be implemented by subclasses.

\end{methoddesc}

\begin{methoddesc}{createLock}{}

Acquire a thread lock for serializing access to the underlying I/O.

\end{methoddesc}

\begin{methoddesc}{emit}{record}

Do whatever it takes to actually log the specified logging record.  This
version is intended to be implemented by subclasses and so raises a
\exception{NotImplementedError}.

\end{methoddesc}

\begin{methoddesc}{flush}{}

Ensure all logging output has been flushed. This version does nothing and is
intended to be implemented by subclasses.

\end{methoddesc}

\begin{methoddesc}{format}{record}

Do formatting for a \var{record} - if a formatter is set, use it.
Otherwise, use the default formatter for the module.

\end{methoddesc}

\begin{methoddesc}{handle}{record}

Conditionally emit the specified logging \var{record}, depending on filters
which may have been added to the handler. Wrap the actual emission of the
record with acquisition/release of the I/O thread lock.

\end{methoddesc}

\begin{methoddesc}{handleError}{}

This method should be called from handlers when an exception is encountered
during an \code{emit()} call. By default it does nothing, because by default
{}\var{raiseExceptions} is false, which means that exceptions get silently
ignored. This is what is mostly wanted for a logging system - most users
will not care about errors in the logging system, they are more interested
in application errors.  You could, however, replace this with a custom
handler if you wish.

XXX looks to me like raiseExceptions defaults to 1.

\end{methoddesc}

\begin{methoddesc}{release}{}

Release the I/O thread lock.

\end{methoddesc}

\begin{methoddesc}{setFormatter}{formatter}

Set the \var{formatter} for this handler.

\end{methoddesc}

\begin{methoddesc}{setLevel}{level}

Set the logging \var{level} of this handler.

\end{methoddesc}

\begin{classdesc}{LogRecord}{name,lvl,pathname,lineno,msg,args,exc_info}

\class{LogRecord} instances are created every time something is logged. They
contain all the information pertinent to the event being logged. The main
information passed in is in msg and args, which are combined using
\code{str(msg) \% args} to create the message field of the record. The
record also includes information such as when the record was created, the
source line where the logging call was made, and any exception information
to be logged.

\end{classdesc}

\begin{methoddesc}{getMessage}{}

Return the message for this LogRecord, merging any user-supplied arguments
with the message.

\end{methoddesc}

\begin{classdesc}{Logger}{name\optional{, level=0}}

Instances of the \class{Logger} class represent a single logging channel. A
"logging channel" indicates an area of an application. Exactly how an "area"
is defined is up to the application developer. Since an application can have
any number of areas, logging channels are identified by a unique
string. Application areas can be nested (e.g. an area of "input processing"
might include sub-areas "read CSV files", "read XLS files" and "read
Gnumeric files"). To cater for this natural nesting, channel names are
organized into a namespace hierarchy where levels are separated by periods,
much like the Java or Python package namespace. So in the instance given
above, channel names might be "input" for the upper level, and "input.csv",
"input.xls" and "input.gnu" for the sub-levels.  There is no arbitrary limit
to the depth of nesting.

The logger is initialized with a \var{name} and an optional \var{level}.

\end{classdesc}

\begin{methoddesc}{_log}{lvl, msg, args\optional{, exc_info=None}}

Low-level logging routine which creates a \class{LogRecord} and then calls
all the handlers of this logger to handle the record.

\end{methoddesc}

\begin{methoddesc}{addHandler}{hdlr}

Add the specified handler to this logger.

\end{methoddesc}

\begin{methoddesc}{callHandlers}{record}

Loop through all handlers for this logger and its parents in the logger
hierarchy. If no handler was found, output a one-off error message to
sys.stderr. Stop searching up the hierarchy whenever a logger with the
"propagate" attribute set to zero is found - that will be the last logger
whose handlers are called.

\end{methoddesc}

\begin{methoddesc}{critical}{msg, *args, **kwargs}

Log \code{msg \% args} with severity \code{CRITICAL}. To pass exception
information, use the keyword argument \var{exc_info} with a true value,
e.g., \code{logger.critical("Houston, we have a \%s", "major disaster",
exc_info=1)}.

\end{methoddesc}

\begin{methoddesc}{fatal}{msg, *args, **kwargs}

Synonym for \method{critical}.

\end{methoddesc}

\begin{methoddesc}{debug}{msg, *args, **kwargs}

Log \code{msg \% args} with severity \code{DEBUG}. To pass exception
information, use the keyword argument exc_info with a true value, e.g.,
\code{logger.debug("Houston, we have a \%s", "thorny problem", exc_info=1)}.

\end{methoddesc}

\begin{methoddesc}{error}{msg, *args, **kwargs}

Log \code{msg \% args} with severity \code{ERROR}. To pass exception
information, use the keyword argument exc_info with a true value, e.g.,
\code{logger.error("Houston, we have a \%s", "major problem", exc_info=1)}

\end{methoddesc}

\begin{methoddesc}{exception}{msg, *args}

Convenience method for logging an \code{ERROR} with exception information.

\end{methoddesc}


\begin{methoddesc}{findCaller}{}

Find the stack frame of the caller so that we can note the source file name
and line number.

\end{methoddesc}

\begin{methoddesc}{getEffectiveLevel}{}

Loop through this logger and its parents in the logger hierarchy, looking
for a non-zero logging level. Return the first one found.

\end{methoddesc}

\begin{methoddesc}{handle}{record}

Call the handlers for the specified \var{record}. This method is used for
unpickled records received from a socket, as well as those created
locally. Logger-level filtering is applied.

\end{methoddesc}

\begin{methoddesc}{info}{msg, *args, **kwargs}

Log \code{msg \% args} with severity \code{INFO}. To pass exception
information, use the keyword argument exc_info with a true value, e.g.,
\code{logger.info("Houston, we have a \%s", "interesting problem",
exc_info=1)}

\end{methoddesc}

\begin{methoddesc}{isEnabledFor}{lvl}

Is this logger enabled for level \var{lvl}?

\end{methoddesc}

\begin{methoddesc}{log}{lvl, msg, *args, **kwargs}

Log \code{msg \% args} with the severity \var{lvl}. To pass exception
information, use the keyword argument \var{exc_info} with a true value,
e.g., \code{logger.log(lvl, "We have a \%s", "mysterious problem",
exc_info=1)}

\end{methoddesc}

\begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info}

A factory method which can be overridden in subclasses to create specialized
\code{LogRecord} instances.

\end{methoddesc}

\begin{methoddesc}{removeHandler}{hdlr}

Remove the specified handler from this logger.

\end{methoddesc}

\begin{methoddesc}{setLevel}{level}

Set the logging \var{level} of this logger.

\end{methoddesc}

\begin{methoddesc}{warn}{msg, *args, **kwargs}

Log \code{msg \% args} with severity \code{WARN}. To pass exception
information, use the keyword argument exc_info with a true value, e.g.,
\code{logger.warn("Houston, we have a \%s", "bit of a problem", exc_info=1)}

\end{methoddesc}


\begin{classdesc}{Manager}{root}

There is (under normal circumstances) just one \code{Manager} instance,
which holds the hierarchy of loggers.

The manager is initialized with the \var{root} node of the logger hierarchy.

\end{classdesc}

\begin{methoddesc}{_fixupChildren}{ph, logger}

Ensure that children of the placeholder \var{ph} are connected to the
specified \code{logger}.

\end{methoddesc}

\begin{methoddesc}{_fixupParents}{logger}

Ensure that there are either loggers or placeholders all the way from the
specified \var{logger} to the root of the logger hierarchy.

\end{methoddesc}

\begin{methoddesc}{getLogger}{name}

Get a logger with the specified \var{name} (channel name), creating it if it
doesn't yet exist. If a PlaceHolder existed for the specified name (i.e. the
logger didn't exist but a child of it did), replace it with the created
logger and fix up the parent/child references which pointed to the
placeholder to now point to the logger.

\end{methoddesc}

\begin{classdesc}{MemoryHandler}{capacity\optional{,
flushLevel=40\optional{, target=None}}} 

A handler class which buffers logging records in memory, periodically
flushing them to a target handler. Flushing occurs whenever the buffer is
full, or when an event of a certain severity or greater is seen.

The handler is initialized with the buffer size (\var{capacity}), the level
at which flushing should occur (\var{flushLevel}) and an optional
{}\var{target}.  Note that without a target being set either here or via
\code{setTarget()}, a \class{MemoryHandler} is no use to anyone!

\end{classdesc}

\begin{methoddesc}{close}{}

Flush, set the target to None and lose the buffer.

\end{methoddesc}

\begin{methoddesc}{flush}{}

For a \class{MemoryHandler}, flushing means just sending the buffered
records to the target, if there is one. Override if you want different
behavior.

\end{methoddesc}

\begin{methoddesc}{setTarget}{target}

Set the \var{target} handler for this handler.

\end{methoddesc}

\begin{methoddesc}{shouldFlush}{record}

Check for buffer full or a \var{record} at the flushLevel or higher.

\end{methoddesc}


\begin{classdesc}{NTEventLogHandler}{appname\optional{,
dllname=None\optional{, logtype='Application'}}} 

A handler class which sends events to the NT Event Log. Adds a registry
entry for the specified application name.  If no \var{dllname} is provided,
\code{win32service.pyd} (which contains some basic message placeholders) is
used.  Note that use of these placeholders will make your event logs big, as
the entire message source is held in the log.  If you want slimmer logs, you
have to pass in the name of your own DLL which contains the message
definitions you want to use in the event log.

XXX what is \var{logtype}?

\end{classdesc}

\begin{methoddesc}{close}{}

You can remove the application name from the registry as a source of event
log entries. However, if you do this, you will not be able to see the events
as you intended in the Event Log Viewer - it needs to be able to access the
registry to get the DLL name.

\end{methoddesc}

\begin{methoddesc}{emit}{record}

Determine the message ID, event category and event type. Then log the
\var{record} in the NT event log.

\end{methoddesc}

\begin{methoddesc}{getEventCategory}{record}

Return the event category for the \var{record}.  Override this if you want
to specify your own categories. This version returns 0.

\end{methoddesc}

\begin{methoddesc}{getEventType}{record}

Return the event type for the \var{record}. Override this if you want to
specify your own types. This version does a mapping using the handler's
typemap attribute, which is set up in the constructor to a dictionary which
contains mappings for \var{DEBUG}, \var{INFO}, \var{WARN}, \var{ERROR} and
{}\var{CRITICAL}. If you are using your own levels you will either need to
override this method or place a suitable dictionary in the handler's typemap
attribute.

\end{methoddesc}

\begin{methoddesc}{getMessageID}{record}

Return the message ID for the event \var{record}. If you are using your own
messages, you could do this by having the msg passed to the logger being an
ID rather than a formatting string. Then, in here, you could use a
dictionary lookup to get the message ID. This version returns 1, which is
the base message ID in \code{win32service.pyd}.

\end{methoddesc}


\begin{classdesc}PlaceHolder{logger}

\class{PlaceHolder} instances are used in the \class{Manager} logger
hierarchy to take the place of nodes for which no loggers have been defined

Initialize with the specified \var{logger} being a child of this
\class{PlaceHolder}.

\end{classdesc}

\begin{methoddesc}{append}{logger}

Add the specified \var{logger} as a child of this placeholder.

\end{methoddesc}

\begin{classdesc}RootLogger{level}

A root logger is not that different to any other logger, except that it must
have a logging \var{level} and there is only one instance of it in the
hierarchy.

\end{classdesc}


\begin{classdesc}{SMTPHandler}{mailhost, fromaddr, toaddr, subject}

A handler class which sends an SMTP email for each logging event.

The instance is initialized with the from (\var{fromaddr}) and to
(\var{toaddr}) addresses and \var{subject} line of the email. To specify a
non-standard SMTP port, use the (host, port) tuple format for the
\var{mailhost} argument.

\end{classdesc}


\begin{methoddesc}{emit}{record}

Format the \var{record} and send it to the specified addressees.

\end{methoddesc}

\begin{methoddesc}{getSubject}{record}

If you want to specify a subject line which is \var{record}-dependent,
override this method.

\end{methoddesc}

\begin{classdesc}{SocketHandler}{host, port}

A handler class which writes pickled logging records to a streaming
socket. The socket is kept open across logging calls.  If the peer resets
it, an attempt is made to reconnect on the next call.  Note that the very
simple wire protocol used means that packet sizes are expected to be
encodable within 16 bits (i.e. < 32767 bytes).

The handler is initialized with a specific \var{host} address and
{}\var{port}.  The attribute \var{closeOnError} is set to 1 - which means
that if a socket error occurs, the socket is silently closed and then
reopened on the next logging call.

\end{classdesc}

\begin{methoddesc}{close}{}

Closes the socket.

\end{methoddesc}

\begin{methoddesc}{emit}{record}

Pickles the \var{record} and writes it to the socket in binary format.  If
there is an error with the socket, silently drop the packet.  If there was a
problem with the socket, re-establishes the socket.

\end{methoddesc}

\begin{methoddesc}{handleError}{}

An error has occurred during logging.  Most likely cause - connection lost.
Close the socket so that we can retry on the next event.

\end{methoddesc}

\begin{methoddesc}{makePickle}{record}

Pickles the \var{record} in binary format with a length prefix, and returns
it ready for transmission across the socket.

\end{methoddesc}

\begin{methoddesc}{makeSocket}{}

A factory method which allows subclasses to define the precise type of
socket they want.

\end{methoddesc}

\begin{methoddesc}{send}{s}

Send a pickled string (\var{s}) to the socket.  This function allows for
partial sends which can happen when the network is busy.

\end{methoddesc}

\begin{classdesc}{StreamHandler}{\optional{strm=None}}

A handler class which writes logging records, appropriately formatted, to a
stream.  Note that this class does not close the stream, as \var{sys.stdout}
or \var{sys.stderr} may be used.

If \var{strm} is not specified, \var{sys.stderr} is used.

\end{classdesc}

\begin{methoddesc}{emit}{record}

If a formatter is specified, it is used to format the \var{record}.  The
record is then written to the stream with a trailing newline (N.B. this may
be removed depending on feedback).  If exception information is present, it
is formatted using \var{traceback.print_exception} and appended to the
stream.

\end{methoddesc}

\begin{methoddesc}{flush}{}

Flushes the stream.

\end{methoddesc}

\begin{classdesc}{SysLogHandler}{\optional{address=('localhost',
514)\optional{, facility=1}}} 

A handler class which sends formatted logging records to a syslog
server. Based on Sam Rushing's
\ulink{http://www.nightmare.com/squirl/python-ext/misc/syslog.py}{syslog
module}.  Contributed by Nicolas Untz (after which minor refactoring changes
have been made).

If \var{address} is specified as a string, UNIX socket is used.  If
\var{facility} is not specified, \code{LOG_USER} is used.

\end{classdesc}

\begin{methoddesc}{close}{}

Closes the socket.

\end{methoddesc}

\begin{methoddesc}{emit}{record}

The \var{record} is formatted, and then sent to the syslog server. If
exception information is present, it is not sent to the server.

\end{methoddesc}

\begin{methoddesc}{encodePriority}{facility, priority}

Encode the \var{facility} and \var{priority}. You can pass in strings or
integers - if strings are passed, the \var{facility_names} and
\var{priority_names} mapping dictionaries are used to convert them to
integers.

\end{methoddesc}


\subsection{Examples \label{logging-example}}

Using the package doesn't get much simpler. It is packaged as a Python
package.  You just need to \code{import logging} and you're ready to
go. Minimal example:

\begin{verbatim}
# -- app.py --
import logging

logging.info("Starting...")
logging.warn("Nothing to do!")
logging.info("Done...")
\end{verbatim}

When you run \code{app.py}, the results are:

\begin{verbatim}
2002-03-15 01:09:10,440 root                INFO  - Starting...
2002-03-15 01:09:10,440 root                WARN  - Nothing to do!
2002-03-15 01:09:10,440 root                INFO  - Done...
\end{verbatim}



Here's a
slightly more involved example; if you've just looked at \pep{282} you will
probably get a feeling of deją vu. (This is intentional.)

\begin{verbatim}
# -- mymodule.py --
import logging
log = logging.getLogger("MyModule")

def doIt():
    log.debug("doin' stuff")
    #do stuff...but suppose an error occurs?
    raise TypeError, "bogus type error for testing"
\end{verbatim}

\begin{verbatim}
# -- myapp.py --
import logging, mymodule

logging.basicConfig() # basic configuration - console output

log = logging.getLogger("MyApp")

log.info("start my app")
try:
    mymodule.doIt()
except Exception, e:
    log.exception("There was a problem doin' stuff.")
log.info("end my app")
\end{verbatim}

When you run \code{myapp.py}, the results are:

\begin{verbatim}
2002-03-14 23:40:49,299 MyApp               INFO  - start my app
2002-03-14 23:40:49,299 MyModule            DEBUG - doin' stuff
2002-03-14 23:40:49,299 MyApp               ERROR - There was a problem doin' stuff.
Traceback (innermost last):
  File "myapp.py", line 9, in ?
    mymodule.doIt()
  File "mymodule.py", line 7, in doIt
    raise TypeError, "bogus type error for testing"
TypeError: bogus type error for testing
2002-03-14 23:40:49,409 MyApp               INFO  - end my app
\end{verbatim}