X Tutup
Skip to content

Commit 8badb8e

Browse files
committed
Improve logging configuration via command line options
1 parent f3cc220 commit 8badb8e

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

bpython/args.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
Module to handle command line argument parsing, for all front-ends.
2626
"""
2727

28+
import argparse
2829
import importlib.util
30+
import logging
2931
import os
3032
import sys
31-
import argparse
3233

3334
from . import __version__, __copyright__
3435
from .config import default_config_path, loadini, Struct
@@ -118,6 +119,18 @@ def callback(group):
118119
action="store_true",
119120
help=_("Print version and exit."),
120121
)
122+
parser.add_argument(
123+
"--log-level",
124+
"-l",
125+
choices=("debug", "info", "warning", "error", "critical"),
126+
default="error",
127+
help=_("Set log level for logging"),
128+
)
129+
parser.add_argument(
130+
"--log-output",
131+
"-L",
132+
help=_("Log output file"),
133+
)
121134

122135
if extras is not None:
123136
extras_group = parser.add_argument_group(extras[0], extras[1])
@@ -147,6 +160,25 @@ def callback(group):
147160
# Just let Python handle this
148161
os.execv(sys.executable, [sys.executable] + args)
149162

163+
# Configure logging handler
164+
bpython_logger = logging.getLogger("bpython")
165+
curtsies_logger = logging.getLogger("curtsies")
166+
bpython_logger.setLevel(options.log_level.upper())
167+
curtsies_logger.setLevel(options.log_level.upper())
168+
if options.log_output:
169+
handler = logging.FileHandler(filename=options.log_output)
170+
handler.setFormatter(
171+
logging.Formatter(
172+
"%(asctime)s: %(name)s: %(levelname)s: %(message)s"
173+
)
174+
)
175+
bpython_logger.addHandler(handler)
176+
curtsies_logger.addHandler(handler)
177+
bpython_logger.propagate = curtsies_logger.propagate = False
178+
else:
179+
bpython_logger.addHandler(logging.NullHandler())
180+
curtsies_logger.addHandler(logging.NullHandler())
181+
150182
config = Struct()
151183
loadini(config, options.config)
152184

bpython/curtsies.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,6 @@ def main(args=None, locals_=None, banner=None, welcome_message=None):
129129
translations.init()
130130

131131
def curtsies_arguments(parser):
132-
parser.add_argument(
133-
"--log",
134-
"-L",
135-
action="count",
136-
help=_("log debug messages to bpython.log"),
137-
)
138132
parser.add_argument(
139133
"--paste",
140134
"-p",
@@ -150,18 +144,6 @@ def curtsies_arguments(parser):
150144
curtsies_arguments,
151145
),
152146
)
153-
if options.log is None:
154-
options.log = 0
155-
logging_levels = (logging.ERROR, logging.INFO, logging.DEBUG)
156-
level = logging_levels[min(len(logging_levels) - 1, options.log)]
157-
logging.getLogger("curtsies").setLevel(level)
158-
logging.getLogger("bpython").setLevel(level)
159-
if options.log:
160-
handler = logging.FileHandler(filename="bpython.log")
161-
logging.getLogger("curtsies").addHandler(handler)
162-
logging.getLogger("curtsies").propagate = False
163-
logging.getLogger("bpython").addHandler(handler)
164-
logging.getLogger("bpython").propagate = False
165147

166148
interp = None
167149
paste = None

doc/sphinx/source/man-bpython.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ The following options are supported by all frontends:
5757
exiting. The PYTHONSTARTUP file is not read.
5858
-q, --quiet Do not flush the output to stdout.
5959
-V, --version Print :program:`bpython`'s version and exit.
60+
-l <level>, --log-level=<level> Set logging level
61+
-L <file>, --log-output=<file> Set log output file
6062

6163
In addition to the above options, :program:`bpython` also supports the following
6264
options:
6365

64-
-L, --log Write debugging messages to the file bpython.log. Use
65-
-LL for more verbose logging.
6666
-p file, --paste=file Paste in the contents of a file at startup.
6767

6868
In addition to the common options, :program:`bpython-urwid` also supports the

0 commit comments

Comments
 (0)
X Tutup