namespace Logger;

/// <summary>
/// Logger that writes to multiple loggers simultaneously
/// </summary>
public class CompositeLogger : ILogger
{
    private readonly List<ILogger> _loggers;

    public CompositeLogger(params ILogger[] loggers)
    {
        _loggers = new List<ILogger>(loggers ?? Array.Empty<ILogger>());
    }

    public void AddLogger(ILogger logger)
    {
        if (logger != null)
            _loggers.Add(logger);
    }

    public void RemoveLogger(ILogger logger)
    {
        _loggers.Remove(logger);
    }

    public void Trace(string message) => Log(LogLevel.Trace, message);
    public void Debug(string message) => Log(LogLevel.Debug, message);
    public void Info(string message) => Log(LogLevel.Info, message);
    public void Warning(string message) => Log(LogLevel.Warning, message);
    public void Error(string message) => Log(LogLevel.Error, message);
    public void Fatal(string message) => Log(LogLevel.Fatal, message);

    public void Log(LogLevel level, string message)
    {
        foreach (var logger in _loggers)
        {
            try
            {
                logger.Log(level, message);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Error in logger: {ex.Message}");
            }
        }
    }
}
