Error Handling & Logging
Yabasi framework provides robust error handling and logging capabilities to help you manage and debug your application effectively.
Error Handling
Yabasi uses a custom exception handler to manage errors gracefully. Here's how you can configure and use it:
use Yabasi\Logging\CustomExceptionHandler;
$config = $this->container->get('config');
$this->exceptionHandler = new CustomExceptionHandler(
$config->get('app.debug', false)
);
Handling Exceptions
You can use the custom exception handler to manage exceptions in your application:
public function exampleAction()
{
try {
// Your code here
} catch (\Exception $e) {
$this->exceptionHandler->handle($e);
}
}
Logging
Yabasi provides a powerful logging system through the Logger class. Here's how to use it:
use Yabasi\Logging\Logger;
$logger = new Logger($config);
$logger->info('This is an informational message');
$logger->warning('This is a warning');
$logger->error('An error occurred', ['error_code' => 500]);
$logger->critical('A critical error occurred');
Log Levels
Yabasi supports various log levels, allowing you to categorize your log messages:
- emergency
- alert
- critical
- error
- warning
- notice
- info
- debug
Custom Log Files
You can create custom log files for different parts of your application:
$apiLogger = new Logger($config, 'api');
$apiLogger->info('API request received', ['endpoint' => '/users']);
Best Practices
- Use appropriate log levels to categorize your messages.
- Include relevant context data in your log messages.
- Implement proper error handling to catch and log exceptions.
- Regularly review and analyze your log files.
- Use custom log files for different components or services in your application.
- Configure log rotation to manage log file sizes.
Proper error handling and logging are crucial for maintaining and debugging your Yabasi application. By leveraging these features, you can quickly identify and resolve issues, improving the overall reliability of your application.