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 file
42 Replies
vince
vinceOP3mo ago
I'm seeing file_put_contents(), could that work?
ἔρως
ἔρως3mo ago
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 ...
vince
vinceOP3mo ago
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
ἔρως
ἔρως3mo ago
that's a very "depends"
vince
vinceOP3mo ago
Yup 😅 and I think the guy who set it up is no longer here so... 😂
ἔρως
ἔρως3mo ago
also, i think it uses composer already
vince
vinceOP3mo ago
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 😂
ἔρως
ἔρως3mo ago
which version is it?
vince
vinceOP3mo ago
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 sysadmin
ἔρως
ἔρως3mo ago
so, if you have a separate api, you need to check what it is written in, if that's where the issues are
vince
vinceOP3mo ago
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 there
ἔρως
ἔρως3mo ago
what compiled files?
vince
vinceOP3mo ago
If index.php is include-ing the logger library, wouldn't I have to compile it?
ἔρως
ἔρως3mo ago
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 too
vince
vinceOP3mo ago
Ohhh 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
ἔρως
ἔρως3mo ago
yeah, php isn't like that php re-compiles the files every time (unless it has opcache or something like that)
vince
vinceOP3mo ago
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?
ἔρως
ἔρως3mo ago
no no you don't have to install composer
vince
vinceOP3mo ago
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
ἔρως
ἔρως3mo ago
no, you don't
vince
vinceOP3mo ago
Well now I'm confused 😅
ἔρως
ἔρως3mo ago
https://getcomposer.org/download/ <-- you just need to run the 4 commands there, and you can use the composer.phar file in your pc
vince
vinceOP3mo ago
Ah okay got it, but I'd still need to generate a composer.json right so the code knows what dependencies it needs?
ἔρως
ἔρως3mo ago
if you have to do it in the server, DELETE THE COMPOSER.PHAR FILE it generates when you run composer require <package>
vince
vinceOP3mo ago
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 automatically
ἔρως
ἔρως3mo ago
nope, you don't need that just have composer.phar somewhere, run php phar composer.phar require <package> and done
vince
vinceOP3mo ago
Thank you! Learned a lot today 🙂 I'll let you know how I crack on
ἔρως
ἔρως3mo ago
you know what's the easiest way to do it?
ἔρως
ἔρως3mo ago
an empty directory in your pc
No description
vince
vinceOP3mo ago
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
ἔρως
ἔρως3mo ago
you have to be careful you need to know the php version in the server
vince
vinceOP3mo ago
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
ἔρως
ἔρως3mo ago
yes, and then you need to specify the php version to composer
ἔρως
ἔρως3mo ago
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
vince
vinceOP3mo ago
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 else
Jochem
Jochem3mo ago
https://pear.php.net/package/Log This is what I've always used
vince
vinceOP3mo ago
ty! never even heard of pear haha
ἔρως
ἔρως3mo ago
pear is really good too told you to fix the bug right here
vince
vinceOP3mo ago
ohhh i read that but didn't process it haha
Jochem
Jochem3mo ago
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)
vince
vinceOP3mo ago
i don't very much like pears irl... 🤭
ἔρως
ἔρως3mo ago
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
Want results from more Discord servers?
Add your server