FileHandler.php 842 Bytes
<?php

namespace common\components\dlog;

use yii\base\InvalidConfigException;
use function fopen;
use function fwrite;
use function fclose;

/**
 * Class FileHandler
 * @package common\components\dlogs
 */
class FileHandler implements Handler
{
    /**
     * @var null|resource
     */
    private $handle = null;

    /**
     * @param string $file
     * @throws InvalidConfigException
     */
    public function __construct($file = '')
    {
        if ( false === ($this->handle  = @fopen($file, 'a')) ) {
            throw new InvalidConfigException("Unable to append to log file: {$file}");
        }
    }

    /**
     * @param $msg
     */
    public function write($msg)
    {
        @fwrite($this->handle, $msg, 4096);
    }

    /**
     *
     */
    public function __destruct()
    {
        @fclose($this->handle);
    }
}