|
36 | 36 |
|
37 | 37 | __author__ = "Vinay Sajip <[email protected]>" |
38 | 38 | __status__ = "beta" |
39 | | -__version__ = "0.4.9.2" |
40 | | -__date__ = "28 February 2004" |
| 39 | +__version__ = "0.4.9.3" |
| 40 | +__date__ = "03 July 2004" |
41 | 41 |
|
42 | 42 | #--------------------------------------------------------------------------- |
43 | 43 | # Miscellaneous module data |
@@ -113,8 +113,12 @@ def getLevelName(level): |
113 | 113 | If the level is one of the predefined levels (CRITICAL, ERROR, WARNING, |
114 | 114 | INFO, DEBUG) then you get the corresponding string. If you have |
115 | 115 | associated levels with names using addLevelName then the name you have |
116 | | - associated with 'level' is returned. Otherwise, the string |
117 | | - "Level %s" % level is returned. |
| 116 | + associated with 'level' is returned. |
| 117 | +
|
| 118 | + If a numeric value corresponding to one of the defined levels is passed |
| 119 | + in, the corresponding string representation is returned. |
| 120 | +
|
| 121 | + Otherwise, the string "Level %s" % level is returned. |
118 | 122 | """ |
119 | 123 | return _levelNames.get(level, ("Level %s" % level)) |
120 | 124 |
|
@@ -968,6 +972,11 @@ def log(self, level, msg, *args, **kwargs): |
968 | 972 |
|
969 | 973 | logger.log(level, "We have a %s", "mysterious problem", exc_info=1) |
970 | 974 | """ |
| 975 | + if type(level) != types.IntType: |
| 976 | + if raiseExceptions: |
| 977 | + raise TypeError, "level must be an integer" |
| 978 | + else: |
| 979 | + return |
971 | 980 | if self.manager.disable >= level: |
972 | 981 | return |
973 | 982 | if self.isEnabledFor(level): |
@@ -1106,17 +1115,54 @@ def __init__(self, level): |
1106 | 1115 |
|
1107 | 1116 | BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s" |
1108 | 1117 |
|
1109 | | -def basicConfig(): |
| 1118 | +def basicConfig(**kwargs): |
1110 | 1119 | """ |
1111 | | - Do basic configuration for the logging system by creating a |
1112 | | - StreamHandler with a default Formatter and adding it to the |
1113 | | - root logger. |
| 1120 | + Do basic configuration for the logging system. |
| 1121 | +
|
| 1122 | + This function does nothing if the root logger already has handlers |
| 1123 | + configured. It is a convenience method intended for use by simple scripts |
| 1124 | + to do one-shot configuration of the logging package. |
| 1125 | +
|
| 1126 | + The default behaviour is to create a StreamHandler which writes to |
| 1127 | + sys.stderr, set a formatter using the BASIC_FORMAT format string, and |
| 1128 | + add the handler to the root logger. |
| 1129 | +
|
| 1130 | + A number of optional keyword arguments may be specified, which can alter |
| 1131 | + the default behaviour. |
| 1132 | +
|
| 1133 | + filename Specifies that a FileHandler be created, using the specified |
| 1134 | + filename, rather than a StreamHandler. |
| 1135 | + filemode Specifies the mode to open the file, if filename is specified |
| 1136 | + (if filemode is unspecified, it defaults to "a"). |
| 1137 | + format Use the specified format string for the handler. |
| 1138 | + datefmt Use the specified date/time format. |
| 1139 | + level Set the root logger level to the specified level. |
| 1140 | + stream Use the specified stream to initialize the StreamHandler. Note |
| 1141 | + that this argument is incompatible with 'filename' - if both |
| 1142 | + are present, 'stream' is ignored. |
| 1143 | +
|
| 1144 | + Note that you could specify a stream created using open(filename, mode) |
| 1145 | + rather than passing the filename and mode in. However, it should be |
| 1146 | + remembered that StreamHandler does not close its stream (since it may be |
| 1147 | + using sys.stdout or sys.stderr), whereas FileHandler closes its stream |
| 1148 | + when the handler is closed. |
1114 | 1149 | """ |
1115 | 1150 | if len(root.handlers) == 0: |
1116 | | - hdlr = StreamHandler() |
1117 | | - fmt = Formatter(BASIC_FORMAT) |
| 1151 | + filename = kwargs.get("filename") |
| 1152 | + if filename: |
| 1153 | + mode = kwargs.get("filemode", "a") |
| 1154 | + hdlr = FileHandler(filename, mode) |
| 1155 | + else: |
| 1156 | + stream = kwargs.get("stream") |
| 1157 | + hdlr = StreamHandler(stream) |
| 1158 | + fs = kwargs.get("format", BASIC_FORMAT) |
| 1159 | + dfs = kwargs.get("datefmt", None) |
| 1160 | + fmt = Formatter(fs, dfs) |
1118 | 1161 | hdlr.setFormatter(fmt) |
1119 | 1162 | root.addHandler(hdlr) |
| 1163 | + level = kwargs.get("level") |
| 1164 | + if level: |
| 1165 | + root.setLevel(level) |
1120 | 1166 |
|
1121 | 1167 | #--------------------------------------------------------------------------- |
1122 | 1168 | # Utility functions at module level. |
|
0 commit comments