Coverage for src / ensembl / utils / logging.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-21 10:45 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15"""Easy initialisation functionality to set an event logging system. 

16 

17Examples: 

18 

19 >>> import logging, pathlib 

20 >>> from ensembl.utils.logging import init_logging 

21 >>> logfile = pathlib.Path("test.log") 

22 >>> init_logging("INFO", logfile, "DEBUG") 

23 >>> logging.info("This message is written in both stderr and the log file") 

24 >>> logging.debug("This message is only written in the log file") 

25 

26""" 

27 

28from __future__ import annotations 

29 

30__all__ = [ 

31 "LogLevel", 

32 "init_logging", 

33 "init_logging_with_args", 

34] 

35 

36import argparse 

37from datetime import datetime 

38import logging 

39from typing import Optional, Union 

40 

41from ensembl.utils import StrPath 

42 

43LogLevel = Union[int, str] 

44 

45 

46def formatTime( 

47 record: logging.LogRecord, 

48 datefmt: str | None = None, # pylint: disable=unused-argument 

49) -> str: 

50 """Returns the creation time of the log record in ISO8601 format. 

51 

52 Args: 

53 record: Log record to format. 

54 datefmt: Date format to use. Ignored in this implementation. 

55 """ 

56 return datetime.fromtimestamp(record.created).astimezone().isoformat(timespec="milliseconds") 

57 

58 

59def init_logging( 

60 log_level: LogLevel = "WARNING", 

61 log_file: Optional[StrPath] = None, 

62 log_file_level: LogLevel = "DEBUG", 

63 msg_format: str = "%(asctime)s [%(process)s] %(levelname)-9s %(name)-13s: %(message)s", 

64) -> None: 

65 """Initialises the logging system. 

66 

67 By default, all the log messages corresponding to `log_level` (and above) will be printed in the 

68 standard error. If `log_file` is provided, all messages of `log_file_level` level (and above) will 

69 be written into the provided file. 

70 

71 Args: 

72 log_level: Minimum logging level for the standard error. 

73 log_file: Logging file where to write logging messages besides the standard error. 

74 log_file_level: Minimum logging level for the logging file. 

75 msg_format: A format string for the logged output as a whole. More information: 

76 https://docs.python.org/3/library/logging.html#logrecord-attributes 

77 

78 """ 

79 # Define new formatter used for handlers 

80 formatter = logging.Formatter(msg_format) 

81 formatter.formatTime = formatTime # type: ignore[method-assign] 

82 # Configure the basic logging system, setting the root logger to the minimum log level available 

83 # to avoid filtering messages in any handler due to "parent delegation". Also close and remove any 

84 # existing handlers before setting this configuration. 

85 logging.basicConfig(level="DEBUG", force=True) 

86 # Set the correct log level and format of the new StreamHandler (by default the latter is set to NOTSET) 

87 logging.root.handlers[0].setLevel(log_level) 

88 logging.root.handlers[0].setFormatter(formatter) 

89 if log_file: 

90 # Create the log file handler and add it to the root logger 

91 file_handler = logging.FileHandler(log_file) 

92 file_handler.setLevel(log_file_level) 

93 file_handler.setFormatter(formatter) 

94 logging.root.addHandler(file_handler) 

95 

96 

97def init_logging_with_args(args: argparse.Namespace) -> None: 

98 """Processes the Namespace object provided to call `init_logging()` with the correct arguments. 

99 

100 Args: 

101 args: Namespace populated by an argument parser. 

102 

103 """ 

104 args_dict = vars(args) 

105 log_args = {x: args_dict[x] for x in ["log_level", "log_file", "log_file_level"] if x in args_dict} 

106 init_logging(**log_args)