TaskLogger   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A log() 0 11 3
1
<?php
2
/**
3
 * Teampass - a collaborative passwords manager.
4
 * ---
5
 * This file is part of the TeamPass project.
6
 * 
7
 * TeamPass is free software: you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, version 3 of the License.
10
 * 
11
 * TeamPass 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
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 * 
19
 * Certain components of this file may be under different licenses. For
20
 * details, see the `licenses` directory or individual file headers.
21
 * ---
22
 * @file      taskLogger.php
23
 * @author    Nils Laumaillé ([email protected])
24
 * @copyright 2009-2025 Teampass.net
25
 * @license   GPL-3.0
26
 * @see       https://www.teampass.net
27
 */
28
29
/**
30
 * Class TaskLogger
31
 * Handles logging for background tasks in TeamPass
32
 */
33
class TaskLogger {
34
    private $settings;
35
    private $logFile;
36
37
    public function __construct(array $settings, string $logFile = '') {
38
        $this->settings = $settings;
39
        $this->logFile = $logFile;
40
    }
41
42
    /**
43
     * Logs a message to the specified log file or default error log
44
     *
45
     * @param string $message The message to log
46
     * @param string $level The log level (e.g., INFO, ERROR, DEBUG)
47
     */
48
    public function log(string $message, string $level = 'INFO') {
49
        if (!empty($this->settings['enable_tasks_log'])) {
50
            $formattedMessage = date($this->settings['date_format'] . ' ' . $this->settings['time_format']) . 
51
                                " - [$level] $message" . PHP_EOL;
52
53
            if (!empty($this->logFile)) {
54
                // WWrite to the specified log file
55
                file_put_contents(__DIR__.'/'.$this->logFile, $formattedMessage, FILE_APPEND | LOCK_EX);
56
            } else {
57
                // Use default error log
58
                error_log($formattedMessage);
59
            }
60
        }
61
    }
62
}