1<?php 2/** 3 * @author Bart Visscher <bartv@thisnet.nl> 4 * @author Morris Jobke <hey@morrisjobke.de> 5 * @author Roeland Jago Douma <rullzer@owncloud.com> 6 * @author Thomas Müller <thomas.mueller@tmit.eu> 7 * 8 * @copyright Copyright (c) 2018, ownCloud GmbH 9 * @license AGPL-3.0 10 * 11 * This code is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Affero General Public License, version 3, 13 * as published by the Free Software Foundation. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Affero General Public License for more details. 19 * 20 * You should have received a copy of the GNU Affero General Public License, version 3, 21 * along with this program. If not, see <http://www.gnu.org/licenses/> 22 * 23 */ 24 25namespace OC\Log; 26 27class Syslog { 28 protected static $levels = [ 29 \OCP\Util::DEBUG => LOG_DEBUG, 30 \OCP\Util::INFO => LOG_INFO, 31 \OCP\Util::WARN => LOG_WARNING, 32 \OCP\Util::ERROR => LOG_ERR, 33 \OCP\Util::FATAL => LOG_CRIT, 34 ]; 35 36 public static $DEFAULT_FORMAT = '[%reqId%][%remoteAddr%][%user%][%app%][%method%][%url%] %message%'; 37 38 /** 39 * Init class data 40 */ 41 public static function init() { 42 \openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER); 43 // Close at shutdown 44 \OC::$server->getShutdownHandler()->register(function () { 45 \closelog(); 46 }); 47 } 48 49 /** 50 * write a message in the log 51 * @param string $app 52 * @param string $message 53 * @param int $level 54 */ 55 public static function write($app, $message, $level) { 56 $syslogLevel = self::$levels[$level]; 57 58 $request = \OC::$server->getRequest(); 59 if (\OC::$server->getConfig()->getSystemValue('installed', false)) { 60 $user = (\OC_User::getUser()) ? \OC_User::getUser() : '--'; 61 } else { 62 $user = '--'; 63 } 64 65 $entry = [ 66 '%reqId%' => $request->getId(), 67 '%level%' => $level, // not needed in the default log line format, added by syslog itself 68 '%remoteAddr%' => $request->getRemoteAddress(), 69 '%user%' => $user, 70 '%app%' => $app, 71 '%method%' => \is_string($request->getMethod()) ? $request->getMethod() : '--', 72 '%url%' => ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--', 73 '%message%' => $message 74 ]; 75 76 $syslogFormat = \OC::$server->getConfig()->getSystemValue( 77 'log.syslog.format', 78 self::$DEFAULT_FORMAT 79 ); 80 81 $entryLine = \str_ireplace(\array_keys($entry), \array_values($entry), $syslogFormat); 82 \syslog($syslogLevel, $entryLine); 83 } 84} 85