1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26abstract class AbstractLoggerCore
27{
28    public $level;
29    protected $level_value = [
30        0 => 'DEBUG',
31        1 => 'INFO',
32        2 => 'WARNING',
33        3 => 'ERROR',
34    ];
35
36    const DEBUG = 0;
37    const INFO = 1;
38    const WARNING = 2;
39    const ERROR = 3;
40
41    public function __construct($level = self::INFO)
42    {
43        if (array_key_exists((int) $level, $this->level_value)) {
44            $this->level = $level;
45        } else {
46            $this->level = self::INFO;
47        }
48    }
49
50    /**
51     * Log the message.
52     *
53     * @param string message
54     * @param level
55     */
56    abstract protected function logMessage($message, $level);
57
58    /**
59     * Check the level and log the message if needed.
60     *
61     * @param string message
62     * @param level
63     */
64    public function log($message, $level = self::DEBUG)
65    {
66        if ($level >= $this->level) {
67            $this->logMessage($message, $level);
68        }
69    }
70
71    /**
72     * Log a debug message.
73     *
74     * @param string message
75     */
76    public function logDebug($message)
77    {
78        $this->log($message, self::DEBUG);
79    }
80
81    /**
82     * Log an info message.
83     *
84     * @param string message
85     */
86    public function logInfo($message)
87    {
88        $this->log($message, self::INFO);
89    }
90
91    /**
92     * Log a warning message.
93     *
94     * @param string message
95     */
96    public function logWarning($message)
97    {
98        $this->log($message, self::WARNING);
99    }
100
101    /**
102     * Log an error message.
103     *
104     * @param string message
105     */
106    public function logError($message)
107    {
108        $this->log($message, self::ERROR);
109    }
110}
111