|
19 | 19 | # environment variables. |
20 | 20 | class DetectEnabled: |
21 | 21 | _instance = None |
| 22 | + _initialized = False |
| 23 | + _logged_at_least_once = False |
22 | 24 |
|
23 | 25 | def __new__(cls): |
24 | 26 | if cls._instance is None: |
@@ -58,60 +60,88 @@ def should_enable(cls, recording_method): |
58 | 60 | if recording_method in cls._detected_for_method: |
59 | 61 | return cls._detected_for_method[recording_method] |
60 | 62 | else: |
61 | | - message, enabled = cls._detect_should_enable(recording_method) |
| 63 | + enabled, log_level, message = cls._detect_should_enable(recording_method) |
62 | 64 | cls._detected_for_method[recording_method] = enabled |
63 | | - if enabled: |
64 | | - logger.warning(dedent(f"AppMap recording is enabled because {message}")) |
| 65 | + # don't log enabled messages more than once |
| 66 | + if (not cls._logged_at_least_once and logger.isEnabledFor(log_level)): |
| 67 | + cls._logged_at_least_once = True |
| 68 | + logger.log(log_level, message) |
65 | 69 | return enabled |
66 | 70 |
|
67 | 71 | @classmethod |
68 | 72 | def any_enabled(cls): |
69 | 73 | for m in RECORDING_METHODS: |
70 | | - _, enabled = cls._detect_should_enable(m) |
71 | | - if enabled: |
| 74 | + if cls.should_enable(m): |
72 | 75 | return True |
73 | 76 | return False |
74 | 77 |
|
| 78 | + @classmethod |
| 79 | + def _log_prefix(cls, should_enable, log_message): |
| 80 | + enabled_prefix = "" |
| 81 | + if not should_enable: |
| 82 | + enabled_prefix = "not " |
| 83 | + |
| 84 | + return dedent( |
| 85 | + f"AppMap recording is {enabled_prefix}enabled because {log_message}." |
| 86 | + ) |
| 87 | + |
75 | 88 | @classmethod |
76 | 89 | def _detect_should_enable(cls, recording_method): |
77 | 90 | if not recording_method: |
78 | | - return ["no recording method is set", False] |
| 91 | + return False, logging.WARNING, cls._log_prefix(False, "no recording method is set") |
79 | 92 |
|
80 | 93 | if recording_method not in RECORDING_METHODS: |
81 | | - return ["invalid recording method", False] |
| 94 | + return False, logging.WARNING, cls._log_prefix( |
| 95 | + False, f"{recording_method} is an invalid recording method" |
| 96 | + ) |
82 | 97 |
|
83 | 98 | # explicitly disabled or enabled |
84 | 99 | if "APPMAP" in os.environ: |
85 | | - if os.environ["APPMAP"] == "false": |
86 | | - return ["APPMAP=false", False] |
87 | | - elif os.environ["APPMAP"] == "true": |
88 | | - return ["APPMAP=true", True] |
| 100 | + if os.environ["APPMAP"].lower() == "false": |
| 101 | + return False, logging.INFO, cls._log_prefix(False, f"APPMAP=false") |
| 102 | + elif os.environ["APPMAP"].lower() == "true": |
| 103 | + return True, logging.INFO, cls._log_prefix(True, f"APPMAP=true") |
| 104 | + else: |
| 105 | + return False, logging.WARNING, cls._log_prefix(False, f"APPMAP={os.environ['APPMAP']} is an invalid option") |
89 | 106 |
|
90 | 107 | # recording method explicitly disabled or enabled |
91 | 108 | if recording_method: |
92 | 109 | for one_recording_method in RECORDING_METHODS: |
93 | 110 | if one_recording_method == recording_method.lower(): |
94 | 111 | env_var = "_".join(["APPMAP", "RECORD", recording_method.upper()]) |
95 | 112 | if env_var in os.environ: |
96 | | - if os.environ[env_var] == "false": |
97 | | - return [f"{env_var}=false", False] |
98 | | - elif os.environ[env_var] == "true": |
99 | | - return [f"{env_var}=true", True] |
| 113 | + if os.environ[env_var].lower() == "false": |
| 114 | + return False, logging.INFO, cls._log_prefix(False, f"{env_var}=false") |
| 115 | + elif os.environ[env_var].lower() == "true": |
| 116 | + return True, logging.INFO, cls._log_prefix(True, f"{env_var}=true") |
| 117 | + else: |
| 118 | + return False, logging.WARNING, cls._log_prefix(False, f"{env_var}={os.environ[env_var]} is an invalid option") |
| 119 | + |
| 120 | + # check if name of APPMAP_RECORD_ env variable was defined incorrectly |
| 121 | + for env_var in os.environ: |
| 122 | + env_var_as_list = env_var.split("_") |
| 123 | + if ( |
| 124 | + len(env_var_as_list) > 2 |
| 125 | + and env_var_as_list[0] == "APPMAP" |
| 126 | + and env_var_as_list[1] == "RECORD" |
| 127 | + ): |
| 128 | + if not (env_var_as_list[2].lower() in RECORDING_METHODS): |
| 129 | + return False, logging.WARNING, cls._log_prefix(False, f"{env_var} is an invalid recording method") |
100 | 130 |
|
101 | 131 | # it's flask |
102 | 132 | message, should_enable = cls.is_flask_and_should_enable() |
103 | | - if should_enable == True or should_enable == False: |
104 | | - return [message, should_enable] |
| 133 | + if should_enable in [True, False]: |
| 134 | + return should_enable, logging.INFO, cls._log_prefix(should_enable, f"{message}") |
105 | 135 |
|
106 | 136 | # it's django |
107 | 137 | message, should_enable = cls.is_django_and_should_enable() |
108 | | - if should_enable == True or should_enable == False: |
109 | | - return [message, should_enable] |
| 138 | + if should_enable in [True, False]: |
| 139 | + return should_enable, logging.INFO, cls._log_prefix(should_enable, f"{message}") |
110 | 140 |
|
111 | 141 | if recording_method in RECORDING_METHODS: |
112 | | - return ["will record by default", True] |
| 142 | + return True, logging.INFO, cls._log_prefix(True, f"will record by default") |
113 | 143 |
|
114 | | - return ["it's not enabled by any configuration or framework", False] |
| 144 | + return False, logging.INFO, cls._log_prefix(False, f"it's not enabled by any configuration or framework") |
115 | 145 |
|
116 | 146 | @classmethod |
117 | 147 | def is_flask_and_should_enable(cls): |
|
0 commit comments