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

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

lfbn/logger-trait
=================

A trait that allows you to add logging capabilities to any class.

0.0.1(6y ago)017MITPHP

Since Jan 9Pushed 6y ago1 watchersCompare

[ Source](https://github.com/lfbn/logger-trait)[ Packagist](https://packagist.org/packages/lfbn/logger-trait)[ RSS](/packages/lfbn-logger-trait/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

Logger Trait
============

[](#logger-trait)

This is a Trait that allows to have logging capabilities in any class.

By default, it uses Monolog and streams to standard output, but you can override this behaviour.

Installation
------------

[](#installation)

```
composer require lfbn/logger-trait
```

Usage
-----

[](#usage)

### Using the default behaviour

[](#using-the-default-behaviour)

```
// In the class you want, add the use of the Trait.
use LoggerTrait;

(...)

// After that, you can use it.
$this->logError('Some message...', ['some context']);
```

### Changing name, stream or minimum level

[](#changing-name-stream-or-minimum-level)

```
// Implement the following protected properties.
/* @var string */
protected static $loggerName = 'my-logger-name';

/* @var string */
protected static $loggerStream = 'php://stdout';

/* @var string */
protected static $loggerMinimumLevel = LogLevel::DEBUG;
```

### Overriding the default behaviour

[](#overriding-the-default-behaviour)

```
class MyClass {
   protected function initLogger(): bool
   {
       $this->logger = new \Monolog\Logger('Overriding default logger: '.$this->getLoggerName());

       try {
           $handler = new StreamHandler(
               $this->getLoggerStream(),
               $this->getLoggerMinimumLevel()
           );
           $this->logger->pushHandler($handler);
       } catch (Exception $e) {
           $this->logger = new NullLogger();

           return false;
       }

       return true;
   }
}
```

### Inject your own logger

[](#inject-your-own-logger)

```
class Test {
    use \Lfbn\LoggerTrait\LoggerTrait;
    public function test(): void
    {
        $this->logDebug('Hello TEST!');
    }
}

$logger = new \Monolog\Logger('test4-my-own-logger');

try {
    $handler = new StreamHandler(
        'php://stdout',
        'debug'
    );
    $logger->pushHandler($handler);
} catch (Exception $e) {
    $logger = new NullLogger();
}

$myClass = (new Test());
$myClass->setLogger($logger);
$myClass->test();
```

### Interpolate messages

[](#interpolate-messages)

```
$this->logWarning(
            self::interpolateMessage(
                'Hello my {private} TEST5!',
                ['private' => 'message']
            ),
        );
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2312d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2d7f7545554c3695bf1cbd1857aa6b9b7bb11de28f4d7634dbf342a4a2712f59?d=identicon)[lfbn](/maintainers/lfbn)

---

Top Contributors

[![lfbn](https://avatars.githubusercontent.com/u/6152567?v=4)](https://github.com/lfbn "lfbn (1 commits)")

---

Tags

loggerloggingtraitlogginglogger

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[rollbar/rollbar

Monitors errors and exceptions and reports them to Rollbar

33723.7M81](/packages/rollbar-rollbar)[analog/analog

Fast, flexible, easy PSR-3-compatible PHP logging package with dozens of handlers.

3451.5M24](/packages/analog-analog)[inpsyde/wonolog

Monolog-based logging package for WordPress.

183617.9k6](/packages/inpsyde-wonolog)[justbetter/magento2-sentry

Magento 2 Logger for Sentry

1851.5M3](/packages/justbetter-magento2-sentry)[amphp/log

Non-blocking logging for PHP based on Amp, Revolt, and Monolog.

402.6M70](/packages/amphp-log)[apix/log

Minimalist, thin and fast PSR-3 compliant (multi-bucket) logger.

511.0M18](/packages/apix-log)

PHPackages © 2026

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