Add Monolog
This commit is contained in:
parent
1d61645a16
commit
fe8f0e0045
15 changed files with 363 additions and 121 deletions
29
src/App.php
29
src/App.php
|
@ -8,8 +8,10 @@ use Detection\MobileDetect;
|
|||
use DOMDocument;
|
||||
use DOMXPath;
|
||||
use Exception;
|
||||
use Friendica\Core\Logger;
|
||||
use Friendica\Database\DBA;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Monolog;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -106,6 +108,11 @@ class App
|
|||
*/
|
||||
public $mobileDetect;
|
||||
|
||||
/**
|
||||
* @var Monolog\Logger The current logger of this App
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* Register a stylesheet file path to be included in the <head> tag of every page.
|
||||
* Inclusion is done in App->initHead().
|
||||
|
@ -147,12 +154,15 @@ class App
|
|||
* @brief App constructor.
|
||||
*
|
||||
* @param string $basePath Path to the app base folder
|
||||
* @param Monolog\Logger Logger of this application
|
||||
* @param bool $isBackend Whether it is used for backend or frontend (Default true=backend)
|
||||
*
|
||||
* @throws Exception if the Basepath is not usable
|
||||
*/
|
||||
public function __construct($basePath, $isBackend = true)
|
||||
public function __construct($basePath, $logger, $isBackend = true)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
|
||||
if (!static::isDirectoryUsable($basePath, false)) {
|
||||
throw new Exception('Basepath ' . $basePath . ' isn\'t usable.');
|
||||
}
|
||||
|
@ -301,6 +311,21 @@ class App
|
|||
return $this->mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Logger of the Application
|
||||
*
|
||||
* @return Monolog\Logger The Logger
|
||||
* @throws InternalServerErrorException when the logger isn't created
|
||||
*/
|
||||
public function getLogger()
|
||||
{
|
||||
if (empty($this->logger)) {
|
||||
throw new InternalServerErrorException('Logger of the Application is not defined');
|
||||
}
|
||||
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the whole app instance
|
||||
*/
|
||||
|
@ -328,6 +353,8 @@ class App
|
|||
Core\L10n::init();
|
||||
|
||||
$this->process_id = Core\System::processID('log');
|
||||
|
||||
Logger::loadDefaultHandler($this->logger, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
48
src/App/FriendicaLoggerProcessor.php
Normal file
48
src/App/FriendicaLoggerProcessor.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\App;
|
||||
|
||||
use Monolog\Processor\ProcessorInterface;
|
||||
|
||||
/**
|
||||
* Includes the Friendica specific process_id of "app->process_id"
|
||||
*
|
||||
* @package Friendica\App
|
||||
*/
|
||||
class FriendicaLoggerProcessor implements ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* @var string the ID of the current Friendica process
|
||||
*/
|
||||
private $processId = null;
|
||||
|
||||
/**
|
||||
* Set the process id based on the Application instance
|
||||
*
|
||||
* @param string $processId the process id
|
||||
*/
|
||||
public function setProcessId($processId)
|
||||
{
|
||||
if (!isset($this->processId) || $this->processId == '')
|
||||
{
|
||||
$this->processId = $processId;
|
||||
}
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->processId = session_id();
|
||||
}
|
||||
|
||||
public function __invoke(array $records)
|
||||
{
|
||||
$records['extra'] = array_merge(
|
||||
$records['extra'],
|
||||
[
|
||||
'app_id' => $this->processId,
|
||||
]
|
||||
);
|
||||
|
||||
return $records;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,10 @@
|
|||
*/
|
||||
namespace Friendica;
|
||||
|
||||
require_once 'boot.php';
|
||||
|
||||
use Friendica\Core\Logger;
|
||||
|
||||
/**
|
||||
* Basic object
|
||||
*
|
||||
|
@ -23,7 +27,8 @@ class BaseObject
|
|||
public static function getApp()
|
||||
{
|
||||
if (empty(self::$app)) {
|
||||
self::$app = new App(dirname(__DIR__));
|
||||
$logger = Logger::create('app');
|
||||
self::$app = new App(dirname(__DIR__), $logger);
|
||||
}
|
||||
|
||||
return self::$app;
|
||||
|
|
|
@ -4,23 +4,72 @@
|
|||
*/
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\BaseObject;
|
||||
use Friendica\Core\Config;
|
||||
use Monolog;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* @brief Logger functions
|
||||
*/
|
||||
class Logger extends BaseObject
|
||||
{
|
||||
/**
|
||||
* Creates a basic Monolog instance for logging.
|
||||
*
|
||||
* @param string $application the current application name (index, daemon, ...)
|
||||
*
|
||||
* @return Monolog\Logger The Logger instance
|
||||
*/
|
||||
public static function create($application)
|
||||
{
|
||||
$logger = new Monolog\Logger($application);
|
||||
|
||||
$logger->pushProcessor(new Monolog\Processor\IntrospectionProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\WebProcessor());
|
||||
$logger->pushProcessor(new App\FriendicaLoggerProcessor());
|
||||
|
||||
return $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default Logging handler for this instance.
|
||||
* Can be combined with other handlers too if necessary.
|
||||
*
|
||||
* @param Monolog\Logger $logger The Logger instance of this Application
|
||||
* @param App $app The Friendica Application
|
||||
*/
|
||||
public static function loadDefaultHandler($logger, $app)
|
||||
{
|
||||
foreach ($logger->getProcessors() as $processor) {
|
||||
if ($processor instanceof App\FriendicaLoggerProcessor) {
|
||||
$processor->setProcessId($app->process_id);
|
||||
}
|
||||
}
|
||||
|
||||
$debugging = Config::get('system', 'debugging');
|
||||
$logfile = Config::get('system', 'logfile');
|
||||
$loglevel = intval(Config::get('system', 'loglevel'));
|
||||
|
||||
if (!$debugging || !$logfile) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fileHandler = new Monolog\Handler\StreamHandler($logfile . ".1", $loglevel);
|
||||
$logger->pushHandler($fileHandler);
|
||||
}
|
||||
|
||||
// Log levels:
|
||||
const WARNING = 0;
|
||||
const INFO = 1;
|
||||
const TRACE = 2;
|
||||
const DEBUG = 3;
|
||||
const DATA = 4;
|
||||
const ALL = 5;
|
||||
//EMERGENCY
|
||||
//ALERT
|
||||
//CRITICAL
|
||||
const WARNING = 0; //ERROR
|
||||
const INFO = 1; //WARNING
|
||||
const TRACE = 2; //NOTICE(default)
|
||||
const DEBUG = 3; //INFO
|
||||
const DATA = 4; //INFO
|
||||
const ALL = 5; //DEBUG
|
||||
|
||||
public static $levels = [
|
||||
self::WARNING => 'Warning',
|
||||
|
@ -36,6 +85,8 @@ class Logger extends BaseObject
|
|||
*
|
||||
* @param string $msg
|
||||
* @param int $level
|
||||
*
|
||||
* @deprecated since 2019.03 - use App->getLogger() instead
|
||||
*/
|
||||
public static function log($msg, $level = self::INFO)
|
||||
{
|
||||
|
@ -90,6 +141,8 @@ class Logger extends BaseObject
|
|||
* personally without background noise
|
||||
*
|
||||
* @param string $msg
|
||||
*
|
||||
* * @deprecated since 2019.03 - never used
|
||||
*/
|
||||
public static function devLog($msg)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue