PHP Classes

File: docs/files/FileLogger.php.txt

Recommend this page to a friend!
  Classes of Kiril Savchev   ITE Logger   docs/files/FileLogger.php.txt   Download  
File: docs/files/FileLogger.php.txt
Role: Documentation
Content type: text/plain
Description: Documentation
Class: ITE Logger
Log messages to different storage PSR-3 compliant
Author: By
Last change:
Date: 7 years ago
Size: 4,475 bytes
 

Contents

Class file image Download
<?php /** * FileLogger file * * Copyright (c) 2016, Kiril Savchev * All rights reserved. * * @category Libs * @package Logger * * @author Kiril Savchev <k.savchev@gmail.com> * * @license https://opensource.org/licenses/BSD-3-Clause BSD 3 License * @link http://ifthenelse.info */ namespace Ite\Logger; use Psr\Log\AbstractLogger; use Ite\Logger\Exception\InvalidArgumentException; /** * FileLogger * * Writes log messages into files with specific levels * * @uses psr\log * * @usage $fileLogger->error("Error message") * @usage $fileLogger->debug("Debug info..."); * @usage $fileLogger->setLogLevel("custom_level", "logs/custom_file.log"); * @usage $fileLogger->log("custom_level", "Custom log message"); * * @version 1.0 * * @author Kiril Savchev <k.savchev@gmail.com> */ class FileLogger extends AbstractLogger { use MessagePrepareTrait; /** * Arrays with log files and their file handlers * * @var array */ protected $resources = []; /** * Log level files map * * @var array */ protected $logFiles = []; /** * The default log file * * @var string */ protected $defaultLog; /** * File logger object * * @param array $logFiles [Optional] * @param string $defaultLog [Optional] * @param string $dateFormat [Optional] */ public function __construct(array $logFiles = [], $defaultLog = '../data/logs/logger.log', $dateFormat = 'Y-m-d H:i:s') { $this->logFiles = $logFiles; $this->defaultLog = $defaultLog; $this->dateFormat = $dateFormat; } /** * Sets default log file * * @param string $defaultLog * @return \Ite\Logger\FileLogger */ public function setDefaultLog($defaultLog) { $this->defaultLog = $defaultLog; return $this; } /** * Adds new log level * * @param string $level * @param string $file * @return \Ite\Logger\FileLogger */ public function setLogLevel($level, $file = '') { if (!$file) { $file = $this->defaultLog; } $this->logFiles[$level] = $file; return $this; } /** * Initialize the log file handler * * @param string $level * @return boolean * @throws InvalidArgumentException */ protected function initLog($level) { if (!array_key_exists($level, $this->logFiles)) { $this->logFiles[$level] = $this->defaultLog; } $file = $this->logFiles[$level]; if (array_key_exists($file, $this->resources) && is_resource($this->resources[$file])) { return true; } if (!file_exists($file)) { throw new InvalidArgumentException("Log file {$file} does not exists"); } if (!is_writable($file)) { throw new InvalidArgumentException("Log file {$file} is not writable"); } return (bool) $this->resources[$file] = fopen($file, 'a'); } /** * Do the actual logging * * @param string $level * @param string $message * @param array $context [Optional] * @throws InvalidArgumentException */ public function log($level, $message, array $context = []) { $this->initLog($level); fwrite($this->resources[$this->logFiles[$level]], $this->prepareMessage($message, $context).PHP_EOL); } /** * Get default log file * * @return string */ public function getDefaultLog() { return $this->defaultLog; } /** * Class destructor * * Closes all opended file handler */ public function __destruct() { foreach ($this->resources as $resource) { if (is_resource($resource)) { fclose($resource); } } } }