Phalcon Logger throws 'Log directory does not exist' when target directory missing
0 reputation · 20 Dec 2021, 19:22 UTC
0 reputation · 20 Dec 2021, 19:22 UTC
The goal is to configure the Phalcon Logger component so that logging continues reliably even when the configured log directory is absent or unwritable, while still emitting only meaningful alerts and avoiding unnecessary notification noise.
Constraints include preserving the existing log level routing (e.g., Stream for errors, File for info), not masking genuine configuration problems, and ensuring that any automatic directory creation does not introduce security risks or permission conflicts in shared hosting environments.
The error is thrown when the Stream (or File in older releases) adapter is constructed and the target directory does not exist. The exception occurs before any log message is written, so the application must catch it at the point of adapter creation.
During construction the adapter checks that the directory part of the configured path is a real directory and is writable. If the directory is missing the constructor raises a Phalcon\Logger\Exception with the message “Log directory does not exist”.
Stream adapter for file logging; older versions use File.log() call.try / catch (\Phalcon\Logger\Exception $e) block.is_writable() for the PHP process user.mkdir($parent, 0750, true) and retry creating the primary adapter.Syslog or, if available, a no‑op adapter) and log a single error‑level notice explaining the missing directory.Is the parent directory of the configured log path writable by the PHP process user? If not, automatic creation is not viable and a fallback adapter is the appropriate path.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 21 Dec 2021, 02:12 UTC
One thing worth adding to the existing answer: the Stream adapter throws Phalcon\Logger\Exception in its constructor, so the try/catch must wrap adapter creation, not the log() call. A snippet that builds the adapter outside the try block will fatal before any fallback logic runs.
try {
$adapter = new \Phalcon\Logger\Adapter\Stream('/var/log/app/app.log');
} catch (\Phalcon\Logger\Exception $e) {
$adapter = new \Phalcon\Logger\Adapter\Syslog('app');
$adapter->error('Log directory missing, using syslog fallback');
}
$logger = new \Phalcon\Logger('app', [$adapter]);
Also, per-adapter level filtering isn't a constructor option in the standard Phalcon 4/5 API; levels are set on the Logger itself, so run separate logger instances if you need different thresholds per adapter. Verify against your installed version's adapter signatures.