Zap
Documentation / Utilities

Logger

Zap uses Logger to record application exceptions. The framework's exception handler writes uncaught errors automatically; use the same utility when your code catches an exception and still needs to record it.

Framework error logging

Zap's ExceptionHandler calls Logger::error() when it renders an uncaught exception. By default, the logger appends entries to storage/logs/errors.log, creating the log directory when needed. The record includes the timestamp, exception class and message, source file and line, and stack trace.

In production, the exception handler returns a generic HTTP 500 response while retaining diagnostic details in the server-side log. In development, it also renders detailed exception information in the response to help with local debugging.

If writing to the application log fails, the exception handler reports the logging failure and original exception through PHP's error_log().

Log an exception you catch

Logger::error() accepts a Throwable. Use it when an exception is handled locally but the failure should remain available for diagnosis. The method returns no value and throws a RuntimeException if it cannot create the log directory or append to the log file.

use Zap\Core\Utils\Logger;

try {
 $reportService->generate($reportId);
} catch (Throwable $exception) {
 app(Logger::class)->error($exception);

 return $this->text('Unable to generate the report.', 500);
}

Catch only failures your action can handle. If a failure should be handled by Zap's global exception handler, allow the exception to propagate instead of catching and suppressing it.

Choose a custom log file

You can pass an optional path to the constructor. This is useful for a dedicated application log, but keep the destination outside public web directories and ensure the PHP process has permission to create and append to it.

use Zap\Core\Utils\Logger;

$logger = new Logger(
 BASE_PATH . '/storage/logs/billing-errors.log'
);

try {
 $billingService->charge($invoice);
} catch (Throwable $exception) {
 $logger->error($exception);
}

The Logger currently provides the error(Throwable) operation for exceptions; it is not a general-purpose logger with methods for arbitrary levels or messages.

Keep logs private and useful

  • Keep logs under private storage, never under a publicly served directory.
  • Restrict filesystem permissions so only the application and authorized operators can read logs.
  • Do not include passwords, session identifiers, API keys, or other secrets in exception messages.
  • Configure log rotation and retention appropriate to your deployment.
  • Show users a generic failure message; use logs for authorized server-side diagnosis.
Next: read environment configuration

Continue with the Envy utility guide.

Envy utility