Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept logging level names (#5765) #5772

Merged
merged 7 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions conans/client/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from conans.paths import DEFAULT_PROFILE_NAME, conan_expand_user, CACERT_FILE
from conans.util.env_reader import get_env
from conans.util.files import load
from conans.util.log import get_log_level_by_name
import logging


Expand Down Expand Up @@ -98,7 +99,7 @@
[log]
run_to_output = True # environment CONAN_LOG_RUN_TO_OUTPUT
run_to_file = False # environment CONAN_LOG_RUN_TO_FILE
level = 50 # environment CONAN_LOGGING_LEVEL
level = critical # environment CONAN_LOGGING_LEVEL
# trace_file = # environment CONAN_TRACE_FILE
print_run_commands = False # environment CONAN_PRINT_RUN_COMMANDS

Expand Down Expand Up @@ -506,7 +507,8 @@ def logging_level(self):
if level is None:
level = self.get_item("log.level")
try:
level = int(level)
parsed_level = get_log_level_by_name(level)
level = parsed_level if parsed_level is not None else int(level)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int("wakawaka") doesn't crash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

except Exception:
level = logging.CRITICAL
return level
Expand Down
84 changes: 84 additions & 0 deletions conans/test/unittests/util/client_conf_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
import unittest
import logging

from conans.client.cache.cache import CONAN_CONF
from conans.client.conf import ConanClientConfigParser
from conans.paths import DEFAULT_PROFILE_NAME
from conans.test.utils.test_files import temp_folder
from conans.util.files import save
from conans.client.tools.oss import environment_append

default_client_conf = '''[storage]
path: ~/.conan/data
Expand All @@ -17,6 +19,17 @@

'''

default_client_conf_log = '''[storage]
uilianries marked this conversation as resolved.
Show resolved Hide resolved
path: ~/.conan/data

[log]
trace_file = "foo/bar/quotes"
{}

[general]

'''

default_profile = '''
[settings]
arch=x86_64
Expand Down Expand Up @@ -49,3 +62,74 @@ def test_proxies(self):
save(os.path.join(tmp_dir, CONAN_CONF), "[proxies]\nno_proxy=localhost")
config = ConanClientConfigParser(os.path.join(tmp_dir, CONAN_CONF))
self.assertEqual(config.proxies["no_proxy"], "localhost")


class ClientConfLogTest(unittest.TestCase):

def setUp(self):
self.tmp_dir = temp_folder()
save(os.path.join(self.tmp_dir, DEFAULT_PROFILE_NAME), default_profile)
try:
del os.environ["CONAN_LOGGING_LEVEL"]
except:
pass

def test_log_level_numbers_critical(self):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf_log.format("level = 50"))
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.CRITICAL, config.logging_level)

def test_log_level_numbers_debug(self):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf_log.format("level = 10"))
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.DEBUG, config.logging_level)

def test_log_level_numbers_invalid(self):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf_log.format("level = wakawaka"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. Shouldn't we rise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from early condition, any error is treated as default logging level (CRITICAL). Should we change it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.CRITICAL, config.logging_level)

def test_log_level_numbers_env_var_debug(self):
with environment_append({"CONAN_LOGGING_LEVEL": "10"}):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf)
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.DEBUG, config.logging_level)

def test_log_level_numbers_env_var_debug(self):
with environment_append({"CONAN_LOGGING_LEVEL": "WakaWaka"}):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf)
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.CRITICAL, config.logging_level)

def test_log_level_names_debug(self):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf_log.format("level = debug"))
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.DEBUG, config.logging_level)

def test_log_level_names_critical(self):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf_log.format("level = Critical"))
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.CRITICAL, config.logging_level)

def test_log_level_names_invalid(self):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf_log.format("level = wakawaka"))
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.CRITICAL, config.logging_level)

def test_log_level_names_env_var_debug(self):
with environment_append({"CONAN_LOGGING_LEVEL": "Debug"}):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf)
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.DEBUG, config.logging_level)

def test_log_level_names_env_var_warning(self):
with environment_append({"CONAN_LOGGING_LEVEL": "WARNING"}):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf)
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.WARNING, config.logging_level)

def test_log_level_names_env_var_invalid(self):
with environment_append({"CONAN_LOGGING_LEVEL": "WakaWaka"}):
save(os.path.join(self.tmp_dir, CONAN_CONF), default_client_conf)
config = ConanClientConfigParser(os.path.join(self.tmp_dir, CONAN_CONF))
self.assertEqual(logging.CRITICAL, config.logging_level)
13 changes: 13 additions & 0 deletions conans/util/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def configure_logger(logging_level=logging.CRITICAL, logging_file=None):
return logger


def get_log_level_by_name(level_name):
uilianries marked this conversation as resolved.
Show resolved Hide resolved
levels = {
"critical": logging.CRITICAL,
"error": logging.ERROR,
"warning": logging.WARNING,
"warn": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
"notset": logging.NOTSET
}
return levels.get(str(level_name).lower())


logger = configure_logger()

# CRITICAL = 50
Expand Down