Is there a quick way in vanilla PHP to log things?
I have only the basic PHP extensions available in the default PHP installation. Just wondering if there was a quick and easy way to log data in the backend? I'm using
error_log
but that's stopping the application it seems when I use it so need something that won't stop the app and log the data to a file42 Replies
I'm seeing
file_put_contents()
, could that work?well, you can either use a logging library or store everything in mysql using the
archive
engine
https://dev.mysql.com/doc/refman/8.4/en/archive-storage-engine.html
and you just throw everything in it
or ... use xdebug to run it locally and fix all the bugs - warning, exceptions, errors, notices ...Yea wanted to run it locally with ddev but I don't even know how it's really setup. There's the base site in a really old version of ExpressionEngine CMS and then there's a separate directory in CP for the api's... so not even sure how I could get that working locally without a lot of headache
Maybe adding composer and a logging library is the way to go... but again I don't even know how the heck they set this up in CP so I don't know if there's a lot of lift to add composer and how to get the app to run those specific compiled files in CP
that's a very "depends"
Yup 😅 and I think the guy who set it up is no longer here so... 😂
also, i think it uses composer already
Alright well I've got some stuff I can research, thanks Epic! I'll come back here if there's any updates but I might just say screw it and keep using
error_log
to test and then removing it after 😂which version is it?
Yea I think it does, but the EE stuff is in a separate directory in CP e.g something like,
website.com
. The API stuff that I'm working on is api.website.com
(separate directory outside of the website.com
one) so I'd have to add composer to that one
I'm not sure how the 2 are connected, I'm assuming apache? I don't know if apache handles that type of stuff... totally new to sysadminso, if you have a separate api, you need to check what it is written in, if that's where the issues are
It's in PHP, but where my confusion would be is, I can add composer no problem to that directory. And then include the logging library into the main
index.php
where we're handling the api stuff and log as needed.
My issue then is, how do I actually get the server to run the compiled files (with library included) instead of just the regular index.php
. As you said, definitely #depends, I just don't know how it was set up so I don't think you can help me therewhat compiled files?
If
index.php
is include
-ing the logger library, wouldn't I have to compile it?no
all you have to do is what composer tells you, which is to include the compoer autoloader
that's it
and make sure that the
vendor
folder is in the server tooOhhh maybe I'm getting confused on typical JS ecosystem stuff. Since we have bundlers and things like that, it compiles things into 1 file (example) and maybe that's where I'm getting confused
If we already have the files from composer it makes sense nothing would need to be compiled
yeah, php isn't like that
php re-compiles the files every time
(unless it has opcache or something like that)
Awesome, thank you! So I'd just have to get composer installed, install the library, and then include it and I'm off to the races?
no no
you don't have to install composer
I do since the directory I'm working in doesn't have it installed / composer.json (or .yml or whatever extension it uses I forget)
Right?
Neither does the parent dir
no, you don't
Well now I'm confused 😅
https://getcomposer.org/download/ <-- you just need to run the 4 commands there, and you can use the
composer.phar
file in your pcAh okay got it, but I'd still need to generate a composer.json right so the code knows what dependencies it needs?
if you have to do it in the server, DELETE THE COMPOSER.PHAR FILE
it generates when you run
composer require <package>
Ohhhhh okay awesome
I thought I'd need to do something like
composer init
like npm init
kinda, but probably same thing happens when you install a npm package it does it automaticallynope, you don't need that
just have composer.phar somewhere, run
php phar composer.phar require <package>
and doneThank you! Learned a lot today 🙂 I'll let you know how I crack on
you know what's the easiest way to do it?
an empty directory in your pc
Ye I actually have a git repo with the api files so I'll just do it locally and upload the required files (composer.json) to the server through ssh
or cp
you have to be careful
you need to know the php version in the server
Oooof that's a good point, ty! I think it's 7.x since EE 3.x can only run on 7 iirc
I'll have to double check tho for sure
yes, and then you need to specify the php version to composer
Stack Overflow
Tell Composer to use Different PHP Version
I've been stuck at this for a few days. I'm using 1and1 hosting, and they have their PHP set up a bit weird.
If I use just php composer.phar install, then I'm using PHP 4.4.6, which is horribly
Oh, I fixed it! The error I was getting wasn't because the application was being stopped by
error_log
(which I thought was default behavior of some kind) - it was because I was redeclaring a variable that was needed somewhere elsehttps://pear.php.net/package/Log This is what I've always used
ty! never even heard of pear haha
pear is really good too
told you to fix the bug
right here
ohhh i read that but didn't process it haha
pear is more like pip, where composer is more like npm. Pear is system wide, so it has some disadvantages, but it's also the way to extend PHP (in combination with
pecl
)i don't very much like pears irl... 🤭
i like them green and not soft
but yes, pear is the best for system-wide stuff
usually, very very cheap vps are shared and managed by a whm account, which means that you don't get system-wide access
in those cases, composer does everything
also, composer is much easier to throw in a repo and distribute
if you have to replicate it locally, it's a lot easier
pip has something that pear doesn't: requirements.txt and virtual environments to prevent global changes