<?php declare(strict_types=1);

namespace Apexl\Io\Logger;

use Apexl\Io\Logger\Interfaces\LoggerInterface;
use Apexl\Io\Logger\Services\ErrorLogLogger;
use Apexl\Io\Logger\Services\LoggerStore;

if (!function_exists(__NAMESPACE__ . '\\logger')) {
    function logger(string $stream = LoggerStore::DEFAULT__LOGGER_STREAM): LoggerInterface
    {
        try {
            return \Apexl\Io\container()->get(LoggerStore::class)->logger($stream);
        } catch (Throwable $e) {
            error_log(
                sprintf(
                    'Error fetching logger; falling back to error_log(): %s',
                    $e->getMessage()
                )
            );

            return new ErrorLogLogger();
        }
    }
}
