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