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

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

geoffroy-aubry/logger
=====================

PSR-3 logger for adding colors and indentation on PHP CLI output.

v1.1.2(12y ago)621.2k[3 issues](https://github.com/geoffroy-aubry/Logger/issues)4LGPL-3.0+PHPPHP &gt;=5.3.3

Since Apr 1Pushed 12y ago2 watchersCompare

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

READMEChangelogDependencies (7)Versions (7)Used By (4)

Colored and Indented Logger
===========================

[](#colored-and-indented-logger)

[![Latest stable version](https://camo.githubusercontent.com/029ff3a07a3c098bc996e5ab906639e45af3aaae262f966019e11d5c9363c1c3/68747470733a2f2f706f7365722e707567782e6f72672f67656f6666726f792d61756272792f4c6f676765722f762f737461626c652e706e67 "Latest stable version")](https://packagist.org/packages/geoffroy-aubry/Logger)[![Build Status](https://camo.githubusercontent.com/f57b99bf11f64a298204eabc9381992d66c99f60b6610bd1d910baf13f7e48aa/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f67656f6666726f792d61756272792f4c6f676765722e706e673f6272616e63683d737461626c65)](http://travis-ci.org/geoffroy-aubry/Logger)[![Coverage Status](https://camo.githubusercontent.com/77d5823d2c8168a98e5c804d8f97b7dbeecd96e3ba5fd236232f0999c57c2bb3/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f67656f6666726f792d61756272792f4c6f676765722f62616467652e706e673f6272616e63683d737461626c65)](https://coveralls.io/r/geoffroy-aubry/Logger)

[PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)logger for adding colors and indentation on PHP CLI output.

Description
-----------

[](#description)

Use tags and placeholder syntax to provide an easy way to color and indent PHP CLI output. [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) compatibility allows graceful degradation when switching to another PSR-3 compliant logger.

### Colors

[](#colors)

1. Declare some colors:

```
    $aConfig = array(
        'colors' => array(
            'debug'      => "\033[0;35m",
            'error'      => "\033[1;31m",
            'section'    => "\033[1;37m",
            'subsection' => "\033[1;33m",
            'ok'         => "\033[1;32m"
        )
    );
    $oLogger = new ColoredIndentedLogger($aConfig);
```

2. Use them either by exploiting placeholder syntax with `'C.'` prefix:

```
    $oLogger->info('{C.section}Start of new section');
    $oLogger->info('Result is {C.ok}OK');
```

Or by logging message with a level equals to a color key:

```
    $oLogger->debug('some debug information…');
    $oLogger->error(new \RuntimeException('Arghhhh!'));
```

It is even possible to specify color tag in the value of a placeholder:

```
    $oLogger->info('Result is {result}', array('result' => '{C.ok}OK'));
```

*See below for demonstration.*

### Indentation

[](#indentation)

You can control indentation level with indent and unindent tags:

- default tags are `'+++'` and `'---'`,
- both usable at the beginning or at the end of any message,
- one or more occurrences,
- can be used alone.

Example:

```
$oLogger->info('Section A+++');
$oLogger->info('Subsection+++');
$oLogger->info('Step 1');
$oLogger->info('Step 2');
$oLogger->info('------Section B+++');
```

*See below for demonstration.*

### Configuration

[](#configuration)

Default configuration:

```
array(
    'colors'               => array(),
    'base_indentation'     => "\033[0;30m┆\033[0m   ",
    'indent_tag'           => '+++',
    'unindent_tag'         => '---',
    'min_message_level'    => \Psr\Log\LogLevel::DEBUG,
    'reset_color_sequence' => "\033[0m",
    'color_tag_prefix'     => 'C.'
);
```

Where:

- `colors` ⇒ `(array)` Array of key/value pairs to associate bash color codes to color tags (see above).
- `base_indentation` ⇒ `(string)` Describe what is a simple indentation, e.g. `"\t"`.
- `indent_tag` ⇒ `(string)` Tag usable at the start or at the end of the message to add one or more indentation level.
- `unindent_tag` ⇒ `(string)` Tag usable at the start or at the end of the message to remove one or more indentation level.
- `min_message_level` ⇒ `(string)` Threshold required to log message, must be defined in `\Psr\Log\LogLevel`.
- `reset_color_sequence` ⇒ `(string)` Concatenated sequence at the end of message when colors are used. For example: `"\033[0m"`.
- `color_tag_prefix` ⇒ `(string)` Prefix used in placeholders to distinguish standard context from colors.

### Demo

[](#demo)

See [demo.php](examples/demo.php) script for an example:

```
$ php examples/demo.php
```

Here is the result:

[![result of demo.php](examples/demo.png)](examples/demo.png)

Usage
-----

[](#usage)

1. Class autoloading and dependencies are managed by [Composer](http://getcomposer.org/)so install it following the instructions on [Composer: Installation - \*nix](http://getcomposer.org/doc/00-intro.md#installation-nix)or just run the following command:

```
$ curl -sS https://getcomposer.org/installer | php
```

2. Add dependency to `GAubry\Logger` into require section of your `composer.json`:

```
    {
        "require": {
            "geoffroy-aubry/logger": "1.*"
        }
    }
```

and run `php composer.phar install` from the terminal into the root folder of your project.

3. Include Composer's autoloader and use the `GAubry\Logger` class:

```
