|
1 | 1 | --- |
2 | 2 | title: logging |
3 | 3 | createTime: 2025/06/09 11:39:11 |
4 | | -permalink: /en/article/25rhx6ij/ |
| 4 | +permalink: /en/dev_guide/logging/ |
5 | 5 | --- |
| 6 | + |
6 | 7 | ## Logger |
7 | 8 |
|
8 | | -目前logger的初始化在pipeline_step.py中 |
| 9 | +Currently, the logger is initialized in `pipeline_step.py`: |
| 10 | + |
9 | 11 | ```python |
10 | 12 | import logging |
11 | 13 | logging.basicConfig(level=logging.INFO, |
12 | 14 | format="%(asctime)s | %(filename)-20s- %(module)-20s- %(funcName)-20s- %(lineno)5d - %(name)-10s | %(levelname)8s | Processno %(process)5d - Threadno %(thread)-15d : %(message)s", |
13 | 15 | datefmt="%Y-%m-%d %H:%M:%S" |
14 | 16 | ) |
15 | 17 | ``` |
16 | | -使用方法如下所示,其中debug, info, warning, error代表不同的日志等级,默认情况下DEBUG等级的日志不会显示。 |
| 18 | + |
| 19 | +Usage is as follows. `debug`, `info`, `warning`, and `error` represent different log levels. By default, logs at the DEBUG level are not shown. |
| 20 | + |
17 | 21 | ```python |
18 | 22 | def main(): |
19 | | - |
| 23 | + |
20 | 24 | logging.debug("This is DEBUG message") |
21 | 25 | logging.info("This is INFO message") |
22 | 26 | logging.warning("This is WARNING message") |
23 | 27 | logging.error("This is ERROR message") |
24 | | - |
| 28 | + |
25 | 29 | return |
26 | 30 |
|
27 | 31 | main() |
28 | 32 | ``` |
29 | | -关于等级的分配原则: |
30 | | -1. DEBUG:一些没什么用需要屏蔽的输出 / 不想展示的技术细节,如: |
31 | | -```python |
32 | | - for x in ['Text', 'image', 'video']: |
33 | | - module_path = "dataflow.Eval." + x |
34 | | - try: |
35 | | - module_lib = importlib.import_module(module_path) |
36 | | - clss = getattr(module_lib, name) |
37 | | - self._obj_map[name] = clss |
38 | | - return clss |
39 | | - except AttributeError as e: |
40 | | - logging.debug(f"{str(e)}") |
41 | | - continue |
42 | | - except Exception as e: |
43 | | - raise e |
44 | | -``` |
45 | | -2. INFO: 让用户得知目前的运行情况,如: |
46 | | -```python |
47 | | -def pipeline_step(yaml_path, step_name, step_type): |
48 | | - import logging |
49 | | - import yaml |
50 | | - logging.info(f"Loading yaml {yaml_path} ......") |
51 | | - with open(yaml_path, "r") as f: |
52 | | - config = yaml.safe_load(f) |
53 | | - config = merge_yaml(config) |
54 | | - logging.info(f"Load yaml success, config: {config}") |
55 | | - if step_type == "process": |
56 | | - algorithm = get_processor(step_name, config) |
57 | | - elif step_type == "generator": |
58 | | - algorithm = get_generator(step_name, config) |
59 | | - logging.info("Start running ...") |
60 | | - algorithm.run() |
61 | | -``` |
62 | | -3. WARNING:可能出现问题的错误信息(暂时没有例子) |
63 | | -4. ERROR:运行出现错误,打印错误信息 |
64 | 33 |
|
65 | | -算子内部的logging可以参考`DataFlow/dataflow/generator/algorithms/TreeSitterParser.py` |
| 34 | +Principles for assigning log levels: |
| 35 | + |
| 36 | +1. **DEBUG**: Outputs that are not very useful or should be hidden / technical details you don’t want to expose, such as: |
| 37 | + |
| 38 | + ```python |
| 39 | + for x in ['Text', 'image', 'video']: |
| 40 | + module_path = "dataflow.Eval." + x |
| 41 | + try: |
| 42 | + module_lib = importlib.import_module(module_path) |
| 43 | + clss = getattr(module_lib, name) |
| 44 | + self._obj_map[name] = clss |
| 45 | + return clss |
| 46 | + except AttributeError as e: |
| 47 | + logging.debug(f"{str(e)}") |
| 48 | + continue |
| 49 | + except Exception as e: |
| 50 | + raise e |
| 51 | + ``` |
| 52 | + |
| 53 | +2. **INFO**: Used to let users know the current execution status, such as: |
| 54 | + |
| 55 | + ```python |
| 56 | + def pipeline_step(yaml_path, step_name, step_type): |
| 57 | + import logging |
| 58 | + import yaml |
| 59 | + logging.info(f"Loading yaml {yaml_path} ......") |
| 60 | + with open(yaml_path, "r") as f: |
| 61 | + config = yaml.safe_load(f) |
| 62 | + config = merge_yaml(config) |
| 63 | + logging.info(f"Load yaml success, config: {config}") |
| 64 | + if step_type == "process": |
| 65 | + algorithm = get_processor(step_name, config) |
| 66 | + elif step_type == "generator": |
| 67 | + algorithm = get_generator(step_name, config) |
| 68 | + logging.info("Start running ...") |
| 69 | + algorithm.run() |
| 70 | + ``` |
| 71 | + |
| 72 | +3. **WARNING**: Error messages indicating potential issues (no examples for now). |
| 73 | + |
| 74 | +4. **ERROR**: Errors that occur during execution; used to print error messages. |
| 75 | + |
| 76 | +For logging inside operators, refer to `DataFlow/dataflow/generator/algorithms/TreeSitterParser.py`. |
0 commit comments