muscula/muscula-php-logger

PHP library for sending errors to Muscula monitoring system

dev-master 2021-01-20 14:31 UTC

This package is auto-updated.

Last update: 2021-04-20 14:56:59 UTC


README

Muscula PHP Logger allows you to send all your PHP errors to Muscula service. Code is completely based on great PHP Console library https://github.com/barbushin/muscula

Requirements

  • PHP 5.4 (or later)

Installation

Composer

{
	"require": {
		"muscula/muscula-php-logger": "^1.0"
	}
}

Or

$ composer require muscula/muscula-php-logger

Usage

Connector

There is a Muscula\Connector class that initializes connection between PHP server and Musucla system. Connection is initialized when Muscula\Connector instance is initialized:

$connector = Muscula\Connector::getInstance();
$connector->logId = 'LOG_ID';

Also it will be initialized when you call Muscula\Handler::getInstance() or Muscula\Helper::register().

Handle errors

There is a Muscula\Handler class that initializes PHP errors & exceptions handlers and provides the next features:

  • Handle PHP errors (+fatal & memory limit errors) and exceptions.
  • Ignore repeated errors.
  • Call previously defined errors and exceptions handlers.
  • Handle caught exceptions using $handler->handleException($exception).
  • Debug vars using $handler->debug($var, 'some.tags').

Initialize Muscula\Handler in the top of your main project script:

$handler = Muscula\Handler::getInstance();
/* You can override default Handler behavior:
	$handler->setHandleErrors(false);  // disable errors handling
	$handler->setHandleExceptions(false); // disable exceptions handling
	$handler->setCallOldHandlers(false); // disable passing errors & exceptions to prviously defined handlers
*/
$handler->start($logId); // initialize handlers, provide logId

Debug vars

Muscula has multifunctional and smart vars dumper that allows to

  • Dump any type variable.
  • Dump protected and private objects properties.
  • Limit dump by level, items count, item size and total size(see $connector->getDumper()).
  • Dump objects class name.
  • Smart dump of callbacks and Closure.
  • Detect dump call source & trace(call $connector->getDebugDispatcher()->detectTraceAndSource = true).

How to call

Longest native debug method call:

Muscula\Connector::getInstance()->getDebugDispatcher()->dispatchDebug($var, 'some.tags');

Shorter call debug from Handler:

Muscula\Handler::getInstance()->debug($var, 'some.tags');

Shortest call debug using global M class

Muscula\Helper::register(); // it will register global M class
// ...
M::debug($var, 'tag');
M::tag($var);

Custom call debug by user defined function

function d($var, $tags = null) {
	Muscula\Connector::getInstance()->getDebugDispatcher()->dispatchDebug($var, $tags, 1);
}
d($var, 'some.tags');

Configuration

$connector = Muscula\Connector::getInstance();
$connector->logId = 'LOG_ID';

// Configure eval provider
$evalProvider = $connector->getEvalDispatcher()->getEvalProvider();
$evalProvider->addSharedVar('post', $_POST); // so "return $post" code will return $_POST
$evalProvider->setOpenBaseDirs(array(__DIR__)); // see http://php.net/open-basedir

$connector->startEvalRequestsListener(); // must be called in the end of all configurations

PSR-3 logger implementation

There is Muscula implementation of PSR-3 interface. to integrate Muscula with PSR-3 compitable loggers(e.g. Monolog). See Muscula\PsrLogger.