]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
06134386700fe8893806fefaac1f9647d964d513
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @file src/Core/Logger.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Core\Config;
8 use Friendica\Util\DataTimeFormat;
9
10 class Logger
11 {
12     /**
13      * @brief Logs the given message at the given log level
14      *
15      * log levels:
16      * LOGGER_WARNING
17      * LOGGER_INFO (default)
18      * LOGGER_TRACE
19      * LOGGER_DEBUG
20      * LOGGER_DATA
21      * LOGGER_ALL
22      *
23      * @global array $LOGGER_LEVELS
24      * @param string $msg
25      * @param int $level
26      */
27     public static function logger($msg, $level = LOGGER_INFO)
28     {
29         $a = get_app();
30         global $LOGGER_LEVELS;
31         $LOGGER_LEVELS = [];
32
33         $debugging = Config::get('system', 'debugging');
34         $logfile   = Config::get('system', 'logfile');
35         $loglevel = intval(Config::get('system', 'loglevel'));
36
37         if (
38             !$debugging
39             || !$logfile
40             || $level > $loglevel
41         ) {
42             return;
43         }
44
45         if (count($LOGGER_LEVELS) == 0) {
46             foreach (get_defined_constants() as $k => $v) {
47                 if (substr($k, 0, 7) == "LOGGER_") {
48                     $LOGGER_LEVELS[$v] = substr($k, 7, 7);
49                 }
50             }
51         }
52
53         $process_id = session_id();
54
55         if ($process_id == '') {
56             $process_id = get_app()->process_id;
57         }
58
59         $callers = debug_backtrace();
60
61         if (count($callers) > 1) {
62             $function = $callers[1]['function'];
63         } else {
64             $function = '';
65         }
66
67         $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
68                 DateTimeFormat::utcNow(DateTimeFormat::ATOM),
69                 $process_id,
70                 $LOGGER_LEVELS[$level],
71                 basename($callers[0]['file']),
72                 $callers[0]['line'],
73                 $function,
74                 $msg
75             );
76
77         $stamp1 = microtime(true);
78         @file_put_contents($logfile, $logline, FILE_APPEND);
79         $a->saveTimestamp($stamp1, "file");
80     }
81
82     /**
83      * @brief An alternative logger for development.
84      * Works largely as logger() but allows developers
85      * to isolate particular elements they are targetting
86      * personally without background noise
87      *
88      * log levels:
89      * LOGGER_WARNING
90      * LOGGER_INFO (default)
91      * LOGGER_TRACE
92      * LOGGER_DEBUG
93      * LOGGER_DATA
94      * LOGGER_ALL
95      *
96      * @global array $LOGGER_LEVELS
97      * @param string $msg
98      * @param int $level
99      */
100     public static function dlogger($msg, $level = LOGGER_INFO)
101     {
102         $a = get_app();
103
104         $logfile = Config::get('system', 'dlogfile');
105         if (!$logfile) {
106             return;
107         }
108
109         $dlogip = Config::get('system', 'dlogip');
110         if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip) {
111             return;
112         }
113
114         if (count($LOGGER_LEVELS) == 0) {
115             foreach (get_defined_constants() as $k => $v) {
116                 if (substr($k, 0, 7) == "LOGGER_") {
117                     $LOGGER_LEVELS[$v] = substr($k, 7, 7);
118                 }
119             }
120         }
121
122         $process_id = session_id();
123
124         if ($process_id == '') {
125             $process_id = $a->process_id;
126         }
127
128         $callers = debug_backtrace();
129         $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
130                 DateTimeFormat::utcNow(),
131                 $process_id,
132                 basename($callers[0]['file']),
133                 $callers[0]['line'],
134                 $callers[1]['function'],
135                 $msg
136             );
137
138         $stamp1 = microtime(true);
139         @file_put_contents($logfile, $logline, FILE_APPEND);
140         $a->saveTimestamp($stamp1, "file");
141     }
142 }