How do you use Phalcon’s built-in logging services?

How do you use Phalcon’s built-in logging services?

To use Phalcon’s built-in logging services, follow these steps:

1. Register the Logger Service

First, register the logger service in the Dependency Injection (DI) container. You can configure the logger to write logs to a file.

Example

<?php
use Phalcon\Logger;
use Phalcon\Logger\Adapter\Stream;
use Phalcon\Di\FactoryDefault;

$di = new FactoryDefault();

$di->set('logger', function() {
    $adapter = new Stream('../app/logs/application.log');
    return new Logger('messages', ['main' => $adapter]);
});
?>

2. Use the Logger in Controllers or Services

Once the logger is registered, you can access and use it in your controllers, services, or models.

Example

<?php
class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        // Log an info message
        $this->logger->info("This is an info log message.");

        // Log an error message
        $this->logger->error("This is an error log message.");
    }
}
?>

3. Log Levels

Phalcon’s logger supports multiple log levels such as DEBUG, INFO, NOTICE, WARNING, ERROR, etc.

Example

<?php
$this->logger->debug("Debugging information");
$this->logger->warning("Warning message");
$this->logger->critical("Critical error");
?>

Related Questions & Topics

Powered and designed by igetvapeaustore.com | © 2024 codestap.com.