Failed Conditions
Push — logging ( 0ecde6 )
by Andreas
05:33 queued 02:52
created

Logger   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 7 2
B log() 0 24 6
A writeLogLines() 0 6 1
1
<?php
2
3
namespace dokuwiki;
4
5
class Logger
6
{
7
    const LOG_ERROR = 'error';
8
    const LOG_DEPRECATED = 'deprecated';
9
    const LOG_DEBUG = 'debug';
10
11
    /** @var Logger[] */
12
    static protected $instances;
13
14
    /** @var string what kind of log is this */
15
    protected $facility;
16
17
    /**
18
     * Logger constructor.
19
     *
20
     * @param string $facility The type of log
21
     */
22
    protected function __construct($facility)
23
    {
24
        $this->facility = $facility;
25
    }
26
27
    /**
28
     * Return a Logger instance for the given facility
29
     *
30
     * @param string $facility The type of log
31
     * @return Logger
32
     */
33
    static public function getInstance($facility = self::LOG_ERROR)
34
    {
35
        if (self::$instances[$facility] === null) {
36
            self::$instances[$facility] = new Logger($facility);
37
        }
38
        return self::$instances[$facility];
39
    }
40
41
    /**
42
     * Log a message to the facility log
43
     *
44
     * @param string $message The log message
45
     * @param mixed $details Any details that should be added to the log entry
46
     * @param string $file A source filename if this is related to a source position
47
     * @param int $line A line number for the above file
48
     * @return bool
49
     */
50
    public function log($message, $details = null, $file = '', $line = 0)
51
    {
52
        // details are logged indented
53
        if ($details && !is_string($details)) {
54
            $details = json_encode($details, JSON_PRETTY_PRINT);
55
            $details = explode("\n", $details);
56
            $loglines = array_map(function ($line) {
57
                return '  ' . $line;
58
            }, $details);
59
        } elseif ($details) {
60
            $loglines = [$details];
61
        } else {
62
            $loglines = [];
63
        }
64
65
        $logline = gmdate('c') . "\t" . $message;
66
        if ($file) {
67
            $logline .= "\t$file";
68
            if ($line) $logline .= "($line)";
69
        }
70
71
        array_unshift($loglines, $logline);
72
        return $this->writeLogLines($loglines);
73
    }
74
75
    /**
76
     * Write the given lines to today's facility log
77
     *
78
     * @param string[] $lines the raw lines to append to the log
79
     * @return bool true if the log was written
80
     */
81
    protected function writeLogLines($lines)
82
    {
83
        global $conf;
84
        $logfile = $conf['logdir'] . '/' . $this->facility . '/' . gmdate('Y-m-d') . '.log';
85
        return io_saveFile($logfile, join("\n", $lines) . "\n", true);
86
    }
87
}
88