/** * Writes the given message and type to all of the configured log adapters. * Configured adapters are passed both the $level and $message variables. $level * is one of the following strings/values. * * ### Levels: * * - `LOG_EMERG` => 'emergency', * - `LOG_ALERT` => 'alert', * - `LOG_CRIT` => 'critical', * - `LOG_ERR` => 'error', * - `LOG_WARNING` => 'warning', * - `LOG_NOTICE` => 'notice', * - `LOG_INFO` => 'info', * - `LOG_DEBUG` => 'debug', * * ### Basic usage * * Write a 'warning' message to the logs: * * ``` * Log::write('warning', 'Stuff is broken here'); * ``` * * ### Using scopes * * When writing a log message you can define one or many scopes for the message. * This allows you to handle messages differently based on application section/feature. * * ``` * Log::write('warning', 'Payment failed', ['scope' => 'payment']); * ``` * * When configuring loggers you can configure the scopes a particular logger will handle. * When using scopes, you must ensure that the level of the message, and the scope of the message * intersect with the defined levels & scopes for a logger. * * ### Unhandled log messages * * If no configured logger can handle a log message (because of level or scope restrictions) * then the logged message will be ignored and silently dropped. You can check if this has happened * by inspecting the return of write(). If false the message was not handled. * * @param int|string $level The severity level of the message being written. * The value must be an integer or string matching a known level. * @param mixed $message Message content to log * @param string|array $context Additional data to be used for logging the message. * The special `scope` key can be passed to be used for further filtering of the * log engines to be used. If a string or a numerically index array is passed, it * will be treated as the `scope` key. * See Cake\Log\Log::config() for more information on logging scopes. * @return bool Success * @throws \InvalidArgumentException If invalid level is passed. */ public static function write($level, $message, $context = []) { static::_init(); if (is_int($level) && in_array($level, static::$_levelMap)) { $level = array_search($level, static::$_levelMap); } if (!in_array($level, static::$_levels)) { throw new InvalidArgumentException(sprintf('Invalid log level "%s"', $level)); } $logged = false; $context = (array) $context; if (isset($context[0])) { $context = ['scope' => $context]; } $context += ['scope' => []]; foreach (static::$_registry->loaded() as $streamName) { $logger = static::$_registry->{$streamName}; $levels = $scopes = null; if ($logger instanceof BaseLog) { $levels = $logger->levels(); $scopes = $logger->scopes(); } if ($scopes === null) { $scopes = []; } $correctLevel = empty($levels) || in_array($level, $levels); $inScope = $scopes === false && empty($context['scope']) || $scopes === [] || is_array($scopes) && array_intersect($context['scope'], $scopes); if ($correctLevel && $inScope) { $logger->log($level, $message, $context); $logged = true; } } return $logged; }
/** * Writes the given message and type to all of the configured log adapters. * Configured adapters are passed both the $level and $message variables. $level * is one of the following strings/values. * * ### Levels: * * - `LOG_EMERG` => 'emergency', * - `LOG_ALERT` => 'alert', * - `LOG_CRIT` => 'critical', * - `LOG_ERR` => 'error', * - `LOG_WARNING` => 'warning', * - `LOG_NOTICE` => 'notice', * - `LOG_INFO` => 'info', * - `LOG_DEBUG` => 'debug', * * ### Basic usage * * Write a 'warning' message to the logs: * * `Log::write('warning', 'Stuff is broken here');` * * ### Using scopes * * When writing a log message you can define one or many scopes for the message. * This allows you to handle messages differently based on application section/feature. * * `Log::write('warning', 'Payment failed', 'payment');` * * When configuring loggers you can configure the scopes a particular logger will handle. * When using scopes, you must ensure that the level of the message, and the scope of the message * intersect with the defined levels & scopes for a logger. * * ### Unhandled log messages * * If no configured logger can handle a log message (because of level or scope restrictions) * then the logged message will be ignored and silently dropped. You can check if this has happened * by inspecting the return of write(). If false the message was not handled. * * @param int|string $level The severity level of the message being written. * The value must be an integer or string matching a known level. * @param string $message Message content to log * @param string|array $scope The scope(s) a log message is being created in. * See Cake\Log\Log::config() for more information on logging scopes. * @return bool Success * @throws \Cake\Error\Exception If invalid level is passed. */ public static function write($level, $message, $scope = array()) { static::_init(); if (is_int($level) && isset(static::$_levels[$level])) { $level = static::$_levels[$level]; } if (!in_array($level, static::$_levels)) { throw new Error\Exception(sprintf('Invalid log level "%s"', $level)); } $logged = false; foreach (static::$_registry->loaded() as $streamName) { $logger = static::$_registry->{$streamName}; $levels = $scopes = null; if ($logger instanceof BaseLog) { $levels = $logger->levels(); $scopes = $logger->scopes(); } $correctLevel = empty($levels) || in_array($level, $levels); $inScope = empty($scopes) || count(array_intersect((array) $scope, $scopes)) > 0; if ($correctLevel && $inScope) { $logger->write($level, $message, $scope); $logged = true; } } return $logged; }