CodeIgniter Forums

Full Version: Should CodeIgniter\Log\FileHandler support [schema]://[path]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

I was wondering if the CodeIgniter\Log\FileHandler should support streaming to [schema]://[path] (eg. php://stdout)?  

Would love to work on a PR for this as I would really love to start contributing to CI4.  Smile (Picked up CI after many years and I must say that I love what you are doing and the direction you have taken)

I have lots more ideas and suggestions, but I have to start somewhere.
Maybe adding a CodeIgniter\Log\StreamHandler would be a better solution?
I think a StreamHandler is the way to go. I'm curious about how do you see the "stream" being used?
(03-27-2020, 02:12 PM)dave friend Wrote: [ -> ]I think a StreamHandler is the way to go. I'm curious about how do you see the "stream" being used?

Everything started with me having a Docker setup in which I collect the stdout from all containers and combine it to one (way of centralizing logs while developing). While looking at the source co of the FileHandler I came across this line:

PHP Code:
$filepath $this->path 'log-' date('Y-m-d') . '.' $this->fileExtension

and realized that setting the path to "php://stdout" is not going to work, because the handler will always append the 'log-'... string as the file name.
That is why there was a need for php://stdout. Looking at the PHP wrapper made me think if some other wrappers would need to be supported.
Supporting  zlib:// or even ssh2:// would also be handy. 

The thing with supporting other wrappers comes with a price and that would be lots of fs, network, ... IO. 
How can we optimize the IO mechanism in a way that we write just once to our configured path (eg. zip://./!?

How about this... 
Logger->log writes to php://temp and the Logger class also registers a shutdown function that reads logs from php://temp and writes to all log handlers in the queue (making some decisions based on the level).

Here is a simple example, to better explain my idea:

PHP Code:

// Memory file handle would be cached as a property of the Logger shared instance
$fp fopen("php://temp/maxmemory:4096"'a+'); // We can use php://memory or php://temp

// Push our dumper to the shutdown function queue
// Each 
register_shutdown_function(function() use ($fp) {
// Here we can loop trough our log handlers queue
    // and write log messages (with levels handlers are subscribed to) to the handler's open file handles
fpassthru($fp); // fpassthru is here just for testing the output :)

for (
$x 0$x <= 100$x++) {
// No need for locking, because the memory file is isolated to only this process
fwrite($fpsprintf("ERROR #%d\n"$x));


Edit: a+ mode would be more appropriate Smile