Log - change filename dynamically

Hi everyone!
I use the same app from two different domains.
I would ask how can i modify the filename of the error log depend on the domain? For example one.com → one_error.log;
two.com → two_error.log.

I would create a custom middleware which sets/overwrites the Log.error.file config with whatever domain is currently present inside the $request variable.
See Middleware - 4.x

I’d probably put a wee bit of code at the top of config/app.php that determines which domain you’re serving, and then set the Log config in that file conditionally based on that.

Also legit if you have no problems dealing with the superglobal variables :smiley:

Thank you everyone!
What do you think about this simple solution in bootstrap.php: Configure::write(‘Log.error.file’,‘error_’.env(‘HTTP_HOST’));

You should avoid that, as the Host header can be spoofed. Only use it after validating its value, or outright avoid using its value directly, and instead use it for obtaining a value from a map.

1 Like