PHPackages                             joebengalen/logger - 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. joebengalen/logger

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

joebengalen/logger
==================

Lightweight psr-3 logger library.

118[3 issues](https://github.com/JoeBengalen/Logger/issues)PHP

Since Mar 12Pushed 11y ago2 watchersCompare

[ Source](https://github.com/JoeBengalen/Logger)[ Packagist](https://packagist.org/packages/joebengalen/logger)[ RSS](/packages/joebengalen-logger/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (4)Used By (0)

Logger
======

[](#logger)

[![Build Status](https://camo.githubusercontent.com/1107bd975fcf174d0779fc8d4bb32564061dd663b8d6749e3a5f67080b99f0e0/68747470733a2f2f7472617669732d63692e6f72672f4a6f6542656e67616c656e2f4c6f676765722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/JoeBengalen/Logger)

Lightweight [psr-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) logger library.

Getting started
---------------

[](#getting-started)

### Installation

[](#installation)

It is recommended to install the package with composer:

```
require: {
    "joebengalen/logger": "*"
}

```

### Usage

[](#usage)

```
use JoeBengalen\Logger\Logger;
use JoeBengalen\Logger\Handler;

// basic instantiation of the logger
$logger = new Logger([
    // callable handlers
]);

// basic usage
$logger->emergency('emergency message');    // System is unusable
$logger->alert('alert message');            // Action must be taken immediately (Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.)
$logger->critical('critical message');      // Critical conditions (Example: Application component unavailable, unexpected exception.)
$logger->error('error message');            // Runtime errors that do not require immediate action but should typically be logged and monitored.
$logger->warning('warning message');        // Exceptional occurrences that are not errors (Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.)
$logger->notice('notice message');          // Normal but significant events.
$logger->info('info message');              // Interesting events (Example: User logs in, SQL logs.)
$logger->debug('debug message');            // Detailed debug information.
```

#### Context

[](#context)

Along with a message also a second argument can be passed. This *context* is an array which could contain anything.

The `$context` can be used to replace placeholders in the message.

```
$logger->info('User {username} created.', ['username' => 'John Doe']);
// -> User John Doe created.
```

The context has a special key *exception* which could be used to pass an `\Exception`. The handler can recognize the `\Exception` and act upon it.

```
$logger->critical("Unexpected Exception occurred.", ['exception' => new \Exception('Something went horribly wrong :(')]);
```

Aside from the named functionality the context array can be used to pass any data that may be useful for the message.

### Handlers

[](#handlers)

Handers are callables registered to the logger. It is up to the user which handler(s) to register. **Note** that there is no default handler. So if none is registered nothing happens. The registered handlers will be called in the order they are given.

#### Shipped handlers

[](#shipped-handlers)

All shipped handlers process *every* message. There if no filter based on the log level.

##### FileHandler

[](#filehandler)

The `FileHandler` is given a file in its initialization and logs all messages into that file.

```
$logger = new Logger([
    new Handler\FileHandler('default.log')
]);
```

##### DatabaseHandler

[](#databasehandler)

The `DatabaseHandler` is given `\PDO` instance in its initialization and logs all messages into a table.

```
$logger = new Logger([
    new Handler\DatabaseHander(new \PDO(...));
]);
```

#### Custom handlers

[](#custom-handlers)

A handler is, simply put, a callable, which is given an instance of `JoeBengalen\Logger\MessageInterface`.

```
function (\JoeBengalen\Logger\MessageInterface $message) { }
```

All shipped handlers are invokable objects, but a handler could as well be an anonymous function, a static class method or any other valid [callable](http://php.net/manual/en/language.types.callable.php).

```
use JoeBengalen\Logger\MessageInterface;
use Psr\Log\LogLevel;

$logger = new Logger([

    // anonymous function
    function (MessageInterface $message) {
        if ($message->getLevel() === LogLevel::EMERGENCY) {
            // send an email
        }
    },

    // static class method
    ['ClassName', 'staticMethod'] // declared somewhere else
]);
```

**Note** that it is up to the handler to handle the *context* array properly. (For example, replacing placeholders in the message and recognizing an `\Exception`)

#### Custom MessageInterface factory

[](#custom-messageinterface-factory)

The factory to create a MessageInterface instance is another callable, registered as $option `message.factory`. This factory is given three arguments. The `$level`, `$message` and `array $context` and should return an instance that implements the `JoeBengalen\Logger\MessageInterface`. Apart from that the factory is completely free to return any object (as long as it implements the right interface) it wants and format the given arguments as it pleases.

By default an instance of `Message` is returned.

```
use JoeBengalen\Logger\Logger;
use JoeBengalen\Logger\Message;

$logger = new Logger([
    // handlers
], [

    // MessageInterface factory
    'message.factory' => function($level, $message, $context) {
        return new Message($level, $message, $context);
    }

]);
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/47732f0e5bc5d319cc52db1313312daa37279fe5fa16abf5bdd5a77e65347b1d?d=identicon)[JoeBengalen](/maintainers/JoeBengalen)

---

Top Contributors

[![JoeBengalen](https://avatars.githubusercontent.com/u/11173689?v=4)](https://github.com/JoeBengalen "JoeBengalen (87 commits)")

### Embed Badge

![Health badge](/badges/joebengalen-logger/health.svg)

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

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[itsgoingd/clockwork

php dev tools in your browser

5.9k27.6M94](/packages/itsgoingd-clockwork)[graylog2/gelf-php

A php implementation to send log-messages to a GELF compatible backend like Graylog2.

41838.2M138](/packages/graylog2-gelf-php)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)[consolidation/log

Improved Psr-3 / Psr\\Log logger based on Symfony Console components.

15462.2M7](/packages/consolidation-log)[ekino/newrelic-bundle

Integrate New Relic into Symfony2

28111.2M8](/packages/ekino-newrelic-bundle)

PHPackages © 2026

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