Skip to content

logging

get_console_handler()

Instanciates the logging output on the console.

Source code in src/dxtr/utils/logging.py
45
46
47
48
49
50
def get_console_handler() -> logging.StreamHandler:
    """Instanciates the logging output on the console.
    """
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setFormatter(FORMATTER_CNSL)
    return console_handler

get_file_handler(log_file, log_level=DEFAULT_LOG_LEVEL_FILE, time_rotating=True)

Instanciate the logging output written on disk.

Parameters:

Name Type Description Default
log_file str

The file where to store the log.

required
log_level str

The log level to save on file.

DEFAULT_LOG_LEVEL_FILE
time_rotating bool

If True, a time-rotating managment of log files is set.

True

Returns:

Type Description
file_handler

The object that manages all the log messages.

Notes
  • If time-rotation is used
    • One file is generated per day, starting at midnight.
    • At most, 5 files are stored on disk. Meaning that we conserve at most a week of log.
Source code in src/dxtr/utils/logging.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def get_file_handler(log_file:str,
                     log_level:str=DEFAULT_LOG_LEVEL_FILE,
                     time_rotating:bool=True,
                     ) -> logging.FileHandler:
    """Instanciate the logging output written on disk.

    Parameters
    ----------
    log_file
        The file where to store the log.
    log_level
        The log level to save on file.
    time_rotating
        If True, a time-rotating managment of log files is set.

    Returns
    -------
    file_handler
        The object that manages all the log messages.

    Notes
    -----
      * If time-rotation is used
        * One file is generated per day, starting at midnight.
        * At most, 5 files are stored on disk. Meaning that we conserve at
          most a week of log.
    """

    if time_rotating:
        file_handler = handlers.TimedRotatingFileHandler(
            log_file, when='midnight', backupCount=5)
    else:
        file_handler = logging.FileHandler(log_file, mode='w')

    file_handler.setFormatter(FORMATTER_FILE)
    file_handler.setLevel(log_level)
    return file_handler

get_logger(logger_name, log_file=None, log_level=DEFAULT_LOG_LEVEL_CNSL)

Instanciates the two logging outputs: console & file on disk.

Parameters:

Name Type Description Default
logger_name str

The name given to the created Logger object.

required
log_file Optional[str]

Optional, default is None. The path to the file where to write the log.

None
log_level

Optional, default is 'INFO'. The log level for the console output.

DEFAULT_LOG_LEVEL_CNSL

Returns:

Type Description
The Logger object gathering all the info.
Source code in src/dxtr/utils/logging.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def get_logger(logger_name:str, log_file:Optional[str]=None, 
               log_level=DEFAULT_LOG_LEVEL_CNSL) -> logging.Logger:
    """Instanciates the two logging outputs: console & file on disk.

    Parameters
    ----------
    logger_name
        The name given to the created Logger object.

    log_file 
        Optional, default is None. The path to the file where to write the log.

    log_level
        Optional, default is 'INFO'. The log level for the console output.

    Returns
    -------
        The Logger object gathering all the info.
    """

    if log_file is None:
        here = os.getcwd()
        name = '.'.join([logger_name, 'log'])
        log_file = '/'.join([here, name])

    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    logger.addHandler(get_console_handler())
    logger.addHandler(get_file_handler(log_file, log_level))

    logger.propagate = False

    return logger

mutelogging(func)

Increases momentary the logging level so no info are displayed.

Source code in src/dxtr/utils/logging.py
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def mutelogging(func) -> Any:
    """Increases momentary the logging level so no info are displayed.
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        from dxtr import logger

        initial_logging_level = logger.level
        logger.setLevel(LEVEL['ERROR'])

        result = func(*args, **kwargs)

        logger.setLevel(initial_logging_level)

        return result

    return wrapper

require_pyvista(func)

Checks if the library pyvista is installed.

Notes
  • This library is an optional dependency.
  • It is used in the visu and io modules.
  • In the io module it is required for recording Cochain.
  • The dependency to logger does not seem super clean... Maybe this means that this decorator does not belong here...
Source code in src/dxtr/utils/logging.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def require_pyvista(func) -> Optional[Any]:
    """Checks if the library pyvista is installed.

    Notes
    -----
      * This library is an optional dependency.
      * It is used in the visu and io modules.
      * In the io module it is required for recording `Cochain`.
      * The dependency to logger does not seem super clean... 
        Maybe this means that this decorator does not belong here...
    """

    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            import pyvista as pv 
            return func(*args, **kwargs)
        except ImportError:
            from dxtr import logger
            logger.warning('Pyvista not installed', ImportWarning)
            return None
    return wrapper