| 171 | return [SQLResult(status=f"Changed prompt format to {arg}")] |
| 172 | |
| 173 | def initialize_logging(self) -> None: |
| 174 | log_file = os.path.expanduser(self.config["main"]["log_file"]) |
| 175 | log_level = self.config["main"]["log_level"] |
| 176 | |
| 177 | level_map = { |
| 178 | "CRITICAL": logging.CRITICAL, |
| 179 | "ERROR": logging.ERROR, |
| 180 | "WARNING": logging.WARNING, |
| 181 | "INFO": logging.INFO, |
| 182 | "DEBUG": logging.DEBUG, |
| 183 | } |
| 184 | |
| 185 | # Disable logging if value is NONE by switching to a no-op handler |
| 186 | # Set log level to a high value so it doesn't even waste cycles getting called. |
| 187 | if log_level.upper() == "NONE": |
| 188 | handler: logging.Handler = logging.NullHandler() |
| 189 | log_level = "CRITICAL" |
| 190 | elif dir_path_exists(log_file): |
| 191 | handler = logging.FileHandler(log_file) |
| 192 | else: |
| 193 | self.echo(f'Error: Unable to open the log file "{log_file}".', err=True, fg="red") |
| 194 | return |
| 195 | |
| 196 | formatter = logging.Formatter("%(asctime)s (%(process)d/%(threadName)s) %(name)s %(levelname)s - %(message)s") |
| 197 | |
| 198 | handler.setFormatter(formatter) |
| 199 | |
| 200 | root_logger = logging.getLogger("mycli") |
| 201 | root_logger.addHandler(handler) |
| 202 | root_logger.setLevel(level_map[log_level.upper()]) |
| 203 | |
| 204 | logging.captureWarnings(True) |
| 205 | |
| 206 | root_logger.debug("Initializing mycli logging.") |
| 207 | root_logger.debug("Log file %r.", log_file) |