]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger.php
Add Monolog
[friendica.git] / src / Core / Logger.php
1 <?php
2 /**
3  * @file src/Core/Logger.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\App;
8 use Friendica\BaseObject;
9 use Monolog;
10 use Friendica\Util\DateTimeFormat;
11
12 /**
13  * @brief Logger functions
14  */
15 class Logger extends BaseObject
16 {
17         /**
18          * Creates a basic Monolog instance for logging.
19          *
20          * @param string $application the current application name (index, daemon, ...)
21          *
22          * @return Monolog\Logger The Logger instance
23          */
24         public static function create($application)
25         {
26                 $logger = new Monolog\Logger($application);
27
28                 $logger->pushProcessor(new Monolog\Processor\IntrospectionProcessor());
29                 $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
30                 $logger->pushProcessor(new Monolog\Processor\WebProcessor());
31                 $logger->pushProcessor(new App\FriendicaLoggerProcessor());
32
33                 return $logger;
34         }
35
36         /**
37          * Sets the default Logging handler for this instance.
38          * Can be combined with other handlers too if necessary.
39          *
40          * @param Monolog\Logger $logger The Logger instance of this Application
41          * @param App            $app    The Friendica Application
42          */
43         public static function loadDefaultHandler($logger, $app)
44         {
45                 foreach ($logger->getProcessors() as $processor) {
46                         if ($processor instanceof App\FriendicaLoggerProcessor) {
47                                 $processor->setProcessId($app->process_id);
48                         }
49                 }
50
51                 $debugging = Config::get('system', 'debugging');
52                 $logfile   = Config::get('system', 'logfile');
53                 $loglevel = intval(Config::get('system', 'loglevel'));
54
55                 if (!$debugging || !$logfile) {
56                         return;
57                 }
58
59                 $fileHandler = new Monolog\Handler\StreamHandler($logfile . ".1", $loglevel);
60                 $logger->pushHandler($fileHandler);
61         }
62
63     // Log levels:
64         //EMERGENCY
65         //ALERT
66         //CRITICAL
67     const WARNING = 0; //ERROR
68     const INFO = 1; //WARNING
69     const TRACE = 2; //NOTICE(default)
70     const DEBUG = 3; //INFO
71     const DATA = 4; //INFO
72     const ALL = 5; //DEBUG
73
74     public static $levels = [
75         self::WARNING => 'Warning',
76         self::INFO => 'Info',
77         self::TRACE => 'Trace',
78         self::DEBUG => 'Debug',
79         self::DATA => 'Data',
80         self::ALL => 'All',
81     ];
82
83     /**
84      * @brief Logs the given message at the given log level
85      *
86      * @param string $msg
87      * @param int $level
88          *
89          * @deprecated since 2019.03 - use App->getLogger() instead
90      */
91     public static function log($msg, $level = self::INFO)
92     {
93         $a = self::getApp();
94
95         $debugging = Config::get('system', 'debugging');
96         $logfile   = Config::get('system', 'logfile');
97         $loglevel = intval(Config::get('system', 'loglevel'));
98
99         if (
100             !$debugging
101             || !$logfile
102             || $level > $loglevel
103         ) {
104             return;
105         }
106
107         $processId = session_id();
108
109         if ($processId == '')
110         {
111             $processId = $a->process_id;
112         }
113
114         $callers = debug_backtrace();
115
116         if (count($callers) > 1) {
117             $function = $callers[1]['function'];
118         } else {
119             $function = '';
120         }
121
122         $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
123                 DateTimeFormat::utcNow(DateTimeFormat::ATOM),
124                 $processId,
125                 self::$levels[$level],
126                 basename($callers[0]['file']),
127                 $callers[0]['line'],
128                 $function,
129                 $msg
130             );
131
132         $stamp1 = microtime(true);
133         @file_put_contents($logfile, $logline, FILE_APPEND);
134         $a->saveTimestamp($stamp1, "file");
135     }
136
137     /**
138      * @brief An alternative logger for development.
139      * Works largely as log() but allows developers
140      * to isolate particular elements they are targetting
141      * personally without background noise
142      *
143      * @param string $msg
144          *
145          * * @deprecated since 2019.03 - never used
146      */
147     public static function devLog($msg)
148     {
149         $a = self::getApp();
150
151         $logfile = Config::get('system', 'dlogfile');
152
153         if (!$logfile) {
154             return;
155         }
156
157         $dlogip = Config::get('system', 'dlogip');
158
159         if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip)
160         {
161             return;
162         }
163
164         $processId = session_id();
165
166         if ($processId == '')
167         {
168             $processId = $a->process_id;
169         }
170
171         if (!is_string($msg)) {
172                 $msg = var_export($msg, true);
173         }
174
175         $callers = debug_backtrace();
176         $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
177                 DateTimeFormat::utcNow(),
178                 $processId,
179                 basename($callers[0]['file']),
180                 $callers[0]['line'],
181                 $callers[1]['function'],
182                 $msg
183             );
184
185         $stamp1 = microtime(true);
186         @file_put_contents($logfile, $logline, FILE_APPEND);
187         $a->saveTimestamp($stamp1, "file");
188     }
189 }