logging - write PHP command line log to file like in Python -


in python, can put command line log file instead of console using command:

python script.py >> mylogfile.txt 

how can using php? i've tried

php script.php >> mylogfile.txt 

but doesn't work.

just fyi, might helps, use windows 10.

finally found answer. it's based on article: https://www.sitepoint.com/php-command-line-1-2/

i first used php script.php > mylog.txt returns of log text console, thought it's not writing log, does. wanted php script.php > mylog.txt 2>&1 put log file.

the article says doesn't work in windows, use windows 10 , works.

the error messages mixed normal output before. piping output script, can split errors normal output:

$ php outwitherrors.php 2> errors.log

this time, you’ll see these messages:

opening file foobar.log job finished

but, if directory in ran script, new file called errors.log have been created, containing error message. number 2 command line handle used identify stderr. note 1 handle stdout, while 0 handle stderr. using > symbol command line, can direct output particular location.

although may not seem exciting, it’s handy tool system administration. simple application, running script cron, following:

$ php outwitherrors.php >> transaction.log 2>> errors.log 

using ‘>>‘, tell terminal append new messages existing log (rather overwrite it). normal operational messages logged transaction.log, might peruse once month, check everything’s ok. meanwhile, errors need quicker response end in errors.log, other cron job might email me on daily basis (or more frequently) required.

there’s 1 difference between unix , windows command lines, when comes piping output, of should aware. on unix, can merge stdout , stderr streams single destination, example:

$ php outwitherrors.php > everything.log 2>&1 

what re-route stderr stdout, meaning both written log file everything.log.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -