10 examples of 'python set logging level command line' in Python

Every line of 'python set logging level command line' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
65def set_and_log_level(log_level, logger=logger):
66 """Log the global level via INFO at start."""
67 logger.setLevel(logging.getLevelName('INFO'))
68 log('log_level {}'.format(log_level), level='INFO')
69 logger.setLevel(logging.getLevelName(log_level))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

273def set_verbosity(args):
274 """Set verbosity level for logging as requested on command line.
275 """
276 if args.verbose:
277 logger.setLevel(logging.INFO)
278 logger.info("Setting verbose mode")
279 if args.veryverbose:
280 logger.setLevel(logging.DEBUG)
281 logger.info("Setting very verbose mode")
61def setup_console_logging(level='INFO'):
62 print('Logging to console, level=%s' % level)
63 logging_configuration = {
64 'version': 1,
65 'formatters': {
66 'standard': {
67 'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
68 'datefmt': '%Y-%m-%d %H:%M:%S'
69 },
70 },
71 'handlers': {
72 'console': {
73 'level': level,
74 'class': 'logging.StreamHandler',
75 'formatter': 'standard'
76 }
77 },
78 'loggers': {
79 '': {
80 'handlers': ['console'],
81 'level': level,
82 'propagate': True
83 }
84 }
85 }
86 dictConfigClass(logging_configuration).configure()
34def set_log_level(level='verbose'):
35 """Controls verbosity of the logs.
36
37 Useful for interactive/debugging applications.
38
39 Args:
40 level (str): verbosity/type of logging. Choose from:
41 - 'silent': disables logging.
42 - 'minimal': only warnings and other critical messages
43 - 'verbose': informative/descriptive messages (default).
44 - 'debug': very verbose internal/diagnostic messages.
45
46 Raises:
47 ValueError: logging level is not supported.
48 """
49
50 # Logging levels
51 _level_dict = {
52 'minimal': 30,
53 'verbose': 20,
54 'debug': 10
55 }
56
57 log_level = _level_dict.get(level)
58 if log_level is None:
59 emsg = f"Unsupported log level: {level}"
60 raise ValueError(emsg)
61
62 handler = logging.StreamHandler()
63 formatter = logging.Formatter(fmt='[%(asctime)s] %(message)s',
64 datefmt='%H:%M:%S')
65 handler.setFormatter(formatter)
66
67 # Override root logger - assume we only call this function interactively.
68 root_logger = logging.getLogger()
69 root_logger.handlers = [] # clear handler list
70 root_logger.addHandler(handler)
71
72 root_logger.setLevel(log_level)
73 logging.critical(f"Logging enabled (level={level})") # always show
15def configure_logging(level):
16 root_logger = logging.getLogger()
17 root_logger.setLevel(level)
18 ch = logging.StreamHandler()
19 ch.setLevel(level)
20 # create formatter
21 formatter = logging.Formatter('[%(asctime)s] %(name)s %(levelname)s %(message)s')
22 # add formatter to ch
23 ch.setFormatter(formatter)
24 # add ch to logger
25 root_logger.addHandler(ch)
63def set_logging(level=1):
64 """Set up logging
65
66 Parameters
67 ----------
68
69 level : `int` or `bool` (optional, default: 1)
70 If False or 0, prints WARNING and higher messages.
71 If True or 1, prints INFO and higher messages.
72 If 2 or higher, prints all messages.
73 """
74 if level is True or level == 1:
75 level = logging.INFO
76 level_name = "INFO"
77 elif level is False or level <= 0:
78 level = logging.WARNING
79 level_name = "WARNING"
80 elif level >= 2:
81 level = logging.DEBUG
82 level_name = "DEBUG"
83
84 logger = get_logger()
85 logger.setLevel(level)
86 if not logger.handlers:
87 logger.task_logger = TaskLogger(logger)
88 logger.propagate = False
89 handler = logging.StreamHandler(stream=RSafeStdErr())
90 handler.setFormatter(logging.Formatter(fmt='%(message)s'))
91 logger.addHandler(handler)
92 log_debug("Set {} logging to {}".format(__logger_name__, level_name))
57def configure_logging(args):
58 if "-v" in args or "--verbose" in args:
59 dlog.set_verbosity(dlog.DEBUG)
60 logging.basicConfig(level=logging.DEBUG)
61 elif "-q" in args or "--quiet" in args:
62 dlog.set_verbosity(dlog.ERROR)
63 logging.basicConfig(level=logging.ERROR)
64 else:
65 logging.basicConfig(level=logging.INFO)
5def setup_logging(log, level=None):
6 console_handler = logging.StreamHandler(sys.stderr)
7
8 if level is not None:
9 log.setLevel(level)
10 console_handler.setLevel(level)
11
12 console_handler.setFormatter(
13 logging.Formatter("%(levelname)s: %(message)s"))
14
15 log.addHandler(console_handler)
96def setLoggerLevel(level, logger_name=None):
97 """
98 Sets the logger level.
99
100 :param level: str, a string representing the desired level
101 :param logger_name: str, a string representing desired logger name for which
102 the level should change. The default is "None" will will
103 tweak the root logger level.
104
105 """
106 logging.getLogger(logger_name).setLevel(levels.get(level, logging.ERROR))
474@cli_base.command('set-log-level')
475@click.argument("log-level")
476def set_logging_level(log_level):
477 """configure mod_http's log level after launch
478
479 use the levels from `logging`: (DEBUG, INFO, WARNING, ERROR, CRITICAL)
480 """
481 ret = requests.post(
482 "http://localhost:2001/logging-level", data={"level": log_level}
483 )
484 ret.raise_for_status()

Related snippets