• Stars
    star
    132
  • Rank 274,205 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Colored/ANSI Line Formatter for Monolog

Monolog Colored Line Formatter

Build Status Source Supported PHP Versions Version Downloads License

A Formatter for Monolog with color support Built by Bramus! - https://www.bram.us/

About

bramus/monolog-colored-line-formatter is a formatter for use with Monolog. It augments the Monolog LineFormatter by adding color support. To achieve this bramus/monolog-colored-line-formatter uses ANSI Escape Sequences – provided by bramus/ansi-php – which makes it perfect for usage on text based terminals (viz. the shell).

bramus/monolog-colored-line-formatter ships with a default color scheme, yet it can be adjusted to fit your own needs.

Prerequisites/Requirements

  • PHP 8.1 or greater
  • Monolog 3.0 or greater

Looking for a version compatible with Monolog 1.x? Check out the monolog-1.x branch then. The version of monolog-colored-line-formatter that is compatible with Monolog 1.x, is monolog-colored-line-formatter version ~2.0

Looking for a version compatible with Monolog 2.x? Check out the monolog-2.x branch then. The version of monolog-colored-line-formatter that is compatible with Monolog 1.x, is monolog-colored-line-formatter version ~3.0.0

Installation

Installation is possible using Composer.

Install monolog-colored-line-formatter, compatible with Monolog 3.x:

composer require bramus/monolog-colored-line-formatter ~3.1

Install monolog-colored-line-formatter, compatible with Monolog 2.x:

composer require bramus/monolog-colored-line-formatter ~3.0.0

Install monolog-colored-line-formatter, compatible with Monolog 1.x:

composer require bramus/monolog-colored-line-formatter ~2.0

Usage

Create an instance of \Bramus\Monolog\Formatter\ColoredLineFormatter and set it as the formatter for the \Monolog\Handler\StreamHandler that you use with your \Monolog\Logger instance.

use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use \Bramus\Monolog\Formatter\ColoredLineFormatter;

$log = new Logger('DEMO');
$handler = new StreamHandler('php://stdout', Logger::WARNING);
$handler->setFormatter(new ColoredLineFormatter());
$log->pushHandler($handler);

$log->addError('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');

Color Schemes

Available Color Schemes

Color Scheme: DefaultScheme

Monolog Colored Line Formatter

Color Scheme: TrafficLight

Monolog Colored Line Formatter

Activating a Color Scheme

Color Schemes are defined as classes. If you do not provide any color scheme the default one will be used.

To activate a color scheme pass it as the first argument of the ColoredLineFormatter Constructor. All successive arguments are the ones as required by the \Monolog\Formatter\LineFormatter class.

use \Monolog\Logger;
use \Monolog\Handler\StreamHandler;
use \Bramus\Monolog\Formatter\ColoredLineFormatter;
use \Bramus\Monolog\Formatter\ColorSchemes\TrafficLight;

$log = new Logger('DEMO');
$handler = new StreamHandler('php://stdout', Logger::WARNING);
$handler->setFormatter(new ColoredLineFormatter(new TrafficLight()));
$log->pushHandler($handler);

Alternatively it's also possible to activate it using the setColorScheme() method of a ColoredLineFormatter instance.

Creating your own Custom Color Scheme

To define your own color scheme make a class that implements the \Bramus\Monolog\Formatter\ColorSchemes\ColorSchemeInterface interface. To make things more easy a trait ColorSchemeTrait is defined.

namespace Bramus\Monolog\Formatter\ColorSchemes;

use Monolog\Logger;
use Monolog\Level;
use Bramus\Ansi\Ansi;
use Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR;

class TrafficLight implements ColorSchemeInterface
{
    /**
     * Use the ColorSchemeTrait and alias its constructor
     */
    use ColorSchemeTrait {
        ColorSchemeTrait::__construct as private __constructTrait;
    }

    /**
     * [__construct description]
     */
    public function __construct()
    {
        // Call Trait Constructor, so that we have $this->ansi available
        $this->__constructTrait();

        // Our Color Scheme
        $this->setColorizeArray(array(
            Level::Debug => $this->ansi->color(SGR::COLOR_FG_WHITE)->get(),
            Level::Info => $this->ansi->color(SGR::COLOR_FG_GREEN)->get(),
            Level::Notice => $this->ansi->color(SGR::COLOR_FG_CYAN)->get(),
            Level::Warning => $this->ansi->color(SGR::COLOR_FG_YELLOW)->get(),
            Level::Error => $this->ansi->color(SGR::COLOR_FG_RED)->get(),
            Level::Critical => $this->ansi->color(SGR::COLOR_FG_RED)->underline()->get(),
            Level::Alert => $this->ansi->color([SGR::COLOR_FG_WHITE, SGR::COLOR_BG_RED_BRIGHT])->get(),
            Level::Emergency => $this->ansi->color(SGR::COLOR_BG_RED_BRIGHT)->blink()->color(SGR::COLOR_FG_WHITE)->get(),
        ));
    }
}

Please refer to the bramus/ansi-php documentation to define your own styles and colors.

Unit Testing

bramus/monolog-colored-line-formatter ships with unit tests using PHPUnit.

  • If PHPUnit is installed globally run phpunit to run the tests.

  • If PHPUnit is not installed globally, install it locally through composer by running composer install --dev. Run the tests themselves by calling vendor/bin/phpunit.

Unit tests are also automatically run on GitHub Actions

License

bramus/monolog-colored-line-formatter is released under the MIT public license. See the enclosed LICENSE.txt for details.

More Repositories

1

react-native-maps-directions

Directions Component for `react-native-maps`
JavaScript
1,219
star
2

router

A lightweight and simple object oriented PHP Router
PHP
1,085
star
3

mixed-content-scan

Scan your HTTPS-enabled website for Mixed Content
PHP
522
star
4

photoshop-google-maps-tile-cutter

PS_Bramus.GoogleMapsTileCutter - A Photoshop script that cuts image tiles from any image for direct use with Google Maps
JavaScript
201
star
5

js-pagination-sequence

Generate a sequence of numbers for use in a pagination system, the clever way.
JavaScript
140
star
6

freshinstall

Automatically configure and install software onto your new Mac
Shell
106
star
7

composer-autocomplete

Bash/Shell autocompletion for Composer
94
star
8

mastodon-redirector

View Mastodon profiles on your Mastodon instance
JavaScript
93
star
9

ansi-php

ANSI Control Functions and ANSI Control Sequences (Colors, Erasing, etc.) for PHP CLI Apps
PHP
89
star
10

specificity

JavaScript Package to calculate the Specificity of CSS Selectors
JavaScript
68
star
11

viewport-resize-behavior

Viewport vs Virtual Keyboard Resize Behavior Explainer
45
star
12

simple-rest-api-explorer

A simple way to showcasing and exploring all endpoints of your RESTful API.
JavaScript
37
star
13

mercator-puzzle-redux

Dynamic take on the original Mercator Puzzle by Google
HTML
37
star
14

scroll-driven-animations-debugger-extension

Browser extension to debug Scroll-Driven Animations
36
star
15

ion-drawer-vertical

A vertical drawer for Ionic 1
JavaScript
35
star
16

sda-utilities

Collection of utility functions for use with Scroll-Driven Animations
JavaScript
28
star
17

style-observer

MutationObserver for CSS
TypeScript
26
star
18

view-transitions-demos

HTML
20
star
19

react-native-maps-directions-example

Example app that uses react-native-maps-directions
Java
20
star
20

ws2-sws-course-materials

Course materials for the course Serverside Webscripting, part of the Professional Bachelor ICT study programme.
PHP
13
star
21

chrome-dark-mode-toggle

Real control over Light Mode / Dark Mode on a per-site basis!
13
star
22

web-dev-rss

Cloudflare Worker that generates RSS Feeds for web.dev
JavaScript
12
star
23

webhid-elgato-stream-deck-daft-punk-soundboard

WebHID Demo: Elgato Stream Deck Daft Punk Soundboard
JavaScript
12
star
24

bramus_cssextras

bramus_cssextras is a plugin for TinyMCE which enables the usage of CSS classes and CSS ids on any HTML element within TinyMCE. Together with an external CSS file, set via the TinyMCE content_css setting, bramus_cssextras bridges the (visual) gap between the content entered through TinyMCE and the actual output
JavaScript
12
star
25

ria-course-materials

Course materials for the course Web & Mobile Development (formerly Rich Internet Applications), part of the Professional Bachelor ICT study programme.
JavaScript
12
star
26

ws1-sws-course-materials

Course materials for the course Serverside Webscripting, part of the Professional Bachelor ICT study programme.
PHP
11
star
27

css-houdini-voronoi

A CSS Houdini Paint Worklet that draws a Voronoi Diagram as a background image
JavaScript
10
star
28

google-maps-polygon-moveto

Programmatically move a google.maps.Polygon to a new google.maps.LatLng using Google Maps V3
JavaScript
9
star
29

js-range

JavaScript
7
star
30

ws2-sws-fiddles-silex

Silex Code Examples/Fiddles
PHP
7
star
31

css-houdini-circles

A CSS Houdini PaintWorklet to draw background circles.
JavaScript
7
star
32

PS_BRAMUS.TextConvert

PSD2TXT and TXT2PSD for the masses!
JavaScript
6
star
33

enumeration

Yet Another Enumeration Implementation for PHP
PHP
5
star
34

chrome-for-developers-rss

Cloudflare Worker that generates RSS Feeds for developer.chrome.com
JavaScript
4
star
35

wec-web-ui-summit-2023-demos

WEC Web UI Summit 2023 Demos
HTML
4
star
36

reflection

A library that tries to make PHP's built-in Reflection better.
PHP
3
star
37

github-toggle-chrome-extension

Quickly toggle between a GitHub Repo and its GitHub Pages
JavaScript
3
star
38

jquery_classdata

jQuery ClassData is a jQuery plugin that allows you to access and manipulate data saved in the class property of DOM-elements in a nice and easy to use way.
JavaScript
3
star
39

js-pagination-sequence-demos

Demos that use @bramus/pagination-sequence
JavaScript
2
star
40

css-selector-benchmark

CSS Selector Benchmarks, using PerfTestRunner and Puppeteer
HTML
2
star
41

wmd-course-materials

Course materials for the course Web & Mobile Development, part of the Professional Bachelor ICT study programme.
JavaScript
2
star
42

yummy

A self hosted Delicious (with del.icio.us import)
PHP
1
star
43

meetingzone

Find a meeting time across timezones
JavaScript
1
star
44

gcloud-kms-scripts

A collection of scripts here to help interact with Google's Cloud Key Management Service (KMS)
Shell
1
star
45

tokenphrase

TokenPhrase is a PHP class that generates unique phrases for you to use in your app as tokens
PHP
1
star
46

terraform-gcloud-event-cloud-function

Terraform deploy a local folder to a Google Cloud Function that can be triggered via an Event
HCL
1
star
47

remix-example-app

JavaScript
1
star
48

parcel-css-demo

JavaScript
1
star
49

bramus_cssextras-demo

Demo for bramus_cssextras (https://github.com/bramus/bramus_cssextras) | bramus_cssextras is a plugin for TinyMCE which enables the usage of CSS classes and CSS ids on any HTML element within TinyMCE. Together with an external CSS file, set via the TinyMCE content_css setting, bramus_cssextras bridges the (visual) gap between the content entered through TinyMCE and the actual output
1
star