volnix/jobber

A slim library for printing typical CLI job output.

1.4.2 2015-08-14 13:47 UTC

This package is auto-updated.

Last update: 2024-04-08 08:46:28 UTC


README

Build Status Downloads Latest Stable Version Code Coverage

Jobber is a super-slim library for printing output from a CLI job. It supports command-line colors through the use of kevinlebrun/colors.php, a well-revered CLI color library.

It was built with simplicity in mind, as printing job output should be the least of your worries when building CLI-based scripts.

Example output from Jobber

***************************************************
2015-01-23 15:52:35 - Starting test_name

INFO: 2015-01-23 15:52:35 - foo
WARNING: 2015-01-23 15:52:35 - bar
SUCCESS: 2015-01-23 15:52:35 - baz
ERROR: 2015-01-23 15:52:35 - qux

2015-01-23 15:52:35 - Execution Time: 0.0 seconds / Peak memory usage: 3.22 Mb
***************************************************

Installation

Command line:

composer require volnix/jobber:~1.0

composer.json:

{
    "name": "your/application",
    "require": {
        "volnix/jobber": "~1.0"
    }
}

Usage

Typically the printer (Volnix/Jobber/Printer) will be started, then stopped. The start method prints out the job name and and some asterisks to fence off this job's output. The stop method prints memory usage, runtime, and more fences.

use Volnix/Jobber/Printer;

Printer::start('my_job_name');
Printer::info('Something happened, but it is not super important.');
Printer::stop();

Jobber also supports getting the output out of the printer in plain-text. This is especially useful for logging job output somewhere.

Printer::start('my_job_name');
Printer::info('Something happened, but it is not super important.');
Printer::stop();

$my_logger->info(Printer::getOutput());

All message types:

  • Info (Printer::info())
  • Warning (Printer::warning())
  • Success (Printer::success())
  • Error (Printer::error())
  • Fatal (Printer::fatal())
    • Note: fatal is merely an alias for error and stop in one call

If you desire to toggle verbosity on your job, this is supported. This will only disable info messages, while still allowing others to come through.

// turn off verbosity, disabling info messages
Printer::setVerbosity(false);

Note: info messages will still be returned when calling Printer::getOutput() for logging purposes

If you desire to run multiple job "sessions" in one command, you can reset the printer.

// do job 1
Printer::start('job_number_1');
// do something in your code...
Printer::success('Something good happened.');
Printer::stop();

// reset the printer
Printer::reset();

// start job 2
Printer::start('job_number_2');
// ...

Finally, you may use sprintf-style messages as well by passing a tokenized (%s, %d, etc.) string as your first argument and an array of tokens as your second.

// printing with tokens
Printer::start('something job');
Printer::info('Exported %d records to %s.', [10, '/filepath/something/here.txt']); // "Exported 10 records to /filepath/something/here.txt"