Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Console
  4. Helpers
  5. Debug Formatter Helper

Debug Formatter Helper

Edit this page

The DebugFormatterHelper provides functions to output debug information when running an external program, for instance a process or HTTP request. For example, if you used it to output the results of running figlet symfony, it might output something like this:

Console output, with the first line showing "RUN Running figlet", followed by lines showing the output of the command prefixed with "OUT" and "RES Finished the command" as last line in the output.

Using the debug_formatter

The formatter is included in the default helper set and you can get it by calling getHelper():

1
$debugFormatter = $this->getHelper('debug_formatter');

The formatter accepts strings and returns a formatted string, which you then output to the console (or even log the information or do anything else).

All methods of this helper have an identifier as the first argument. This is a unique value for each program. This way, the helper can debug information for multiple programs at the same time. When using the Process component, you probably want to use spl_object_hash.

Tip

This information is often too verbose to be shown by default. You can use verbosity levels to only show it when in debugging mode (-vvv).

Starting a Program

As soon as you start a program, you can use start() to display information that the program is started:

1
2
3
4
5
6
7
8
9
// ...
$process = new Process(...);

$output->writeln($debugFormatter->start(
    spl_object_hash($process),
    'Some process description'
));

$process->run();

This will output:

1
RUN Some process description

You can tweak the prefix using the third argument:

1
2
3
4
5
6
7
$output->writeln($debugFormatter->start(
    spl_object_hash($process),
    'Some process description',
    'STARTED'
));
// will output:
//  STARTED Some process description

Output Progress Information

Some programs give output while they are running. This information can be shown using progress():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\Process\Process;

// ...
$process = new Process(...);

$process->run(function (string $type, string $buffer) use ($output, $debugFormatter, $process): void {
    $output->writeln(
        $debugFormatter->progress(
            spl_object_hash($process),
            $buffer,
            Process::ERR === $type
        )
    );
});
// ...

In case of success, this will output:

1
OUT The output of the process

And this in case of failure:

1
ERR The output of the process

The third argument is a boolean which tells the function if the output is error output or not. When true, the output is considered error output.

The fourth and fifth argument allow you to override the prefix for the normal output and error output respectively.

Stopping a Program

When a program is stopped, you can use stop() to notify this to the users:

1
2
3
4
5
6
7
8
// ...
$output->writeln(
    $debugFormatter->stop(
        spl_object_hash($process),
        'Some command description',
        $process->isSuccessful()
    )
);

This will output:

1
RES Some command description

In case of failure, this will be in red and in case of success it will be green.

Using multiple Programs

As said before, you can also use the helper to display more programs at the same time. Information about different programs will be shown in different colors, to make it clear which output belongs to which command.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Make sure your project is risk free

    Make sure your project is risk free

    Version:

    Table of Contents

    • Using the debug_formatter
    • Starting a Program
    • Output Progress Information
    • Stopping a Program
    • Using multiple Programs

    Symfony footer

    Avatar of Denis, a Symfony contributor

    Thanks Denis (@yethee) for being a Symfony contributor

    30 commits • 1.65K lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony