Passed
Push — develop ( 308ae7...12f3df )
by Портнов
04:25
created

Logger::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 15
rs 9.9
cc 2
nc 2
nop 2
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\Modules;
21
22
use MikoPBX\Core\System\System;
23
use MikoPBX\Core\System\Util;
24
use Phalcon\Logger\Adapter\Stream as FileLogger;
25
26
class Logger
27
{
28
    public bool $debug;
29
    private \Phalcon\Logger $logger;
30
    private string $module_name;
31
32
    /**
33
     * Logger constructor.
34
     *
35
     * @param string $class
36
     * @param string $module_name
37
     */
38
    public function __construct(string $class, string $module_name)
39
    {
40
        $this->module_name = $module_name;
41
        $this->debug    = true;
42
        $logPath        = System::getLogDir() . '/' . $this->module_name . '/';
43
        if (!is_dir($logPath)){
44
            Util::mwMkdir($logPath);
45
            Util::addRegularWWWRights($logPath);
46
        }
47
        $logFile  = $logPath . $class . '.log';
48
        $adapter       = new FileLogger($logFile);
49
        $this->logger  = new \Phalcon\Logger(
50
            'messages',
51
            [
52
                'main' => $adapter,
53
            ]
54
        );
55
    }
56
57
    public function write($data): void
58
    {
59
        if ($this->debug) {
60
            $this->logger->info($this->getDecodedString($data));
61
        }
62
    }
63
64
    public function writeError($data): void
65
    {
66
        if ($this->debug) {
67
            $this->logger->error($this->getDecodedString($data));
68
        }
69
    }
70
71
    public function writeInfo($data): void
72
    {
73
        if ($this->debug) {
74
            $this->logger->info($this->getDecodedString($data));
75
        }
76
    }
77
78
    private function getDecodedString($data):string
79
    {
80
        $printedData = print_r($data, true);
81
        if(is_bool($printedData)){
82
            $result = '';
83
        }else{
84
            $result = urldecode($printedData);
85
        }
86
        return $result;
87
    }
88
89
90
}