valdeir2000

StackOverflow 266968 - Log.php

Jan 4th, 2018
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.70 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4.  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5.  *
  6.  * Licensed under The MIT License
  7.  * Redistributions of files must retain the above copyright notice.
  8.  *
  9.  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10.  * @link          https://cakephp.org CakePHP(tm) Project
  11.  * @since         0.2.9
  12.  * @license       https://opensource.org/licenses/mit-license.php MIT License
  13.  */
  14. namespace Cake\Log;
  15.  
  16. use Cake\Core\StaticConfigTrait;
  17. use Cake\Log\Engine\BaseLog;
  18. use InvalidArgumentException;
  19.  
  20. /**
  21.  * Logs messages to configured Log adapters. One or more adapters
  22.  * can be configured using Cake Logs's methods. If you don't
  23.  * configure any adapters, and write to Log, the messages will be
  24.  * ignored.
  25.  *
  26.  * ### Configuring Log adapters
  27.  *
  28.  * You can configure log adapters in your applications `config/app.php` file.
  29.  * A sample configuration would look like:
  30.  *
  31.  * ```
  32.  * Log::setConfig('my_log', ['className' => 'FileLog']);
  33.  * ```
  34.  *
  35.  * You can define the className as any fully namespaced classname or use a short hand
  36.  * classname to use loggers in the `App\Log\Engine` & `Cake\Log\Engine` namespaces.
  37.  * You can also use plugin short hand to use logging classes provided by plugins.
  38.  *
  39.  * Log adapters are required to implement `Psr\Log\LoggerInterface`, and there is a
  40.  * built-in base class (`Cake\Log\Engine\BaseLog`) that can be used for custom loggers.
  41.  *
  42.  * Outside of the `className` key, all other configuration values will be passed to the
  43.  * logging adapter's constructor as an array.
  44.  *
  45.  * ### Logging levels
  46.  *
  47.  * When configuring loggers, you can set which levels a logger will handle.
  48.  * This allows you to disable debug messages in production for example:
  49.  *
  50.  * ```
  51.  * Log::setConfig('default', [
  52.  *     'className' => 'File',
  53.  *     'path' => LOGS,
  54.  *     'levels' => ['error', 'critical', 'alert', 'emergency']
  55.  * ]);
  56.  * ```
  57.  *
  58.  * The above logger would only log error messages or higher. Any
  59.  * other log messages would be discarded.
  60.  *
  61.  * ### Logging scopes
  62.  *
  63.  * When configuring loggers you can define the active scopes the logger
  64.  * is for. If defined, only the listed scopes will be handled by the
  65.  * logger. If you don't define any scopes an adapter will catch
  66.  * all scopes that match the handled levels.
  67.  *
  68.  * ```
  69.  * Log::setConfig('payments', [
  70.  *     'className' => 'File',
  71.  *     'scopes' => ['payment', 'order']
  72.  * ]);
  73.  * ```
  74.  *
  75.  * The above logger will only capture log entries made in the
  76.  * `payment` and `order` scopes. All other scopes including the
  77.  * undefined scope will be ignored.
  78.  *
  79.  * ### Writing to the log
  80.  *
  81.  * You write to the logs using Log::write(). See its documentation for more information.
  82.  *
  83.  * ### Logging Levels
  84.  *
  85.  * By default Cake Log supports all the log levels defined in
  86.  * RFC 5424. When logging messages you can either use the named methods,
  87.  * or the correct constants with `write()`:
  88.  *
  89.  * ```
  90.  * Log::error('Something horrible happened');
  91.  * Log::write(LOG_ERR, 'Something horrible happened');
  92.  * ```
  93.  *
  94.  * ### Logging scopes
  95.  *
  96.  * When logging messages and configuring log adapters, you can specify
  97.  * 'scopes' that the logger will handle. You can think of scopes as subsystems
  98.  * in your application that may require different logging setups. For
  99.  * example in an e-commerce application you may want to handle logged errors
  100.  * in the cart and ordering subsystems differently than the rest of the
  101.  * application. By using scopes you can control logging for each part
  102.  * of your application and also use standard log levels.
  103.  */
  104. class Log
  105. {
  106.  
  107.     /**
  108.      * Writes the given message and type to all of the configured log adapters.
  109.      * Configured adapters are passed both the $level and $message variables. $level
  110.      * is one of the following strings/values.
  111.      *
  112.      * ### Levels:
  113.      *
  114.      * - `LOG_EMERG` => 'emergency',
  115.      * - `LOG_ALERT` => 'alert',
  116.      * - `LOG_CRIT` => 'critical',
  117.      * - `LOG_ERR` => 'error',
  118.      * - `LOG_WARNING` => 'warning',
  119.      * - `LOG_NOTICE` => 'notice',
  120.      * - `LOG_INFO` => 'info',
  121.      * - `LOG_DEBUG` => 'debug',
  122.      *
  123.      * ### Basic usage
  124.      *
  125.      * Write a 'warning' message to the logs:
  126.      *
  127.      * ```
  128.      * Log::write('warning', 'Stuff is broken here');
  129.      * ```
  130.      *
  131.      * ### Using scopes
  132.      *
  133.      * When writing a log message you can define one or many scopes for the message.
  134.      * This allows you to handle messages differently based on application section/feature.
  135.      *
  136.      * ```
  137.      * Log::write('warning', 'Payment failed', ['scope' => 'payment']);
  138.      * ```
  139.      *
  140.      * When configuring loggers you can configure the scopes a particular logger will handle.
  141.      * When using scopes, you must ensure that the level of the message, and the scope of the message
  142.      * intersect with the defined levels & scopes for a logger.
  143.      *
  144.      * ### Unhandled log messages
  145.      *
  146.      * If no configured logger can handle a log message (because of level or scope restrictions)
  147.      * then the logged message will be ignored and silently dropped. You can check if this has happened
  148.      * by inspecting the return of write(). If false the message was not handled.
  149.      *
  150.      * @param int|string $level The severity level of the message being written.
  151.      *    The value must be an integer or string matching a known level.
  152.      * @param mixed $message Message content to log
  153.      * @param string|array $context Additional data to be used for logging the message.
  154.      *  The special `scope` key can be passed to be used for further filtering of the
  155.      *  log engines to be used. If a string or a numerically index array is passed, it
  156.      *  will be treated as the `scope` key.
  157.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  158.      * @return bool Success
  159.      * @throws \InvalidArgumentException If invalid level is passed.
  160.      */
  161.     public static function write($level, $message, $context = [])
  162.     {
  163.         static::_init();
  164.         if (is_int($level) && in_array($level, static::$_levelMap)) {
  165.             $level = array_search($level, static::$_levelMap);
  166.         }
  167.  
  168.         if (!in_array($level, static::$_levels)) {
  169.             throw new InvalidArgumentException(sprintf('Invalid log level "%s"', $level));
  170.         }
  171.  
  172.         $logged = false;
  173.         $context = (array)$context;
  174.         if (isset($context[0])) {
  175.             $context = ['scope' => $context];
  176.         }
  177.         $context += ['scope' => []];
  178.  
  179.         foreach (static::$_registry->loaded() as $streamName) {
  180.             $logger = static::$_registry->{$streamName};
  181.             $levels = $scopes = null;
  182.  
  183.             if ($logger instanceof BaseLog) {
  184.                 $levels = $logger->levels();
  185.                 $scopes = $logger->scopes();
  186.             }
  187.             if ($scopes === null) {
  188.                 $scopes = [];
  189.             }
  190.  
  191.             $correctLevel = empty($levels) || in_array($level, $levels);
  192.             $inScope = $scopes === false && empty($context['scope']) || $scopes === [] ||
  193.                 is_array($scopes) && array_intersect((array)$context['scope'], $scopes);
  194.  
  195.             if ($correctLevel && $inScope) {
  196.                 $logger->log($level, $message, $context);
  197.                 $logged = true;
  198.             }
  199.         }
  200.  
  201.         return $logged;
  202.     }
  203.  
  204.     /**
  205.      * Convenience method to log emergency messages
  206.      *
  207.      * @param string $message log message
  208.      * @param string|array $context Additional data to be used for logging the message.
  209.      *  The special `scope` key can be passed to be used for further filtering of the
  210.      *  log engines to be used. If a string or a numerically index array is passed, it
  211.      *  will be treated as the `scope` key.
  212.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  213.      * @return bool Success
  214.      */
  215.     public static function emergency($message, $context = [])
  216.     {
  217.         return static::write(__FUNCTION__, $message, $context);
  218.     }
  219.  
  220.     /**
  221.      * Convenience method to log alert messages
  222.      *
  223.      * @param string $message log message
  224.      * @param string|array $context Additional data to be used for logging the message.
  225.      *  The special `scope` key can be passed to be used for further filtering of the
  226.      *  log engines to be used. If a string or a numerically index array is passed, it
  227.      *  will be treated as the `scope` key.
  228.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  229.      * @return bool Success
  230.      */
  231.     public static function alert($message, $context = [])
  232.     {
  233.         return static::write(__FUNCTION__, $message, $context);
  234.     }
  235.  
  236.     /**
  237.      * Convenience method to log critical messages
  238.      *
  239.      * @param string $message log message
  240.      * @param string|array $context Additional data to be used for logging the message.
  241.      *  The special `scope` key can be passed to be used for further filtering of the
  242.      *  log engines to be used. If a string or a numerically index array is passed, it
  243.      *  will be treated as the `scope` key.
  244.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  245.      * @return bool Success
  246.      */
  247.     public static function critical($message, $context = [])
  248.     {
  249.         return static::write(__FUNCTION__, $message, $context);
  250.     }
  251.  
  252.     /**
  253.      * Convenience method to log error messages
  254.      *
  255.      * @param string $message log message
  256.      * @param string|array $context Additional data to be used for logging the message.
  257.      *  The special `scope` key can be passed to be used for further filtering of the
  258.      *  log engines to be used. If a string or a numerically index array is passed, it
  259.      *  will be treated as the `scope` key.
  260.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  261.      * @return bool Success
  262.      */
  263.     public static function error($message, $context = [])
  264.     {
  265.         return static::write(__FUNCTION__, $message, $context);
  266.     }
  267.  
  268.     /**
  269.      * Convenience method to log warning messages
  270.      *
  271.      * @param string $message log message
  272.      * @param string|array $context Additional data to be used for logging the message.
  273.      *  The special `scope` key can be passed to be used for further filtering of the
  274.      *  log engines to be used. If a string or a numerically index array is passed, it
  275.      *  will be treated as the `scope` key.
  276.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  277.      * @return bool Success
  278.      */
  279.     public static function warning($message, $context = [])
  280.     {
  281.         return static::write(__FUNCTION__, $message, $context);
  282.     }
  283.  
  284.     /**
  285.      * Convenience method to log notice messages
  286.      *
  287.      * @param string $message log message
  288.      * @param string|array $context Additional data to be used for logging the message.
  289.      *  The special `scope` key can be passed to be used for further filtering of the
  290.      *  log engines to be used. If a string or a numerically index array is passed, it
  291.      *  will be treated as the `scope` key.
  292.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  293.      * @return bool Success
  294.      */
  295.     public static function notice($message, $context = [])
  296.     {
  297.         return static::write(__FUNCTION__, $message, $context);
  298.     }
  299.  
  300.     /**
  301.      * Convenience method to log debug messages
  302.      *
  303.      * @param string $message log message
  304.      * @param string|array $context Additional data to be used for logging the message.
  305.      *  The special `scope` key can be passed to be used for further filtering of the
  306.      *  log engines to be used. If a string or a numerically index array is passed, it
  307.      *  will be treated as the `scope` key.
  308.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  309.      * @return bool Success
  310.      */
  311.     public static function debug($message, $context = [])
  312.     {
  313.         return static::write(__FUNCTION__, $message, $context);
  314.     }
  315.  
  316.     /**
  317.      * Convenience method to log info messages
  318.      *
  319.      * @param string $message log message
  320.      * @param string|array $context Additional data to be used for logging the message.
  321.      *  The special `scope` key can be passed to be used for further filtering of the
  322.      *  log engines to be used. If a string or a numerically index array is passed, it
  323.      *  will be treated as the `scope` key.
  324.      *  See Cake\Log\Log::setConfig() for more information on logging scopes.
  325.      * @return bool Success
  326.      */
  327.     public static function info($message, $context = [])
  328.     {
  329.         return static::write(__FUNCTION__, $message, $context);
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment