4

I am using composer in part of a project setup workflow. Each part of the setup will log its results and any other relevant info to file. I am having trouble capturing the output of composer install. When I run composer install directly from the command line the packages are correctly installed and I see the output in the command window. When I run it from a cmd file the packages are installed but the output is not saved to file, however, other composer commands (-V, help etc.) do have their output saved to file.

I know the help and -V commands do not need to cd but I left that in the test1 and test2 cmd files to rule out that had anything to do with the issue.

The following are the contents and results of a few .cmd files.

test1.cmd

@ECHO OFF
cd "C:\path\to\project"
composer -V > test1.txt

test1.txt contents

Composer version 1.2.1 2016-09-12 11:27:19

test2.cmd

@ECHO OFF
cd "C:\path\to\project"
composer help > test2.txt

test2.txt contents

Usage:
  help [options] [--] [<command_name>]

Arguments:
  command                        The command to execute
  command_name                   The command name [default: "help"]

Options:
      --xml                      To output help as XML

...truncated

test3.cmd

@ECHO OFF
cd "C:\path\to\project"
composer install > test3.txt

test3.txt is created but is an empty 0 kb file.

Why can I capture the output of some composer commands but not the install command? What is different about that particular command?

  • 2
    Perhaps *composer* prints on `install` only to __STDERR__ instead of __STDOUT__ and you need therefore `2>test3.txt`. See the Microsoft article [Using command redirection operators](https://technet.microsoft.com/en-us/library/bb490982.aspx). You could also use `composer install >test3.txt 2>&1` to get all messages printed to both standard handles redirected into the text file. – Mofi Oct 15 '16 at 15:30
  • 2
    @Mofi both `install 2>test3.txt` and `install >test3.txt 2>&1` worked. It appears Composer does output to **STDERR** when using the install command. Several links are returned when searching for that issue confirming what you said. https://github.com/composer/composer/issues/3795 – Steve Zuranski Oct 15 '16 at 20:21

3 Answers3

4

In bash: composer install --no-ansi &>log

Quolonel Questions
  • 6,603
  • 2
  • 32
  • 33
3

In my case, the Composer output was writing to STDERR rather than STDOUT, so piping directly to a file like this resulted in no output:

composer update --no-scripts >> out.txt

By adding 2>&1 to redirect the STDERR stream into the file, I was able to get the output I needed into the file.

composer update --no-scripts >> out.txt 2>&1

MarathonStudios
  • 2,849
  • 4
  • 20
  • 18
1

Since the question seems to be based on Windows and running composer from a batch script:

The composer batch script will quit a calling parent batch script, so you need to call composer with %comspec% /c composer from your batch script to avoid this.

To remove ansi output on Windows and redirect output to a log file, run:

%comspec% /c composer --no-ansi --no-interaction install >>composer.log 2>&1

You can also find your local composer.bat and edit the line php "%~dp0composer.phar" %* to be php "%~dp0composer.phar" %* --no-ansi

Depending on what scripts are called, there still may be ansi output if the scripts do not obey the --no-ansi option passed to composer. For example, in Laravel, it will still generate optimized autoload scripts and echo ansi output to the console even when calling composer --no-ansi install, when it runs php artisan package:discover --ansi.

Ref. https://stackoverflow.com/a/41599767/1925358

Ref. https://getcomposer.org/doc/03-cli.md

Tim M.
  • 105
  • 2
  • 8