| Conditions | 16 |
| Total Lines | 72 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ocrd_utils.logging.initLogging() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """ |
||
| 132 | def initLogging(builtin_only=False, force_reinit=False, silent=not config.OCRD_LOGGING_DEBUG): |
||
| 133 | """ |
||
| 134 | Reset ``ocrd`` logger, read logging configuration if exists, otherwise use basicConfig |
||
| 135 | |||
| 136 | initLogging is to be called by OCR-D/core once, i.e. |
||
| 137 | - for the ``ocrd`` CLI |
||
| 138 | - for the processor wrapper methods |
||
| 139 | |||
| 140 | Other processes that use OCR-D/core as a library can, but do not have to, use this functionality. |
||
| 141 | |||
| 142 | Keyword Args: |
||
| 143 | - builtin_only (bool, False): Whether to search for logging configuration |
||
| 144 | on-disk (``False``) or only use the |
||
| 145 | hard-coded config (``True``). For testing |
||
| 146 | - force_reinit (bool, False): Whether to ignore the module-level |
||
| 147 | ``_initialized_flag``. For testing only. |
||
| 148 | - silent (bool, True): Whether to log logging behavior by printing to stderr |
||
| 149 | """ |
||
| 150 | global _initialized_flag |
||
| 151 | if _initialized_flag and not force_reinit: |
||
| 152 | return |
||
| 153 | # disableLogging() |
||
| 154 | |||
| 155 | # https://docs.python.org/3/library/logging.html#logging.disable |
||
| 156 | # If logging.disable(logging.NOTSET) is called, it effectively removes this |
||
| 157 | # overriding level, so that logging output again depends on the effective |
||
| 158 | # levels of individual loggers. |
||
| 159 | logging.disable(logging.NOTSET) |
||
| 160 | |||
| 161 | # remove all handlers for the ocrd root loggers |
||
| 162 | for logger_name in ROOT_OCRD_LOGGERS: |
||
| 163 | for handler in logging.getLogger(logger_name).handlers[:]: |
||
| 164 | logging.getLogger(logger_name).removeHandler(handler) |
||
| 165 | |||
| 166 | config_file = None |
||
| 167 | if not builtin_only: |
||
| 168 | CONFIG_PATHS = [ |
||
| 169 | Path.cwd(), |
||
| 170 | Path.home(), |
||
| 171 | Path('/etc'), |
||
| 172 | ] |
||
| 173 | config_file = [f for f \ |
||
| 174 | in [p / 'ocrd_logging.conf' for p in CONFIG_PATHS] \ |
||
| 175 | if f.exists()] |
||
| 176 | if config_file: |
||
| 177 | if len(config_file) > 1 and not silent: |
||
| 178 | print(f"[LOGGING] Multiple logging configuration files found at {config_file}, using first one", file=sys.stderr) |
||
| 179 | config_file = config_file[0] |
||
| 180 | if not silent: |
||
| 181 | print(f"[LOGGING] Picked up logging config at {config_file}", file=sys.stderr) |
||
| 182 | logging.config.fileConfig(config_file) |
||
| 183 | # Set permission of processing-server logfile to 666 to prevent possible permission erros while logging |
||
| 184 | try: |
||
| 185 | network_logger = logging.getLogger("ocrd_network") |
||
| 186 | for handler in network_logger.handlers: |
||
| 187 | if isinstance(handler, logging.FileHandler): |
||
| 188 | chmod(handler.baseFilename, 0o664) |
||
| 189 | except PermissionError: |
||
| 190 | # if the file exists the permissions are supposed to already fit |
||
| 191 | pass |
||
| 192 | else: |
||
| 193 | if not silent: |
||
| 194 | print("[LOGGING] Initializing logging with built-in defaults", file=sys.stderr) |
||
| 195 | # Default logging config |
||
| 196 | ocrd_handler = logging.StreamHandler(stream=sys.stderr) |
||
| 197 | ocrd_handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT, datefmt=LOG_TIMEFMT)) |
||
| 198 | ocrd_handler.setLevel(logging.DEBUG) |
||
| 199 | for logger_name in ROOT_OCRD_LOGGERS: |
||
| 200 | logging.getLogger(logger_name).addHandler(ocrd_handler) |
||
| 201 | for logger_name, logger_level in LOGGING_DEFAULTS.items(): |
||
| 202 | logging.getLogger(logger_name).setLevel(logger_level) |
||
| 203 | _initialized_flag = True |
||
| 204 | |||
| 235 |