Merge ~cjwatson/launchpad:py3-logger-options into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 68d830809697c383ce5bbae9682a4a24b2cbf353
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-logger-options
Merge into: launchpad:master
Diff against target: 41 lines (+14/-5)
1 file modified
lib/lp/services/scripts/logger.py (+14/-5)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+391919@code.launchpad.net

Commit message

Port logger_options to Python >= 3.4

Description of the change

We use some internal details of the logging module, which changed in Python 3.4 (https://bugs.python.org/issue18046).

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) wrote :

looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/scripts/logger.py b/lib/lp/services/scripts/logger.py
2index a180651..f5e3242 100644
3--- a/lib/lp/services/scripts/logger.py
4+++ b/lib/lp/services/scripts/logger.py
5@@ -223,9 +223,18 @@ def logger_options(parser, default=logging.INFO, milliseconds=False):
6 parser, default,
7 LogLevelNudger(default, False), LogLevelNudger(default, True))
8
9- debug_levels = ', '.join([
10- v for k, v in sorted(logging._levelNames.items(), reverse=True)
11- if isinstance(k, int)])
12+ if hasattr(logging, '_levelToName'):
13+ # Python >= 3.4
14+ name_to_level = dict(logging._nameToLevel)
15+ debug_levels = ', '.join(
16+ v for k, v in sorted(logging._levelToName.items(), reverse=True))
17+ else:
18+ name_to_level = {
19+ k: v for k, v in logging._levelNames.items()
20+ if not isinstance(k, int)}
21+ debug_levels = ', '.join(
22+ v for k, v in sorted(logging._levelNames.items(), reverse=True)
23+ if isinstance(k, int))
24
25 def log_file(option, opt_str, value, parser):
26 try:
27@@ -235,12 +244,12 @@ def logger_options(parser, default=logging.INFO, milliseconds=False):
28
29 if isinstance(level, int):
30 pass
31- elif level.upper() not in logging._levelNames:
32+ elif level.upper() not in name_to_level:
33 parser.error(
34 "'%s' is not a valid logging level. Must be one of %s" % (
35 level, debug_levels))
36 else:
37- level = logging._levelNames[level.upper()]
38+ level = name_to_level[level.upper()]
39
40 if not path:
41 parser.error("Path to log file not specified")

Subscribers

People subscribed via source and target branches

to status/vote changes: