3 * @file src/Core/Logger.php
5 namespace Friendica\Core;
8 use Friendica\BaseObject;
10 use Friendica\Util\DateTimeFormat;
13 * @brief Logger functions
15 class Logger extends BaseObject
18 * Creates a basic Monolog instance for logging.
20 * @param string $application the current application name (index, daemon, ...)
22 * @return Monolog\Logger The Logger instance
24 public static function create($application)
26 $logger = new Monolog\Logger($application);
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());
37 * Sets the default Logging handler for this instance.
38 * Can be combined with other handlers too if necessary.
40 * @param Monolog\Logger $logger The Logger instance of this Application
41 * @param App $app The Friendica Application
43 public static function loadDefaultHandler($logger, $app)
45 foreach ($logger->getProcessors() as $processor) {
46 if ($processor instanceof App\FriendicaLoggerProcessor) {
47 $processor->setProcessId($app->process_id);
51 $debugging = Config::get('system', 'debugging');
52 $logfile = Config::get('system', 'logfile');
53 $loglevel = intval(Config::get('system', 'loglevel'));
55 if (!$debugging || !$logfile) {
59 $fileHandler = new Monolog\Handler\StreamHandler($logfile . ".1", $loglevel);
60 $logger->pushHandler($fileHandler);
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
74 public static $levels = [
75 self::WARNING => 'Warning',
77 self::TRACE => 'Trace',
78 self::DEBUG => 'Debug',
84 * @brief Logs the given message at the given log level
89 * @deprecated since 2019.03 - use App->getLogger() instead
91 public static function log($msg, $level = self::INFO)
95 $debugging = Config::get('system', 'debugging');
96 $logfile = Config::get('system', 'logfile');
97 $loglevel = intval(Config::get('system', 'loglevel'));
102 || $level > $loglevel
107 $processId = session_id();
109 if ($processId == '')
111 $processId = $a->process_id;
114 $callers = debug_backtrace();
116 if (count($callers) > 1) {
117 $function = $callers[1]['function'];
122 $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
123 DateTimeFormat::utcNow(DateTimeFormat::ATOM),
125 self::$levels[$level],
126 basename($callers[0]['file']),
132 $stamp1 = microtime(true);
133 @file_put_contents($logfile, $logline, FILE_APPEND);
134 $a->saveTimestamp($stamp1, "file");
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
145 * * @deprecated since 2019.03 - never used
147 public static function devLog($msg)
151 $logfile = Config::get('system', 'dlogfile');
157 $dlogip = Config::get('system', 'dlogip');
159 if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip)
164 $processId = session_id();
166 if ($processId == '')
168 $processId = $a->process_id;
171 if (!is_string($msg)) {
172 $msg = var_export($msg, true);
175 $callers = debug_backtrace();
176 $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
177 DateTimeFormat::utcNow(),
179 basename($callers[0]['file']),
181 $callers[1]['function'],
185 $stamp1 = microtime(true);
186 @file_put_contents($logfile, $logline, FILE_APPEND);
187 $a->saveTimestamp($stamp1, "file");