You are here

LogMessage.php in Apigee Edge 8

File

modules/apigee_edge_actions/src/Plugin/RulesAction/LogMessage.php
View source
<?php

/**
 * Copyright 2020 Google Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */
namespace Drupal\apigee_edge_actions\Plugin\RulesAction;

use Drupal\Core\Annotation\ContextDefinition;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Core\Annotation\RulesAction;
use Drupal\rules\Core\RulesActionBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a 'Log a message' action.
 *
 * @RulesAction(
 *   id = "apigee_edge_actions_log_message",
 *   label = @Translation("Log a message"),
 *   category = @Translation("Apigee"),
 *   context_definitions = {
 *     "message" = @ContextDefinition("string",
 *       label = @Translation("Message")
 *     ),
 *     "level" = @ContextDefinition("string",
 *       label = @Translation("Level"),
 *       description = @Translation("Specify a log level. Example: notice, error, info, warning or debug"),
 *       default_value = "notice"
 *     )
 *   }
 * )
 */
class LogMessage extends RulesActionBase implements ContainerFactoryPluginInterface {

  /**
   * The logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * LogMessage constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('logger.channel.apigee_edge_actions'));
  }

  /**
   * Executes the action.
   *
   * @param string $message
   *   The message.
   * @param string $level
   *   The log level.
   */
  protected function doExecute(string $message, string $level = "notice") {
    if (method_exists($this->logger, $level)) {
      $this->logger
        ->{$level}($message);
    }
  }

}

Classes

Namesort descending Description
LogMessage Defines a 'Log a message' action.