Skip to content

Commit f3babfb

Browse files
blueyedjustinmk
authored andcommitted
setup_logging: warn about invalid log level from env (#413)
1 parent 0a84966 commit f3babfb

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

pynvim/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,14 @@ def setup_logging(name):
135135
'%(filename)s:%(funcName)s:%(lineno)s] %(process)s - %(message)s')
136136
logging.root.addHandler(handler)
137137
level = logging.INFO
138-
if 'NVIM_PYTHON_LOG_LEVEL' in os.environ:
139-
lvl = getattr(logging,
140-
os.environ['NVIM_PYTHON_LOG_LEVEL'].strip(),
141-
level)
138+
env_log_level = os.environ.get('NVIM_PYTHON_LOG_LEVEL', None)
139+
if env_log_level is not None:
140+
lvl = getattr(logging, env_log_level.strip(), None)
142141
if isinstance(lvl, int):
143142
level = lvl
143+
else:
144+
logger.warning('Invalid NVIM_PYTHON_LOG_LEVEL: %r, using INFO.',
145+
env_log_level)
144146
logger.setLevel(level)
145147

146148

test/test_logging.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import sys
3+
4+
5+
def test_setup_logging(monkeypatch, tmpdir, caplog):
6+
from pynvim import setup_logging
7+
8+
major_version = sys.version_info[0]
9+
10+
setup_logging('name1')
11+
assert caplog.messages == []
12+
13+
def get_expected_logfile(prefix, name):
14+
return '{}_py{}_{}'.format(prefix, major_version, name)
15+
16+
prefix = tmpdir.join('testlog1')
17+
monkeypatch.setenv('NVIM_PYTHON_LOG_FILE', str(prefix))
18+
setup_logging('name2')
19+
assert caplog.messages == []
20+
logfile = get_expected_logfile(prefix, 'name2')
21+
assert os.path.exists(logfile)
22+
assert open(logfile, 'r').read() == ''
23+
24+
monkeypatch.setenv('NVIM_PYTHON_LOG_LEVEL', 'invalid')
25+
setup_logging('name3')
26+
assert caplog.record_tuples == [
27+
('pynvim', 30, "Invalid NVIM_PYTHON_LOG_LEVEL: 'invalid', using INFO."),
28+
]
29+
logfile = get_expected_logfile(prefix, 'name2')
30+
assert os.path.exists(logfile)
31+
with open(logfile, 'r') as f:
32+
lines = f.readlines()
33+
assert len(lines) == 1
34+
assert lines[0].endswith(
35+
"- Invalid NVIM_PYTHON_LOG_LEVEL: 'invalid', using INFO.\n"
36+
)

0 commit comments

Comments
 (0)