fixed monolog errors

This commit is contained in:
Taras Machyshyn
2014-03-20 18:06:35 +02:00
parent b5093dccf2
commit 8b9538f118
6 changed files with 250 additions and 36 deletions
-2
View File
@@ -1,7 +1,5 @@
<?php
error_reporting(0);
require_once('../../bootstrap.php');
$app = new \Espo\Core\Application();
+13 -10
View File
@@ -1,4 +1,4 @@
<?php
<?php
/************************************************************************
* This file is part of EspoCRM.
*
@@ -18,12 +18,12 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
namespace Espo\Core\Loaders;
use Espo\Core\Utils;
use Monolog\Handler;
use Espo\Core\Utils,
Espo\Core\Utils\Log\Monolog\Handler;
class Log
{
@@ -41,12 +41,11 @@ class Log
public function load()
{
$config = $this->getContainer()->get('config');
$logConfig = $this->getContainer()->get('config')->get('logger');
$logConfig = $config->get('logger');
$log = new Utils\Log('Espo');
$levelCode = $log->getLevelCode($logConfig['level']);
$log = new Utils\Log('Espo');
$levelCode = $log->getLevelCode($logConfig['level']);
if ($logConfig['isRotate']) {
$handler = new Handler\RotatingFileHandler($logConfig['path'], $logConfig['maxRotateFiles'], $levelCode);
@@ -54,7 +53,11 @@ class Log
$handler = new Handler\StreamHandler($logConfig['path'], $levelCode);
}
$log->pushHandler($handler);
\Monolog\ErrorHandler::register($log);
$errorHandler = new \Monolog\ErrorHandler($log);
$errorHandler->registerExceptionHandler(null, false);
$errorHandler->registerErrorHandler(array(), false);
$errorHandler->registerFatalHandler();
return $log;
}
+5 -24
View File
@@ -1,4 +1,4 @@
<?php
<?php
/************************************************************************
* This file is part of EspoCRM.
*
@@ -18,32 +18,13 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
namespace Espo\Core\Utils;
class Log extends \Monolog\Logger
use Espo\Core\Utils\Log\Monolog\Logger;
class Log extends Logger
{
protected $defaultLevelName = 'DEBUG';
/**
* Get Level Code
* @param string $level Ex. DEBUG, ...
* @return int
*/
public function getLevelCode($levelName)
{
$levelName = strtoupper($levelName);
$levels = $this->getLevels();
if (isset($levels[$levelName])) {
return $levels[$levelName];
}
return $levels[$this->defaultLevelName];
}
}
@@ -0,0 +1,117 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core\Utils\Log\Monolog\Handler;
use Monolog\Logger;
class RotatingFileHandler extends StreamHandler
{
/**
* Date format as a part of filename
* @var string
*/
protected $dateFormat = 'Y-m-d';
/**
* Filename format
* @var string
*/
protected $filenameFormat = '{filename}-{date}';
protected $filename;
protected $maxFiles;
public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true)
{
$this->filename = $filename;
$this->maxFiles = (int) $maxFiles;
parent::__construct($this->getTimedFilename(), $level, $bubble);
$this->rotate();
}
public function setFilenameFormat($filenameFormat, $dateFormat)
{
$this->filenameFormat = $filenameFormat;
$this->dateFormat = $dateFormat;
}
protected function rotate()
{
if (0 === $this->maxFiles) {
return; //unlimited number of files for 0
}
$filePattern = $this->getFilePattern();
$dirPath = $this->getFileManager()->getDirName($this->filename);
$logFiles = $this->getFileManager()->getFileList($dirPath, false, $filePattern, 'file');
if (!empty($logFiles) && count($logFiles) > $this->maxFiles) {
usort($logFiles, function($a, $b) {
return strcmp($b, $a);
});
$logFilesToBeRemoved = array_slice($logFiles, $this->maxFiles);
$this->getFileManager()->removeFiles($logFilesToBeRemoved, $dirPath);
}
}
protected function getTimedFilename()
{
$fileInfo = pathinfo($this->filename);
$timedFilename = str_replace(
array('{filename}', '{date}'),
array($fileInfo['filename'], date($this->dateFormat)),
$fileInfo['dirname'] . '/' . $this->filenameFormat
);
if (!empty($fileInfo['extension'])) {
$timedFilename .= '.'.$fileInfo['extension'];
}
return $timedFilename;
}
protected function getFilePattern()
{
$fileInfo = pathinfo($this->filename);
$glob = str_replace(
array('{filename}', '{date}'),
array($fileInfo['filename'], '.*'),
$this->filenameFormat
);
if (!empty($fileInfo['extension'])) {
$glob .= '\.'.$fileInfo['extension'];
}
$glob = '^'.$glob.'$';
return $glob;
}
}
@@ -0,0 +1,66 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core\Utils\Log\Monolog\Handler;
use Monolog\Logger;
class StreamHandler extends \Monolog\Handler\StreamHandler
{
protected $fileManager;
public function __construct($url, $level = Logger::DEBUG, $bubble = true)
{
parent::__construct($url, $level, $bubble);
$this->fileManager = new \Espo\Core\Utils\File\Manager();
}
protected function getFileManager()
{
return $this->fileManager;
}
protected function write(array $record)
{
if (!$this->url) {
throw new \LogicException('Missing logger path, the stream can not be opened. Please check logger options in the data/config.php.');
}
$this->errorMessage = null;
set_error_handler(array($this, 'customErrorHandler'));
$this->getFileManager()->appendContents($this->url, (string) $record['formatted']);
restore_error_handler();
if (isset($this->errorMessage)) {
throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
}
}
private function customErrorHandler($code, $msg)
{
$this->errorMessage = $msg;
}
}
@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
namespace Espo\Core\Utils\Log\Monolog;
class Logger extends \Monolog\Logger
{
protected $defaultLevelName = 'DEBUG';
/**
* Get Level Code
* @param string $level Ex. DEBUG, ...
* @return int
*/
public function getLevelCode($levelName)
{
$levelName = strtoupper($levelName);
$levels = $this->getLevels();
if (isset($levels[$levelName])) {
return $levels[$levelName];
}
return $levels[$this->defaultLevelName];
}
}