PHP Classes

File: src/FileSystemLogger.php

Recommend this page to a friend!
  Classes of Vitaly   QuEasy PHP PSR Logger   src/FileSystemLogger.php   Download  
File: src/FileSystemLogger.php
Role: Class source
Content type: text/plain
Description: Class source
Class: QuEasy PHP PSR Logger
Log messages to containers compliant with PSR-3
Author: By
Last change:
Date: 3 years ago
Size: 1,422 bytes
 

Contents

Class file image Download
<?php

/*
 * Queasy PHP Framework - Logger
 *
 * (c) Vitaly Demyanenko <vitaly_demyanenko@yahoo.com>
 *
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

namespace queasy\log;

/**
 * File system logger
 */
class FileSystemLogger extends Logger
{
    const
DEFAULT_PATH = 'debug.log';

    private
$path;

    public function
__construct($config = null, $setErrorHandlers = true)
    {
       
parent::__construct($config, $setErrorHandlers);

       
$this->path = getcwd() . DIRECTORY_SEPARATOR . (isset($this->config['path'])? $this->config['path']: static::DEFAULT_PATH);
    }

   
/**
     * File system log method.
     *
     * @param string $level Log level
     * @param string $message Log message
     * @param array|null $context Context
     */
   
public function log($level, $message, array $context = array())
    {
       
$preparedMessage = $this->prepareMessage($level, $message, $context);

       
$path = isset($this->config['timeLabel'])? sprintf($this->path(), date($this->config['timeLabel'])): $this->path();

       
file_put_contents($path, $preparedMessage . PHP_EOL, FILE_APPEND | LOCK_EX);

        return
parent::log($level, $message, $context);
    }

   
/**
     * Get log file path
     *
     * @return string Log file path
     */
   
protected function path()
    {
        return
$this->path;
    }
}