Mayil-SB-Internal-Prod / loguru-II

MIT License
0 stars 0 forks source link

How to inspect sinks? (hashtag1024) #74

Open vikramsubramanian opened 6 months ago

vikramsubramanian commented 6 months ago

hashtag in which situation do I need this

The loguru configuration of our project start to be complex, and programmatically configured. I arrive to the point where I need to inspect the configuration of all the sinks at a given point in the code, to see if the configuration values do produce the logging configuration I wish.

hashtag example needs

mayil-ai[bot] commented 6 months ago

To inspect the configuration of all the sinks in Loguru, you can follow these steps:

Here is a pseudo-code example of how you might track and inspect sink configurations:

# Global list to keep track of sinks and their configurations
sinks_list = []

# Function to add a sink and store its configuration
def add_sink(sink, **kwargs):
    sink_id = logger.add(sink, **kwargs)
    sink_config = {
        'id': sink_id,
        'sink': sink,
        'config': kwargs
    }
    sinks_list.append(sink_config)
    return sink_id

# Function to inspect all active sinks
def inspect_sinks():
    for sink_config in sinks_list:
        print(f"Sink ID: {sink_config['id']}")
        print(f"Sink: {sink_config['sink']}")
        print(f"Configuration: {sink_config['config']}")

# Example usage
add_sink(sys.stdout, format="{time} - {level} - {message}")
add_sink("file.log", rotation="1 week", level="INFO")

# Later in the code, you can inspect the sinks
inspect_sinks()

Remember that this is a workaround and not a built-in feature of Loguru. You will need to ensure that your add_sink function is used in place of logger.add() throughout your codebase to maintain an accurate list of sinks and their configurations.