ICanBoogie/ICanBoogie v2.3.1
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Autoconfig
    • Core
    • HTTP
      • Dispatcher
    • Session

Classes

  • Core
  • Debug
  • Helpers
  • Logger
  • LogLevel
  • Session

Interfaces

  • LoggerInterface

Traits

  • LoggerTrait

Exceptions

  • AlreadyAuthenticated
  • AuthenticationRequired
  • CoreAlreadyBooted
  • CoreAlreadyInstantiated
  • CoreNotInstantiated
  • PermissionRequired
  • SecurityException

Constants

  • TOKEN_ALPHA
  • TOKEN_ALPHA_UPCASE
  • TOKEN_NUMERIC
  • TOKEN_SYMBOL
  • TOKEN_SYMBOL_WIDE

Functions

  • app
  • boot
  • excerpt
  • generate_token
  • generate_token_wide
  • get_autoconfig
  • log
  • log_error
  • log_info
  • log_success
  • log_time
  • normalize_namespace_part
  • pbkdf2
  • resolve_app_paths
  • strip_root
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie;

/**
 * A message logger using the core's session to store the messages.
 */
class Logger implements LoggerInterface
{
    const MAX_MESSAGES = 50;

    use LoggerTrait;

    static public function get_logger(Core $app)
    {
        static $logger;

        if (!$logger)
        {
            $logger = new static($app);
        }

        return $logger;
    }

    static private function format_messages(array $messages)
    {
        $rc = [];

        foreach ($messages as $message_and_context)
        {
            list($message, $context) = $message_and_context;

            $message = (string) $message;

            if ($context)
            {
                $message = format($message, $context);
            }

            $rc[] = $message;
        }

        return $rc;
    }

    private $app;

    public function __construct(Core $app)
    {
        $this->app = $app;
    }

    public function log($level, $message, array $context = [])
    {
        $messages = &$this->get_stash()[$level];
        $messages[] = [ $message, $context ];

        $count = count($messages);
        $max = self::MAX_MESSAGES;

        if ($count + 1 > $max)
        {
            $messages = array_splice($messages, $count - $max + 1);
            array_unshift($messages, [ '*** SLICED', [] ]);
        }
    }

    public function get_messages($level)
    {
        $messages = &$this->get_stash()[$level];

        if (!$messages)
        {
            return [];
        }

        return self::format_messages($messages);
    }

    public function fetch_messages($level)
    {
        $messages = $this->get_messages($level);

        $this->get_stash()[$level] = [];

        return $messages;
    }

    private function &get_stash()
    {
        $stash = &$this->app->session->icanboogie_logger;

        return $stash;
    }
}
ICanBoogie/ICanBoogie v2.3.1 API documentation generated by ApiGen