Debian Codegraph @

cylc/flow/log_level.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Utilities for configuring logging level via the CLI."""
18
19 import logging
20 from typing import List, Dict, Union, TYPE_CHECKING
21
22 if TYPE_CHECKING:
23 import os
24
25
26 def verbosity_to_log_level(verb: int) -> int:
27 """Convert Cylc verbosity to log severity level."""
28 if verb < 0:
29 return logging.WARNING
30 if verb > 0:
31 return logging.DEBUG
32 return logging.INFO
33
34
35 def log_level_to_verbosity(lvl: int) -> int:
36 """Convert log severity level to Cylc verbosity.
37
38 Examples:
39 >>> log_level_to_verbosity(logging.NOTSET)
40 2
41 >>> log_level_to_verbosity(logging.DEBUG)
42 2
43 >>> log_level_to_verbosity(logging.INFO)
44 0
45 >>> log_level_to_verbosity(logging.WARNING)
46 -1
47 >>> log_level_to_verbosity(logging.ERROR)
48 -1
49 """
50 if lvl <= logging.DEBUG:
51 return 2
52 if lvl < logging.INFO:
53 return 1
54 if lvl == logging.INFO:
55 return 0
56 return -1
57
58
59 def verbosity_to_opts(verb: int) -> List[str]:
60 """Convert Cylc verbosity to the CLI opts required to replicate it.
61
62 Examples:
63 >>> verbosity_to_opts(0)
64 []
65 >>> verbosity_to_opts(-2)
66 ['-q', '-q']
67 >>> verbosity_to_opts(2)
68 ['-v', '-v']
69
70 """
71 return [
72 '-q'
73 for _ in range(verb, 0)
74 ] + [
75 '-v'
76 for _ in range(0, verb)
77 ]
78
79
80 def verbosity_to_env(verb: int) -> Dict[str, str]:
81 """Convert Cylc verbosity to the env vars required to replicate it.
82
83 Examples:
84 >>> verbosity_to_env(0)
85 {'CYLC_VERBOSE': 'false', 'CYLC_DEBUG': 'false'}
86 >>> verbosity_to_env(1)
87 {'CYLC_VERBOSE': 'true', 'CYLC_DEBUG': 'false'}
88 >>> verbosity_to_env(2)
89 {'CYLC_VERBOSE': 'true', 'CYLC_DEBUG': 'true'}
90
91 """
92 return {
93 'CYLC_VERBOSE': str((verb > 0)).lower(),
94 'CYLC_DEBUG': str((verb > 1)).lower(),
95 }
96
97
98 def env_to_verbosity(env: 'Union[Dict, os._Environ]') -> int:
99 """Extract verbosity from environment variables.
100
101 Examples:
102 >>> env_to_verbosity({})
103 0
104 >>> env_to_verbosity({'CYLC_VERBOSE': 'true'})
105 1
106 >>> env_to_verbosity({'CYLC_DEBUG': 'true'})
107 2
108 >>> env_to_verbosity({'CYLC_DEBUG': 'TRUE'})
109 2
110
111 """
112 return (
113 2 if env.get('CYLC_DEBUG', '').lower() == 'true'
114 else 1 if env.get('CYLC_VERBOSE', '').lower() == 'true'
115 else 0
116 )