langfuse.utils.error_logging

 1import functools
 2import logging
 3from typing import List, Optional
 4
 5logger = logging.getLogger("langfuse")
 6
 7
 8def catch_and_log_errors(func):
 9    """Catch all exceptions and log them. Do NOT re-raise the exception."""
10
11    @functools.wraps(func)
12    def wrapper(*args, **kwargs):
13        try:
14            return func(*args, **kwargs)
15        except Exception as e:
16            logger.error(f"An error occurred in {func.__name__}: {e}", exc_info=True)
17
18    return wrapper
19
20
21def auto_decorate_methods_with(decorator, exclude: Optional[List[str]] = []):
22    """Class decorator to automatically apply a given decorator to all
23    methods of a class.
24    """
25
26    def class_decorator(cls):
27        for attr_name, attr_value in cls.__dict__.items():
28            if attr_name in exclude:
29                continue
30            if callable(attr_value):
31                # Wrap callable attributes (methods) with the decorator
32                setattr(cls, attr_name, decorator(attr_value))
33            elif isinstance(attr_value, classmethod):
34                # Special handling for classmethods
35                original_method = attr_value.__func__
36                decorated_method = decorator(original_method)
37                setattr(cls, attr_name, classmethod(decorated_method))
38            elif isinstance(attr_value, staticmethod):
39                # Special handling for staticmethods
40                original_method = attr_value.__func__
41                decorated_method = decorator(original_method)
42                setattr(cls, attr_name, staticmethod(decorated_method))
43        return cls
44
45    return class_decorator
logger = <Logger langfuse (WARNING)>
def catch_and_log_errors(func):
 9def catch_and_log_errors(func):
10    """Catch all exceptions and log them. Do NOT re-raise the exception."""
11
12    @functools.wraps(func)
13    def wrapper(*args, **kwargs):
14        try:
15            return func(*args, **kwargs)
16        except Exception as e:
17            logger.error(f"An error occurred in {func.__name__}: {e}", exc_info=True)
18
19    return wrapper

Catch all exceptions and log them. Do NOT re-raise the exception.

def auto_decorate_methods_with(decorator, exclude: Optional[List[str]] = []):
22def auto_decorate_methods_with(decorator, exclude: Optional[List[str]] = []):
23    """Class decorator to automatically apply a given decorator to all
24    methods of a class.
25    """
26
27    def class_decorator(cls):
28        for attr_name, attr_value in cls.__dict__.items():
29            if attr_name in exclude:
30                continue
31            if callable(attr_value):
32                # Wrap callable attributes (methods) with the decorator
33                setattr(cls, attr_name, decorator(attr_value))
34            elif isinstance(attr_value, classmethod):
35                # Special handling for classmethods
36                original_method = attr_value.__func__
37                decorated_method = decorator(original_method)
38                setattr(cls, attr_name, classmethod(decorated_method))
39            elif isinstance(attr_value, staticmethod):
40                # Special handling for staticmethods
41                original_method = attr_value.__func__
42                decorated_method = decorator(original_method)
43                setattr(cls, attr_name, staticmethod(decorated_method))
44        return cls
45
46    return class_decorator

Class decorator to automatically apply a given decorator to all methods of a class.