Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
989b69a5
Commit
989b69a5
authored
Jan 16, 2006
by
Vinay Sajip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring for fileConfig. Contributed by Shane Hathaway.
parent
fe03bee6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
142 additions
and
125 deletions
+142
-125
Lib/logging/config.py
Lib/logging/config.py
+142
-125
No files found.
Lib/logging/config.py
View file @
989b69a5
...
...
@@ -72,135 +72,147 @@ def fileConfig(fname, defaults=None):
cp
.
readfp
(
fname
)
else
:
cp
.
read
(
fname
)
#first, do the formatters...
flist
=
cp
.
get
(
"formatters"
,
"keys"
)
if
len
(
flist
):
flist
=
string
.
split
(
flist
,
","
)
formatters
=
{}
for
form
in
flist
:
sectname
=
"formatter_%s"
%
form
opts
=
cp
.
options
(
sectname
)
if
"format"
in
opts
:
fs
=
cp
.
get
(
sectname
,
"format"
,
1
)
else
:
fs
=
None
if
"datefmt"
in
opts
:
dfs
=
cp
.
get
(
sectname
,
"datefmt"
,
1
)
else
:
dfs
=
None
f
=
logging
.
Formatter
(
fs
,
dfs
)
formatters
[
form
]
=
f
#next, do the handlers...
#critical section...
formatters
=
_create_formatters
(
cp
)
# critical section
logging
.
_acquireLock
()
try
:
try
:
#first, lose the existing handlers...
logging
.
_handlers
.
clear
()
#now set up the new ones...
hlist
=
cp
.
get
(
"handlers"
,
"keys"
)
if
len
(
hlist
):
hlist
=
string
.
split
(
hlist
,
","
)
handlers
=
{}
fixups
=
[]
#for inter-handler references
for
hand
in
hlist
:
try
:
sectname
=
"handler_%s"
%
hand
klass
=
cp
.
get
(
sectname
,
"class"
)
opts
=
cp
.
options
(
sectname
)
if
"formatter"
in
opts
:
fmt
=
cp
.
get
(
sectname
,
"formatter"
)
else
:
fmt
=
""
klass
=
eval
(
klass
,
vars
(
logging
))
args
=
cp
.
get
(
sectname
,
"args"
)
args
=
eval
(
args
,
vars
(
logging
))
h
=
apply
(
klass
,
args
)
if
"level"
in
opts
:
level
=
cp
.
get
(
sectname
,
"level"
)
h
.
setLevel
(
logging
.
_levelNames
[
level
])
if
len
(
fmt
):
h
.
setFormatter
(
formatters
[
fmt
])
#temporary hack for FileHandler and MemoryHandler.
if
klass
==
logging
.
handlers
.
MemoryHandler
:
if
"target"
in
opts
:
target
=
cp
.
get
(
sectname
,
"target"
)
else
:
target
=
""
if
len
(
target
):
#the target handler may not be loaded yet, so keep for later...
fixups
.
append
((
h
,
target
))
handlers
[
hand
]
=
h
except
:
#if an error occurs when instantiating a handler, too bad
pass
#this could happen e.g. because of lack of privileges
#now all handlers are loaded, fixup inter-handler references...
for
fixup
in
fixups
:
h
=
fixup
[
0
]
t
=
fixup
[
1
]
h
.
setTarget
(
handlers
[
t
])
#at last, the loggers...first the root...
llist
=
cp
.
get
(
"loggers"
,
"keys"
)
llist
=
string
.
split
(
llist
,
","
)
llist
.
remove
(
"root"
)
sectname
=
"logger_root"
root
=
logging
.
root
log
=
root
opts
=
cp
.
options
(
sectname
)
if
"level"
in
opts
:
level
=
cp
.
get
(
sectname
,
"level"
)
log
.
setLevel
(
logging
.
_levelNames
[
level
])
for
h
in
root
.
handlers
[:]:
root
.
removeHandler
(
h
)
hlist
=
cp
.
get
(
sectname
,
"handlers"
)
if
len
(
hlist
):
hlist
=
string
.
split
(
hlist
,
","
)
for
hand
in
hlist
:
log
.
addHandler
(
handlers
[
hand
])
#and now the others...
#we don't want to lose the existing loggers,
#since other threads may have pointers to them.
#existing is set to contain all existing loggers,
#and as we go through the new configuration we
#remove any which are configured. At the end,
#what's left in existing is the set of loggers
#which were in the previous configuration but
#which are not in the new configuration.
existing
=
root
.
manager
.
loggerDict
.
keys
()
#now set up the new ones...
for
log
in
llist
:
sectname
=
"logger_%s"
%
log
qn
=
cp
.
get
(
sectname
,
"qualname"
)
opts
=
cp
.
options
(
sectname
)
if
"propagate"
in
opts
:
propagate
=
cp
.
getint
(
sectname
,
"propagate"
)
else
:
propagate
=
1
logger
=
logging
.
getLogger
(
qn
)
if
qn
in
existing
:
existing
.
remove
(
qn
)
if
"level"
in
opts
:
level
=
cp
.
get
(
sectname
,
"level"
)
logger
.
setLevel
(
logging
.
_levelNames
[
level
])
for
h
in
logger
.
handlers
[:]:
logger
.
removeHandler
(
h
)
logger
.
propagate
=
propagate
logger
.
disabled
=
0
hlist
=
cp
.
get
(
sectname
,
"handlers"
)
if
len
(
hlist
):
hlist
=
string
.
split
(
hlist
,
","
)
for
hand
in
hlist
:
logger
.
addHandler
(
handlers
[
hand
])
#Disable any old loggers. There's no point deleting
#them as other threads may continue to hold references
#and by disabling them, you stop them doing any logging.
for
log
in
existing
:
root
.
manager
.
loggerDict
[
log
].
disabled
=
1
except
:
ei
=
sys
.
exc_info
()
traceback
.
print_exception
(
ei
[
0
],
ei
[
1
],
ei
[
2
],
None
,
sys
.
stderr
)
del
ei
logging
.
_handlers
.
clear
()
# Handlers add themselves to logging._handlers
handlers
=
_install_handlers
(
cp
,
formatters
)
_install_loggers
(
cp
,
handlers
)
finally
:
logging
.
_releaseLock
()
def
_create_formatters
(
cp
):
"""Create and return formatters"""
flist
=
cp
.
get
(
"formatters"
,
"keys"
)
if
not
len
(
flist
):
return
{}
flist
=
string
.
split
(
flist
,
","
)
formatters
=
{}
for
form
in
flist
:
sectname
=
"formatter_%s"
%
form
opts
=
cp
.
options
(
sectname
)
if
"format"
in
opts
:
fs
=
cp
.
get
(
sectname
,
"format"
,
1
)
else
:
fs
=
None
if
"datefmt"
in
opts
:
dfs
=
cp
.
get
(
sectname
,
"datefmt"
,
1
)
else
:
dfs
=
None
f
=
logging
.
Formatter
(
fs
,
dfs
)
formatters
[
form
]
=
f
return
formatters
def
_install_handlers
(
cp
,
formatters
):
"""Install and return handlers"""
hlist
=
cp
.
get
(
"handlers"
,
"keys"
)
if
not
len
(
hlist
):
return
{}
hlist
=
string
.
split
(
hlist
,
","
)
handlers
=
{}
fixups
=
[]
#for inter-handler references
for
hand
in
hlist
:
sectname
=
"handler_%s"
%
hand
klass
=
cp
.
get
(
sectname
,
"class"
)
opts
=
cp
.
options
(
sectname
)
if
"formatter"
in
opts
:
fmt
=
cp
.
get
(
sectname
,
"formatter"
)
else
:
fmt
=
""
klass
=
eval
(
klass
,
vars
(
logging
))
args
=
cp
.
get
(
sectname
,
"args"
)
args
=
eval
(
args
,
vars
(
logging
))
h
=
apply
(
klass
,
args
)
if
"level"
in
opts
:
level
=
cp
.
get
(
sectname
,
"level"
)
h
.
setLevel
(
logging
.
_levelNames
[
level
])
if
len
(
fmt
):
h
.
setFormatter
(
formatters
[
fmt
])
#temporary hack for FileHandler and MemoryHandler.
if
klass
==
logging
.
handlers
.
MemoryHandler
:
if
"target"
in
opts
:
target
=
cp
.
get
(
sectname
,
"target"
)
else
:
target
=
""
if
len
(
target
):
#the target handler may not be loaded yet, so keep for later...
fixups
.
append
((
h
,
target
))
handlers
[
hand
]
=
h
#now all handlers are loaded, fixup inter-handler references...
for
h
,
t
in
fixups
:
h
.
setTarget
(
handlers
[
t
])
return
handlers
def
_install_loggers
(
cp
,
handlers
):
"""Create and install loggers"""
# configure the root first
llist
=
cp
.
get
(
"loggers"
,
"keys"
)
llist
=
string
.
split
(
llist
,
","
)
llist
.
remove
(
"root"
)
sectname
=
"logger_root"
root
=
logging
.
root
log
=
root
opts
=
cp
.
options
(
sectname
)
if
"level"
in
opts
:
level
=
cp
.
get
(
sectname
,
"level"
)
log
.
setLevel
(
logging
.
_levelNames
[
level
])
for
h
in
root
.
handlers
[:]:
root
.
removeHandler
(
h
)
hlist
=
cp
.
get
(
sectname
,
"handlers"
)
if
len
(
hlist
):
hlist
=
string
.
split
(
hlist
,
","
)
for
hand
in
hlist
:
log
.
addHandler
(
handlers
[
hand
])
#and now the others...
#we don't want to lose the existing loggers,
#since other threads may have pointers to them.
#existing is set to contain all existing loggers,
#and as we go through the new configuration we
#remove any which are configured. At the end,
#what's left in existing is the set of loggers
#which were in the previous configuration but
#which are not in the new configuration.
existing
=
root
.
manager
.
loggerDict
.
keys
()
#now set up the new ones...
for
log
in
llist
:
sectname
=
"logger_%s"
%
log
qn
=
cp
.
get
(
sectname
,
"qualname"
)
opts
=
cp
.
options
(
sectname
)
if
"propagate"
in
opts
:
propagate
=
cp
.
getint
(
sectname
,
"propagate"
)
else
:
propagate
=
1
logger
=
logging
.
getLogger
(
qn
)
if
qn
in
existing
:
existing
.
remove
(
qn
)
if
"level"
in
opts
:
level
=
cp
.
get
(
sectname
,
"level"
)
logger
.
setLevel
(
logging
.
_levelNames
[
level
])
for
h
in
logger
.
handlers
[:]:
logger
.
removeHandler
(
h
)
logger
.
propagate
=
propagate
logger
.
disabled
=
0
hlist
=
cp
.
get
(
sectname
,
"handlers"
)
if
len
(
hlist
):
hlist
=
string
.
split
(
hlist
,
","
)
for
hand
in
hlist
:
logger
.
addHandler
(
handlers
[
hand
])
#Disable any old loggers. There's no point deleting
#them as other threads may continue to hold references
#and by disabling them, you stop them doing any logging.
for
log
in
existing
:
root
.
manager
.
loggerDict
[
log
].
disabled
=
1
def
listen
(
port
=
DEFAULT_LOGGING_CONFIG_PORT
):
"""
Start up a socket server on the specified port, and listen for new
...
...
@@ -247,7 +259,12 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
f
=
open
(
file
,
"w"
)
f
.
write
(
chunk
)
f
.
close
()
fileConfig
(
file
)
try
:
fileConfig
(
file
)
except
(
KeyboardInterrupt
,
SystemExit
):
raise
except
:
traceback
.
print_exc
()
os
.
remove
(
file
)
except
socket
.
error
,
e
:
if
type
(
e
.
args
)
!=
types
.
TupleType
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment