PHPackages                             aaronsaray/phproblemlogger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. aaronsaray/phproblemlogger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

aaronsaray/phproblemlogger
==========================

PHP Problem Logger: Exception and Error Logging with full runtime information.

1.1.2(9y ago)243MITPHPPHP &gt;=5.5.0

Since Jul 11Pushed 9y ago2 watchersCompare

[ Source](https://github.com/aaronsaray/PHProblemLogger)[ Packagist](https://packagist.org/packages/aaronsaray/phproblemlogger)[ Docs](https://github.com/aaronsaray/PHProblemLogger)[ RSS](/packages/aaronsaray-phproblemlogger/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (5)Used By (0)

PHProblemLogger
===============

[](#phproblemlogger)

[![Build Status](https://camo.githubusercontent.com/22978f74b6566c57290b83b7482fc036b2521685ddb889e1334acda4d323bcee/68747470733a2f2f7472617669732d63692e6f72672f6161726f6e73617261792f504850726f626c656d4c6f676765722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/aaronsaray/PHProblemLogger)[![Coverage Status](https://camo.githubusercontent.com/5fb7a0a4dfa7024fa9b19a9f727d5df17fa8ed43b3e3611127c2cfd9a1066404/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6161726f6e73617261792f504850726f626c656d4c6f676765722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/aaronsaray/PHProblemLogger?branch=master)

This tool helps create a better log of the current environment when a problem happens in PHP - like an Exception or an Error. Exceptions are handled by an exception handler - and errors are converted into a custom ErrorException and thrown.

Installation Instructions
-------------------------

[](#installation-instructions)

Install the latest version with

```
$ composer require aaronsaray/phproblemlogger
```

Documentation
-------------

[](#documentation)

### Getting Started

[](#getting-started)

This library will add on to the existing uncaught exception and error handler using a PSR-3 compatible logging instance for output of the current environment. The configuration of this logger is *additive* meaning that out of the box nothing is logged during a problem. You must configure filters to indicate what you'd like to be logged.

All uncaught exceptions are processed using this library. Error types of E\_ALL | E\_STRICT are turned into a custom Exception and processed.

To use this tool, have an instance of a PSR-3 logger available and at least one filter callable ready.

**Important** If you have your own exception handling in place (like a redirect or a nice view), create a new instance of PHProblemLogger **after** you've defined your handler. PHProblemLogger keeps a reference to the previous handler and will call it after it after processing finishes. This does **not** apply to the error handler. This is overwritten and errors are converted into an `ErrorException`.

### An Example

[](#an-example)

In this example, we want to log `$_SERVER` and `$_COOKIE` variables to our log file whenever there is an exception.

```
use AaronSaray\PHProblemLogger\Handler as Handler;
use AaronSaray\PHProblemLogger\Handler as HandlerFilter;

$monolog = $monolog; // this is an instance of a PSR-3 logger interface

$problemHandler = new Handler($monolog);
$problemHandler
    ->server(function(array $payload) {
        return $payload;
    })
    ->cookie(HandlerFilter::all());
```

To create the new instance of the problem handler, create a new instance of the class and pass in a `LoggerInterface` class as the first parameter of the constructor. This will automatically add PHPProblemHandler to the stack of error and exception handlers in your application. Then, for each portion of the runtime environment that we want, we pass a callable function to it.

First, in order to get all `$_SERVER` variables, we call -&gt;server() with a closure that takes a payload of what is the `$_SERVER`variable and allows us to edit it. In this case, we just return the entire array.

Next, when filtering the `$_COOKIE` super global, we call the helper function `all()` of the HandlerFilter.
This is essentially returns a function that is the same as the closure we wrote. It's just used to save time.

Now, any exception or error will write to the logger with the entire contents of the `$_SERVER` and the `$_COOKIE` variables.
Remember, since we didn't add any other filters, items like `$_GET` and `$_POST` will not be logged.

### Filter Functions

[](#filter-functions)

Filter functions require a PHP Callable to be passed as their only parameter. This callable will receive an array of the payload variable. The return type should be an array or null. `NULL` indicates that this portion should not be logged. You may return the entire array unaltered, alter it, or completely replace it (not recommended).

You may want to alter the values when there are instances that deal with secure values, like a credit card number in a `$_POST`or database connection variables in your `$_ENV`.

`Handler::session` - access to `$_SESSION`

`Handler::get` - access to `$_GET`

`Handler::post` - access to `$_POST`

`Handler::cookie` - access to `$_COOKIE`

`Handler::environment` - access to `$_ENV`

`Handler::server` - access to `$_SERVER`

`Handler::application` - empty array to add custom application values to

### Built-in Filter Callables

[](#built-in-filter-callables)

To save time, there are two helper methods that return filter callables.

`HandlerFilter::all` - returns a filter that returns the entire payload unaltered

`HandlerFilter::none` - returns a filter that returns `null`, making sure that the variable is not logged

### Cookbooks

[](#cookbooks)

For the following cookbooks, we're assuming that the `$handler` variable is an instance of this library with a valid logger injected.

**Log `$_SERVER` only if running from web server**

```
$handler->server(function(array $payload) {
  return php_sapi_name() != 'cli' ? $payload : null;
});
```

**Mask a credit card number in `$_POST` by the key 'cc\_num'**

```
$handler->post(function(array $payload) {
  if (array_key_exists('cc_num', $payload)) {
    $payload['cc_num'] = str_pad(substr($payload['cc_num'], -4), strlen($payload['cc_num']), '*', STR_PAD_LEFT);
  }
  return $payload;
});
```

**Conditionally do not log `$_SESSION` based on an application choice**

```
use AaronSaray\PHProblemLogger\Handler as HandlerFilter;

$handler->session(HandlerFilter::all());

if (someFunctionIsTrue()) {
  $handler->session(HandlerFilter::none());
}
```

**Log user information from your session without a complex application / DI solution**

```
$handler->application(function(array $payload) {
  if (isset($_SESSION['user'])) {
    $payload['user'] = $_SESSION['user'];
  }
  return $payload;
});
```

**Log detailed user information within an application with Dependency Injection**

```
class MyUserErrorFilter
{
  protected $authenticationProvider;

  public function __construct($authenticationProvider)
  {
    $this->authenticationProvider = $authenticationProvider;
  }

  public function __invoke(array $payload)
  {
    if ($this->authenticationProvider->isLoggedIn()) {
       $payload['authenticationInfo'] = $this->authenticationProvider->getAuthenticationInfo();
    }
    return $payload;
  }
}

$handler->application(new MyUserErrorFilter($yourAuthenticationProviderInstance));
```

**Add additional useful information like memory usage**

```
$handler->application(function(array $payload) {
  $payload['memory_usage'] = memory_get_usage();
  $payload['pid'] = getmypid();
  $payload['resource_usage'] = getrusage();
  return $payload;
});
```

**Use PHProblemLogger exception logging tools even in caught exception**

*Keep in mind: if you have your own exception handler defined before PHProblemLogger, this will kick off yours as well.*

```
try {
  somethingCausesException();
}
catch (\Exception $e) {
  // some custom programming here
  $handler->handleException($e);
}
```

### Troubleshooting

[](#troubleshooting)

**Session variables are not being logged.**
Make sure that you are calling `session_start()` somewhere before the error happens. This library will not attempt to start a session just to report on the content of it.

**My own custom exception handler is not firing**
Make sure that you've made a new instance of `Handler` **after** you've defined your custom exception handler. If you're using any other sort of exception handler queueing system, this may not work.

**I am not seeing any of the logs from PHProblemLogger in the output**
This could be happening for a number of reasons. First, note that the log level of `ERROR` is being used to log the exceptions and errors. Verify that your log writer is configured to write that log level. Second, you may want to verify that PHProblemHandler's exception handler hasn't been removed. When you call `set_exception_handler` it returns the previous exception handler. Use a debugger to validate that the exception handler is still set. A quick and dirty solution is to use the following code to verify this: `var_dump(set_exception_handler(function(){}));` - which should return a callable that reflects the PHProblemHandler function of `Handler::handleException`.

Todo
----

[](#todo)

- TravisCI for 7.x and HHVM
    Currently, this is failing because of the independent process tests and not finding a code coverage file and/or a PharException.

About
-----

[](#about)

There are a lot of systems out there to handle the display of errors - and some more complex solutions (like Zend Server) to handle gathering the entire environment during an error condition. However, there is nothing really in-between that - something that logs errors and the runtime environment - without relying on a larger, enterprise-level solution or third-party.

I was having some weird errors that I really needed to know more about the environment to troubleshoot, so I decided to add something like this to my application. I then created it as an open source project - hopefully it will help you out!

### Requirements

[](#requirements)

- PHP 5.5+

### Bugs and Feature Requests

[](#bugs-and-feature-requests)

Bugs and feature request are tracked on [GitHub](https://github.com/aaronsaray/phproblemlogger/issues)

Run tests by executing `composer tests` in the root of the project.

### Author

[](#author)

Aaron Saray -

### License

[](#license)

This library is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

4

Last Release

3583d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5a9ad2a426eb84bbb6d5fd7f8a9000797796581c2aeda0d27b39391e55c0e7a3?d=identicon)[aaronsaray](/maintainers/aaronsaray)

---

Top Contributors

[![aaronsaray](https://avatars.githubusercontent.com/u/956888?v=4)](https://github.com/aaronsaray "aaronsaray (24 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/aaronsaray-phproblemlogger/health.svg)

```
[![Health](https://phpackages.com/badges/aaronsaray-phproblemlogger/health.svg)](https://phpackages.com/packages/aaronsaray-phproblemlogger)
```

###  Alternatives

[sentry/sentry

PHP SDK for Sentry (http://sentry.io)

1.9k227.1M273](/packages/sentry-sentry)[rollbar/rollbar

Monitors errors and exceptions and reports them to Rollbar

33723.7M82](/packages/rollbar-rollbar)[illuminate/log

The Illuminate Log package.

6224.3M517](/packages/illuminate-log)[open-telemetry/sdk

SDK for OpenTelemetry PHP.

2222.9M248](/packages/open-telemetry-sdk)[open-telemetry/api

API for OpenTelemetry PHP.

1833.0M214](/packages/open-telemetry-api)[pagemachine/typo3-formlog

Form log for TYPO3

23225.3k6](/packages/pagemachine-typo3-formlog)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
