PHP code example of jdz / output

1. Go to this page and download the library: Download jdz/output library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jdz / output example snippets


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

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->

// Check if one level udes(Verbosity::WARN);  // true
Verbosity::WARN-> 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

// 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

// 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

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

$output->step('Step 1');   // Will be info('Info 1');    // Will NOT be oString(false);
// Contains: STEP, ERROR, WARN

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

$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

$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

$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

[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