subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.38k stars 685 forks source link

Client.logger access #2045

Closed orazdow closed 1 month ago

orazdow commented 1 month ago

Is it possible intercept error response messages using the cl object's logger?

sb0y commented 1 month ago

Try to do this

cl = Client()
cl.logger.setLevel(logging.DEBUG)
orazdow commented 1 month ago

Is the best way to get the error string from the logger to add a handler I guess?

sb0y commented 1 month ago

for such a task you need something like this

import logging
from instagrapi import Client

# Custom handler to intercept log messages
class ErrorInterceptHandler(logging.Handler):
    def emit(self, record):
        if record.levelno >= logging.ERROR:
            # Intercept the error log here (do whatever you need with it)
            print(f"Intercepted error: {record.getMessage()}")

# Configure logging
cl = Client()
cl.logger.setLevel(logging.DEBUG)

# Add our custom handler
error_handler = ErrorInterceptHandler()
cl.logger.addHandler(error_handler)

https://docs.python.org/3/library/logging.html

orazdow commented 1 month ago

Ok thanks