|
1 | | -import logging |
2 | | -from typing import Optional, Union |
| 1 | +import warnings |
3 | 2 |
|
| 3 | +from openeo.internal.warnings import UserDeprecationWarning |
| 4 | +from openeo.rest.models.logs import LogEntry, log_level_name, normalize_log_level |
4 | 5 |
|
5 | | -class LogEntry(dict): |
6 | | - """ |
7 | | - Log message and info for jobs and services |
| 6 | +warnings.warn( |
| 7 | + message="Submodule `openeo.api.logs` is deprecated in favor of `openeo.rest.models.logs`.", |
| 8 | + category=UserDeprecationWarning, |
| 9 | + stacklevel=2, |
| 10 | +) |
8 | 11 |
|
9 | | - Fields: |
10 | | - - ``id``: Unique ID for the log, string, REQUIRED |
11 | | - - ``code``: Error code, string, optional |
12 | | - - ``level``: Severity level, string (error, warning, info or debug), REQUIRED |
13 | | - - ``message``: Error message, string, REQUIRED |
14 | | - - ``time``: Date and time of the error event as RFC3339 date-time, string, available since API 1.1.0 |
15 | | - - ``path``: A "stack trace" for the process, array of dicts |
16 | | - - ``links``: Related links, array of dicts |
17 | | - - ``usage``: Usage metrics available as property 'usage', dict, available since API 1.1.0 |
18 | | - May contain the following metrics: cpu, memory, duration, network, disk, storage and other custom ones |
19 | | - Each of the metrics is also a dict with the following parts: value (numeric) and unit (string) |
20 | | - - ``data``: Arbitrary data the user wants to "log" for debugging purposes. |
21 | | - Please note that this property may not exist as there's a difference |
22 | | - between None and non-existing. None for example refers to no-data in |
23 | | - many cases while the absence of the property means that the user did |
24 | | - not provide any data for debugging. |
25 | | - """ |
26 | 12 |
|
27 | | - _required = {"id", "level", "message"} |
28 | | - |
29 | | - def __init__(self, *args, **kwargs): |
30 | | - super().__init__(*args, **kwargs) |
31 | | - |
32 | | - # Check required fields |
33 | | - missing = self._required.difference(self.keys()) |
34 | | - if missing: |
35 | | - raise ValueError("Missing required fields: {m}".format(m=sorted(missing))) |
36 | | - |
37 | | - @property |
38 | | - def id(self): |
39 | | - return self["id"] |
40 | | - |
41 | | - # Legacy alias |
42 | | - log_id = id |
43 | | - |
44 | | - @property |
45 | | - def message(self): |
46 | | - return self["message"] |
47 | | - |
48 | | - @property |
49 | | - def level(self): |
50 | | - return self["level"] |
51 | | - |
52 | | - # TODO: add properties for "code", "time", "path", "links" and "data" with sensible defaults? |
53 | | - |
54 | | - |
55 | | -def normalize_log_level( |
56 | | - log_level: Union[int, str, None], default: int = logging.DEBUG |
57 | | -) -> int: |
58 | | - """ |
59 | | - Helper function to convert a openEO API log level (e.g. string "error") |
60 | | - to the integer constants defined in Python's standard library ``logging`` module (e.g. ``logging.ERROR``). |
61 | | -
|
62 | | - :param log_level: log level to normalize: a log level string in the style of |
63 | | - the openEO API ("error", "warning", "info", or "debug"), |
64 | | - an integer value (e.g. a ``logging`` constant), or ``None``. |
65 | | -
|
66 | | - :param default: fallback log level to return on unknown log level strings or ``None`` input. |
67 | | -
|
68 | | - :raises TypeError: when log_level is any other type than str, an int or None. |
69 | | - :return: One of the following log level constants from the standard module ``logging``: |
70 | | - ``logging.ERROR``, ``logging.WARNING``, ``logging.INFO``, or ``logging.DEBUG`` . |
71 | | - """ |
72 | | - if isinstance(log_level, str): |
73 | | - log_level = log_level.upper() |
74 | | - if log_level in ["CRITICAL", "ERROR", "FATAL"]: |
75 | | - return logging.ERROR |
76 | | - elif log_level in ["WARNING", "WARN"]: |
77 | | - return logging.WARNING |
78 | | - elif log_level == "INFO": |
79 | | - return logging.INFO |
80 | | - elif log_level == "DEBUG": |
81 | | - return logging.DEBUG |
82 | | - else: |
83 | | - return default |
84 | | - elif isinstance(log_level, int): |
85 | | - return log_level |
86 | | - elif log_level is None: |
87 | | - return default |
88 | | - else: |
89 | | - raise TypeError( |
90 | | - f"Value for log_level is not an int or str: type={type(log_level)}, value={log_level!r}" |
91 | | - ) |
92 | | - |
93 | | - |
94 | | -def log_level_name(log_level: Union[int, str, None]) -> str: |
95 | | - """ |
96 | | - Get the name of a normalized log level. |
97 | | - This value conforms to log level names used in the openEO API. |
98 | | - """ |
99 | | - return logging.getLevelName(normalize_log_level(log_level)).lower() |
| 13 | +__all__ = ["LogEntry", "normalize_log_level", "log_level_name"] |
0 commit comments