<?php

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\FirePHPHandler;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;

class BillDeskLogger {
    const DEFAULT_CHANNEL = 'default';

    public static function logger($channel = self::DEFAULT_CHANNEL) {
        $logger = new Logger($channel);

        // Use PHP constant or fallback
        $isDebug = defined('ZEN_DEBUG') ? ZEN_DEBUG : false;
        $level   = $isDebug ? Logger::DEBUG : Logger::INFO;

        // Make sure the logs directory exists
        $logDir = DIR_FS_LOGS . '/Billdesk';
        if (!is_dir($logDir)) {
            @mkdir($logDir, 0775, true);
        }

        $fileStream = new RotatingFileHandler(
            $logDir . '/zencart_billdesk.log',
            10, // keep 10 rotated files
            $level
        );

        $dateFormat = "Y-m-d H:i:s e";
        $output = "%datetime% [%channel%.%level_name%] %message% - %context% %extra%\n";
        $formatter = new LineFormatter($output, $dateFormat);
        $fileStream->setFormatter($formatter);

        if ($isDebug) {
            $logger->pushHandler(new FirePHPHandler());
        }

        $logger->pushProcessor(new IntrospectionProcessor());
        $logger->pushHandler($fileStream);

        return $logger;
    }

    public static function defaultLogger() {
        return self::logger(self::DEFAULT_CHANNEL);
    }
}
