Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect Output of Composer Install to Text File

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?

like image 489
Steve Zuranski Avatar asked Oct 20 '25 14:10

Steve Zuranski


2 Answers

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

like image 86
Quolonel Questions Avatar answered Oct 23 '25 05:10

Quolonel Questions


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

like image 45
Tim M. Avatar answered Oct 23 '25 03:10

Tim M.