Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 779e0c9

Browse files
committed
Changed basicConfig() to add keyword arguments. Changes are backward-compatible.
Added error checking to log() to check that level is an integer, and raise a TypeError if not (as long as raiseExceptions is set). Minor documentation corrections.
1 parent a13c60b commit 779e0c9

1 file changed

Lines changed: 56 additions & 10 deletions

File tree

Lib/logging/__init__.py

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
__author__ = "Vinay Sajip <[email protected]>"
3838
__status__ = "beta"
39-
__version__ = "0.4.9.2"
40-
__date__ = "28 February 2004"
39+
__version__ = "0.4.9.3"
40+
__date__ = "03 July 2004"
4141

4242
#---------------------------------------------------------------------------
4343
# Miscellaneous module data
@@ -113,8 +113,12 @@ def getLevelName(level):
113113
If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
114114
INFO, DEBUG) then you get the corresponding string. If you have
115115
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.
118122
"""
119123
return _levelNames.get(level, ("Level %s" % level))
120124

@@ -968,6 +972,11 @@ def log(self, level, msg, *args, **kwargs):
968972
969973
logger.log(level, "We have a %s", "mysterious problem", exc_info=1)
970974
"""
975+
if type(level) != types.IntType:
976+
if raiseExceptions:
977+
raise TypeError, "level must be an integer"
978+
else:
979+
return
971980
if self.manager.disable >= level:
972981
return
973982
if self.isEnabledFor(level):
@@ -1106,17 +1115,54 @@ def __init__(self, level):
11061115

11071116
BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"
11081117

1109-
def basicConfig():
1118+
def basicConfig(**kwargs):
11101119
"""
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.
11141149
"""
11151150
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)
11181161
hdlr.setFormatter(fmt)
11191162
root.addHandler(hdlr)
1163+
level = kwargs.get("level")
1164+
if level:
1165+
root.setLevel(level)
11201166

11211167
#---------------------------------------------------------------------------
11221168
# Utility functions at module level.

0 commit comments

Comments
 (0)