<?php

namespace Dipper\Foundation\Debugger\Queries;

use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;

/**
 * Class QueriesLogger
 */
class QueriesLogger
{
    protected $debuggingEnabled;

    protected $logger;

    protected $logFile;

    /**
     * QueriesLogger constructor.
     */
    public function __construct()
    {
        $this->prepareConfigs();
        $this->prepareLogger();
    }

    /**
     * @param \Dipper\Foundation\Debugger\Objects\Output $output
     */
    public function releaseOutput(Output $output)
    {
        if ($this->debuggingEnabled === true) {
            $this->logger->info($output->get());
        }
    }

    /**
     * @void
     */
    private function prepareConfigs()
    {
        $this->debuggingEnabled = config('dipper.debugger.queries.debug');
        $this->logFile = config('dipper.debugger.queries.log_file');
    }

    /**
     * @void
     */
    private function prepareLogger()
    {
        $handler = new StreamHandler(storage_path('logs/' . $this->logFile));
        $handler->setFormatter(new LineFormatter(null, null, true, true));

        $this->logger = new Logger('QUERIES DEBUGGER');
        $this->logger->pushHandler($handler, Logger::INFO);
    }
}
