PHPackages                             jdz/output - 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. [CLI &amp; Console](/categories/cli)
4. /
5. jdz/output

ActiveLibrary[CLI &amp; Console](/categories/cli)

jdz/output
==========

CLI Output Library

2.0.0(5mo ago)041MITPHPPHP &gt;=8.1

Since Aug 8Pushed 5mo agoCompare

[ Source](https://github.com/joffreydemetz/output)[ Packagist](https://packagist.org/packages/jdz/output)[ Docs](https://jdz.joffreydemetz.com/output/)[ RSS](/packages/jdz-output/feed)WikiDiscussions main Synced 1mo ago

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

JDZ Output
==========

[](#jdz-output)

A lightweight PHP library for handling process output with different verbosity levels and formatting options. Perfect for CLI applications, logging, and process monitoring.

Features
--------

[](#features)

- **Multiple output types**: step, info, warn, error, and dump messages
- **Dual storage system**: Filtered output based on verbosity + complete message dump
- **Verbosity control**: Fine-grained filtering with hierarchical levels using PHP 8.1+ enums
- **CLI detection**: Automatically detects CLI environment and outputs to console
- **File export**: Save filtered or complete output to files for logging
- **Tagged messages**: All messages are automatically tagged and aligned
- **String conversion**: Access both filtered and complete output programmatically
- **Type-safe**: Uses backed enums for type safety and better IDE support

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

[](#installation)

Install via Composer:

```
composer require jdz/output
```

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Quick Start
-----------

[](#quick-start)

```
use JDZ\Output\Output;
use JDZ\Output\Verbosity;

// Create output instance (auto-detects CLI mode)
$output = new Output();

// Add messages
$output->step('Starting process...');
$output->info('Processing 100 records');
$output->warn('Skipping invalid record');
$output->error('Database connection failed');
$output->dump('Debug info: memory usage');

// Control verbosity
$output->setVerbosity(Verbosity::WARN);

// Get output
echo $output->toString(false); // Filtered based on verbosity
echo $output->toString(true);  // All messages regardless of verbosity

// Save to file
$output->toFile('output.log');        // Filtered output
$output->toFile('complete.log', true); // Complete output
```

Verbosity Levels
----------------

[](#verbosity-levels)

The library uses a PHP 8.1+ backed enum for type-safe verbosity levels. All messages are always stored internally regardless of verbosity level.

### Enum Cases

[](#enum-cases)

Enum CaseValueDescription`Verbosity::NONE`0No messages in filtered output`Verbosity::STEP`1Only step messages`Verbosity::ERROR`4Step and error messages`Verbosity::WARN`8Step, error, and warning messages`Verbosity::INFO`16Step, error, warning, and info messages`Verbosity::ALL`32All messages including debug/dump### Usage

[](#usage)

```
use JDZ\Output\Output;
use JDZ\Output\Verbosity;

$output = new Output();

// Using enum (recommended - type-safe)
$output->setVerbosity(Verbosity::WARN);
$output->setVerbosity(Verbosity::INFO);
$output->setVerbosity(Verbosity::ALL); // default

// Using integer value (backward compatible)
$output->setVerbosity(8);  // Same as Verbosity::WARN

// Using constants (backward compatible)
$output->setVerbosity(Verbosity::WARN); // Same as 8

// Get current verbosity level
$level = $output->getVerbosity(); // Returns Verbosity enum
echo $level->name; // "WARN"
echo $level->value; // 8
echo $level->description(); // "Step, error, and warning messages"

// Check verbosity hierarchy
if (Verbosity::INFO->includes(Verbosity::WARN)) {
    // INFO level includes WARN level
}
```

### Verbosity Hierarchy

[](#verbosity-hierarchy)

Higher verbosity levels include all lower level messages:

```
NONE (0)
  ↓
STEP (1)
  ↓
ERROR (4)  ← includes STEP
  ↓
WARN (8)   ← includes STEP + ERROR
  ↓
INFO (16)  ← includes STEP + ERROR + WARN
  ↓
ALL (32)   ← includes everything

```

### Enum Methods

[](#enum-methods)

```
// Check if one level includes another
Verbosity::INFO->includes(Verbosity::WARN);  // true
Verbosity::WARN->includes(Verbosity::INFO);  // false

// Get human-readable description
Verbosity::WARN->description();
// Returns: "Step, error, and warning messages"

// Get all cases
foreach (Verbosity::cases() as $level) {
    echo $level->name . ": " . $level->description() . "\n";
}

// Create from integer
$level = Verbosity::from(8);      // Returns Verbosity::WARN
$level = Verbosity::tryFrom(999); // Returns null for invalid value
```

Message Types
-------------

[](#message-types)

The Output class provides methods for different types of messages, each tagged appropriately.

```
// Step Messages (verbosity >= Verbosity::STEP)
$output->step('Initializing application...');
// Output: [STEP]  Initializing application...

// Info Messages (verbosity >= Verbosity::INFO)
$output->info('Found 150 records to process');
// Output: [INFO]  Found 150 records to process

// Warning Messages (verbosity >= Verbosity::WARN)
$output->warn('Using default configuration');
// Output: [WARN]  Using default configuration

// Error Messages (verbosity >= Verbosity::ERROR)
$output->error('Database connection failed');
// Output: [ERROR] Database connection failed

// Debug/Dump Messages (verbosity >= Verbosity::ALL)
$output->dump('Current memory usage: 45MB');
// Output: [DUMP]  Current memory usage: 45MB

// Custom Tagged Messages
$output->add('Custom log entry', 'custom');
// Output: [CUSTOM]Custom log entry
```

CLI Mode
--------

[](#cli-mode)

The library automatically detects if it's running in CLI mode and outputs messages to the console in real-time.

```
// Auto-detect mode
$output = new Output(); // Will detect CLI automatically

// Force CLI mode
$output = new Output('cli');

// Force non-CLI mode (no console output)
$output = new Output('');

// Only messages that pass the verbosity filter are echoed to CLI
// But all messages are stored internally regardless
```

Dual Storage System
-------------------

[](#dual-storage-system)

One of the key features is the dual storage system:

1. **Filtered Output** (`$output` array): Contains only messages that pass the verbosity filter
2. **Complete Dump** (`$dump` array): Contains ALL messages regardless of verbosity

```
$output = new Output('');
$output->setVerbosity(Verbosity::WARN);

$output->step('Step 1');   // Will be included
$output->error('Error 1'); // Will be included
$output->warn('Warning 1'); // Will be included
$output->info('Info 1');    // Will NOT be included in filtered output
$output->dump('Debug 1');   // Will NOT be included in filtered output

// Get filtered output (respects verbosity)
$filtered = $output->toString(false);
// Contains: STEP, ERROR, WARN

// Get complete output (ignores verbosity)
$complete = $output->toString(true);
// Contains: STEP, ERROR, WARN, INFO, DUMP
```

File Output
-----------

[](#file-output)

Save output to files with optional verbosity filtering:

```
$output = new Output('');
$output->setVerbosity(Verbosity::WARN);

$output->step('Processing started');
$output->info('Record 1 processed');
$output->warn('Skipped invalid record');
$output->error('Failed to process record 5');
$output->dump('Memory usage: 45MB');

// Save filtered output (respects verbosity level)
$output->toFile('filtered.log', false);
// File contains: step, error, warn messages only

// Save complete output (all messages)
$output->toFile('complete.log', true);
// File contains: all messages including info and dump
```

**File contents example:**

`filtered.log`:

```
[STEP]  Processing started
[WARN]  Skipped invalid record
[ERROR] Failed to process record 5

```

`complete.log`:

```
[STEP]  Processing started
[INFO]  Record 1 processed
[WARN]  Skipped invalid record
[ERROR] Failed to process record 5
[DUMP]  Memory usage: 45MB

```

Advanced Usage
--------------

[](#advanced-usage)

### Dual Output Access

[](#dual-output-access)

```
$output = new Output('');
$output->setVerbosity(Verbosity::ERROR);

$output->step('Step 1');
$output->error('Error occurred');
$output->warn('Warning message');
$output->info('Info message');
$output->dump('Debug data');

// Method 1: toString() with parameter
$filtered = $output->toString(false); // Only STEP and ERROR
$complete = $output->toString(true);  // All messages

// Method 2: __toString() always returns filtered
$filtered = (string) $output; // Only STEP and ERROR
```

### Custom Tags

[](#custom-tags)

Add custom tagged messages that don't fit standard categories:

```
$output->add('Database connected successfully', 'db');
// Output: [DB]    Database connected successfully

$output->add('Cache cleared', 'cache');
// Output: [CACHE] Cache cleared

$output->add('Custom event triggered', 'event');
// Output: [EVENT] Custom event triggered
```

### Understanding Message Formatting

[](#understanding-message-formatting)

All tags are padded to 8 characters for consistent alignment:

```
[STEP]  Message  // 4-char tag + 2 spaces + message
[INFO]  Message  // 4-char tag + 2 spaces + message
[WARN]  Message  // 4-char tag + 2 spaces + message
[ERROR] Message  // 5-char tag + 1 space + message
[DUMP]  Message  // 4-char tag + 2 spaces + message
[CUSTOM]Message  // 6-char tag + 0 spaces + message
```

Examples
--------

[](#examples)

See the [examples](examples/) directory for detailed examples:

- `example.php` - Complete usage demonstration with all verbosity levels

Testing
-------

[](#testing)

Run the test suite:

```
# Run all tests
composer test

# Run with coverage
composer test -- --coverage-html coverage

# Run specific test file
vendor/bin/phpunit tests/OutputTest.php
vendor/bin/phpunit tests/VerbosityTest.php

# Run with detailed output
vendor/bin/phpunit --testdox
```

License
-------

[](#license)

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

Changelog
---------

[](#changelog)

### Version 2.0.0

[](#version-200)

- Added PHP 8.1+ backed enum for type-safe verbosity levels
- Added `Verbosity::includes()` method for hierarchy checking
- Added `Verbosity::description()` method for human-readable descriptions
- Improved dual storage system documentation
- Enhanced test coverage

### Version 1.0.0

[](#version-100)

- Initial release
- Basic output handling with verbosity levels
- CLI detection and console output
- File export functionality
- Tagged messages with alignment

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance71

Regular maintenance activity

Popularity3

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity47

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

Every ~120 days

Total

2

Last Release

162d ago

Major Versions

1.0.0 → 2.0.02025-12-07

### Community

Maintainers

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

---

Top Contributors

[![joffreydemetz](https://avatars.githubusercontent.com/u/15113527?v=4)](https://github.com/joffreydemetz "joffreydemetz (3 commits)")

---

Tags

clioutputJDZ

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jdz-output/health.svg)

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

###  Alternatives

[symfony/console

Eases the creation of beautiful and testable command line interfaces

9.8k1.1B11.3k](/packages/symfony-console)[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k331.8M8.5k](/packages/nunomaduro-collision)[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k239.8M286](/packages/nunomaduro-termwind)[wp-cli/wp-cli

WP-CLI framework

5.0k17.2M320](/packages/wp-cli-wp-cli)[wp-cli/php-cli-tools

Console utilities for PHP

68325.0M367](/packages/wp-cli-php-cli-tools)[socialengine/sniffer-rules

A Lumen 5 and Laravel 5 SquizLabs Code Sniffer 2.0 artisan command. Detect violations of a defined coding standard. It helps your code remains clean and consistent.

1248.2k1](/packages/socialengine-sniffer-rules)

PHPackages © 2026

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