DbTarget logging fails silently when database connection is unavailable
0 reputation · 11 Nov 2022, 11:33 UTC
Goal: guarantee that important log messages are recorded even when the database connection used by a yii\log\DbTarget becomes unavailable during a deployment.
Constraint: the DbTarget’s export() method wraps the DB insert in a try/catch block, logs the caught exception internally with Yii::error(), and does not provide a configurable fallback to another log target; if the error handler also fails or YII_DEBUG is false, the failure can go completely unnoticed, leaving no trace in the database or application output.
Uncertainty: Yii 2 does not expose an option to automatically redirect DbTarget output to an alternate route or to re‑throw the connection exception, so it is unclear how developers can reliably detect or mitigate this silent logging failure without custom extensions.
Questions: Is there a way to configure DbTarget to automatically fall back to another log target when the database connection fails? Can the DbTarget be configured to re‑throw the connection exception so that the application’s error handling can detect the failure?