/** * test using the path setting to write logs in other places. * * @return void */ function testPathSetting() { $path = TMP . 'tests' . DS; @unlink($path . 'error.log'); $log = new FileLog(compact('path')); $log->write('warning', 'Test warning'); $this->assertTrue(file_exists($path . 'error.log')); unlink($path . 'error.log'); }
/** * 处理异常信息 * @param \Exception $ex */ public static function exception($ex) { static $logger = null; if (is_null($logger)) { if (!class_exists('FileLog')) { Cache::import('FileLog.php'); } $logger = new \FileLog('exception'); } $logger->error('Message:' . $ex->getMessage() . "\nTrace:" . $ex->getTraceAsString() . PHP_EOL); }
protected function validation() { // process the submitted form $this->form->processInput($this->input->post); // check if honeypot is checked $spam_field = $this->form->get("sendemail"); $spam_action = $this->sanitizer->text($this->input->post->sendemail); // if it is checked, add an error to the error array if ($spam_action == 1) { $spam_field->error("If you are human, you'd best email us directly; your submission is being detected as spam! If you are a robot, please ignore this."); // write this attempt to a log $spam_log = new FileLog($this->config->paths->logs . 'detectedspam.txt'); $spam_log->save('Spam caught: ' . $this->sanitizer->textarea($this->input->post->body)); } }
/** * Implements writing to log files. * * @param string $type The type of log you are making. * @param string $message The message you want to log. * @return boolean success of write. */ public function write($type, $message) { $trace = debug_backtrace(); if (isset($trace[2]) && isset($trace[2]['file']) && isset($trace[2]['line'])) { parent::write($type, $trace[2]['file'] . ' Line: ' . $trace[2]['line']); } parent::write($type, $message); }
function run_server($time = 1, $log_file = null) { $filelog = new FileLog($log_file); while (true) { $event = $this->_queue->get(); if ($event and !isset($event['HTTPSQS_GET_END'])) { if (!isset($this->_handles[$event[0]])) { $filelog->info('SwooleEvent Error: empty event!'); } $func = $this->_handles[$event[0]]; if (!function_exists($func)) { $filelog->info('SwooleEvent Error: event handle function not exists!'); } else { $parmas = array_slice($event, 1); call_user_func_array($func, $parmas); $filelog->info('SwooleEvent Info: process success!event type ' . $func . ',params(' . implode(',', $parmas) . ')'); } } else { usleep($time * 1000); //echo 'sleep',NL; } } }
/** * Return an array of entries that exist in the given range of dates * * @param string $name Name of log * @param int|string $dateFrom Unix timestamp or string date/time to start from * @param int|string $dateTo Unix timestamp or string date/time to end at (default = now) * @return array * */ public function getDate($name, $dateFrom, $dateTo = 0) { $log = new FileLog($this->getFilename($name)); $log->setDelimeter("\t"); return $log->getDate($dateFrom, $dateTo); }
function ProcessWireShutdown() { $types = array(E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parse Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Strict Warning', E_RECOVERABLE_ERROR => 'Recoverable Fatal Error'); $fatalTypes = array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_PARSE, E_RECOVERABLE_ERROR); $error = error_get_last(); if (!$error) { return true; } $type = $error['type']; if (!in_array($type, $fatalTypes)) { return true; } $http = isset($_SERVER['HTTP_HOST']); $config = wire('config'); $user = wire('user'); $userName = $user ? $user->name : '?'; $page = wire('page'); $path = ($config ? $config->httpHost : '') . ($page ? $page->url : '/?/'); if ($config && $http) { $path = ($config->https ? 'https://' : 'http://') . $path; } $line = $error['line']; $file = $error['file']; $message = isset($types[$type]) ? $types[$type] : 'Error'; if (strpos($error['message'], "\t") !== false) { $error['message'] = str_replace("\t", ' ', $error['message']); } $message .= ": \t{$error['message']}"; if ($type != E_USER_ERROR) { $message .= " (line {$line} of {$file}) "; } $debug = false; $log = null; $why = ''; $who = ''; $sendOutput = true; if ($config) { $debug = $config->debug; $sendOutput = $config->allowExceptions !== true; if ($config->ajax) { $http = false; } if ($config->adminEmail && $sendOutput) { $logMessage = "Page: {$path}\nUser: {$userName}\n\n" . str_replace("\t", "\n", $message); wireMail($config->adminEmail, $config->adminEmail, 'ProcessWire Error Notification', $logMessage); } if ($config->paths->logs) { $logMessage = "{$userName}\t{$path}\t" . str_replace("\n", " ", $message); $log = new FileLog($config->paths->logs . 'errors.txt'); $log->setDelimeter("\t"); $log->save($logMessage); } } if (!$sendOutput) { return true; } // we populate $who to give an ambiguous indication where the full error message has been sent if ($log) { $who .= "Error has been logged. "; } if ($config && $config->adminEmail) { $who .= "Administrator has been notified. "; } // we populate $why if we're going to show error details for any of the following reasons: // otherwise $why will NOT be populated with anything if ($debug) { $why = "site is in debug mode (\$config->debug = true; in /site/config.php)."; } else { if (!$http) { $why = "you are using the command line API."; } else { if ($user && $user->isSuperuser()) { $why = "you are logged in as a Superuser."; } else { if ($config && is_file($config->paths->root . "install.php")) { $why = "/install.php still exists."; } else { if ($config && !is_file($config->paths->assets . "active.php")) { // no login has ever occurred or user hasn't logged in since upgrade before this check was in place // check the date the site was installed to ensure we're not dealing with an upgrade $installed = $config->paths->assets . "installed.php"; if (!is_file($installed) || filemtime($installed) > time() - 21600) { // site was installed within the last 6 hours, safe to assume it's a new install $why = "Superuser has never logged in."; } } } } } } if ($why) { // when in debug mode, we can assume the message was already shown, so we just say why. // when not in debug mode, we display the full error message since error_reporting and display_errors are off. $why = "This error message was shown because {$why} {$who}"; $html = "<p><b>{message}</b><br /><small>{why}</small></p>"; if ($http) { if ($config && $config->fatalErrorHTML) { $html = $config->fatalErrorHTML; } $html = str_replace(array('{message}', '{why}'), array(nl2br(htmlspecialchars($message, ENT_QUOTES, "UTF-8", false)), htmlspecialchars($why, ENT_QUOTES, "UTF-8", false)), $html); // make a prettier looking debug backtrace, when applicable $html = preg_replace('!(<br[^>]*>\\s*)(#\\d+\\s+[^<]+)!is', '$1<code>$2</code>', $html); echo "\n\n{$html}\n\n"; } else { echo "\n\n{$message}\n\n{$why}\n\n"; } } else { // public fatal error that doesn't reveal anything specific if ($http) { header("HTTP/1.1 500 Internal Server Error"); } // file that error message will be output in, when available $file = $config && $http ? $config->paths->templates . 'errors/500.html' : ''; if ($file && is_file($file)) { // use defined /site/templates/errors/500.html file echo str_replace('{message}', $who, file_get_contents($file)); } else { // use generic error message, since no 500.html available echo "\n\nUnable to complete this request due to an error. {$who}\n\n"; } } return true; }
public function testMaskSetting() { if (DS === '\\') { $this->markTestSkipped('File permission testing does not work on Windows.'); } $path = TMP . 'tests' . DS; $this->_deleteLogs($path); $log = new FileLog(array('path' => $path, 'mask' => 0666)); $log->write('warning', 'Test warning one'); $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4); $expected = '0666'; $this->assertEquals($expected, $result); unlink($path . 'error.log'); $log = new FileLog(array('path' => $path, 'mask' => 0644)); $log->write('warning', 'Test warning two'); $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4); $expected = '0644'; $this->assertEquals($expected, $result); unlink($path . 'error.log'); $log = new FileLog(array('path' => $path, 'mask' => 0640)); $log->write('warning', 'Test warning three'); $result = substr(sprintf('%o', fileperms($path . 'error.log')), -4); $expected = '0640'; $this->assertEquals($expected, $result); unlink($path . 'error.log'); }
/** * Returns instance of FileLog for given log name * * @param $name * @param array $options * @return FileLog * */ public function getFileLog($name, array $options = array()) { $log = new FileLog($this->getFilename($name)); if (isset($options['delimiter'])) { $log->setDelimeter($options['delimiter']); } else { $log->setDelimeter("\t"); } return $log; }
<?php $vowel = array('a' => 'ᅡ', 'eo' => 'ᅥ', 'o' => 'ᅩ', 'u' => 'ᅮ', 'eu' => 'ᅳ', 'i' => 'ᅵ', 'ae' => 'ᅢ', 'e' => 'ᅦ', 'oe' => 'ᅬ', 'wi' => 'ᅱ', 'ya' => 'ㅑ', 'yeo' => 'ㅕ', 'yo' => 'ㅛ', 'yu' => 'ㅠ', 'yae' => 'ㅒ', 'ye' => 'ᅨ', 'wa' => 'ᅪ', 'wae' => 'ᅫ', 'wo' => 'ᅯ', 'we' => 'ᅰ', 'ui' => 'ᅴ', 'y' => 'ㅣ'); $consonant = array('g' => 'ㄱ', 'k' => 'ㄱ', 'kk' => 'ㄲ', 'k' => 'ㅋ', 'd' => 'ㄷ', 't' => 'ㄷ', 'tt' => 'ㄸ', 't' => 'ㅌ', 'b' => 'ㅂ', 'pp' => 'ㅃ', 'p' => 'ㅍ', 'j' => 'ㅈ', 'jj' => 'ㅉ', 'ch' => 'ㅊ', 's' => 'ㅅ', 'ss' => 'ㅆ', 'h' => 'ㅎ', 'n' => 'ㄴ', 'm' => 'ㅁ', 'ng' => 'ㅇ', 'r' => 'ㄹ', 'l' => 'ㄹ', 'f' => 'ㅍ', 'q' => 'ㅋ', 'x' => 'ㅋ', 'z' => 'ㅈ'); require_once 'lib/FileLog.class.php'; $f = new FileLog('data/names.tsv', array('time', 'name', 'korean')); if ($_POST['name']) { $f->save(array(time(), $_POST['name'], strlen($_POST['korean']) > 0 ? $_POST['korean'] : $_POST['mozilla'])); header('Location: ./'); exit; } $d = $f->load(); $d = array_reverse($d); ?> <!DOCTYPE html> <html lang="en-US"> <head> <meta name="viewport" content="width=device-width" /> <meta charset="UTF-8"> <title>Let's type my name in Korean.</title> <link rel="stylesheet" href="style.css"> </head> <body> <h1>My Name to Korean</h1> <p>Let's try to type my name in Korean with Firefox OS Korean keyboard.</p> <h2>Conversion table</h2> <h3>vowel</h3> <ul class="table"> <?php ksort($vowel);
/** * test log rotation * * @return void */ public function testRotation() { $path = TMP . 'tests' . DS; $this->_deleteLogs($path); file_put_contents($path . 'error.log', "this text is under 35 bytes\n"); $log = new FileLog(array('path' => $path, 'size' => 35, 'rotate' => 2)); $log->write('warning', 'Test warning one'); $this->assertTrue(file_exists($path . 'error.log')); $result = file_get_contents($path . 'error.log'); $this->assertRegExp('/Warning: Test warning one/', $result); $this->assertEquals(0, count(glob($path . 'error.log.*'))); clearstatcache(); $log->write('warning', 'Test warning second'); $files = glob($path . 'error.log.*'); $this->assertEquals(1, count($files)); $result = file_get_contents($files[0]); $this->assertRegExp('/this text is under 35 bytes/', $result); $this->assertRegExp('/Warning: Test warning one/', $result); sleep(1); clearstatcache(); $log->write('warning', 'Test warning third'); $result = file_get_contents($path . 'error.log'); $this->assertRegExp('/Warning: Test warning third/', $result); $files = glob($path . 'error.log.*'); $this->assertEquals(2, count($files)); $result = file_get_contents($files[0]); $this->assertRegExp('/this text is under 35 bytes/', $result); $result = file_get_contents($files[1]); $this->assertRegExp('/Warning: Test warning second/', $result); sleep(1); clearstatcache(); $log->write('warning', 'Test warning fourth'); // rotate count reached so file count should not increase $files = glob($path . 'error.log.*'); $this->assertEquals(2, count($files)); $result = file_get_contents($path . 'error.log'); $this->assertRegExp('/Warning: Test warning fourth/', $result); $result = file_get_contents(array_pop($files)); $this->assertRegExp('/Warning: Test warning third/', $result); $result = file_get_contents(array_pop($files)); $this->assertRegExp('/Warning: Test warning second/', $result); file_put_contents($path . 'debug.log', "this text is just greater than 35 bytes\n"); $log = new FileLog(array('path' => $path, 'size' => 35, 'rotate' => 0)); $log->write('debug', 'Test debug'); $this->assertTrue(file_exists($path . 'debug.log')); $result = file_get_contents($path . 'debug.log'); $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Debug: Test debug/', $result); $this->assertFalse(strstr($result, 'greater than 5 bytes')); $this->assertEquals(0, count(glob($path . 'debug.log.*'))); }
//DNSPod 国内版帐号 define('CN_USER', ''); //DNSPod 国内版密码 define('CN_PASSWORD', ''); //DNSPod 国内版 D令牌 验证码, 如果没有开启 D令牌,留空即可 define('CN_DTOKEN', ''); //DNSPod 国际版帐号 define('COM_USER', ''); //DNSPod 国际版密码 define('COM_PASSWORD', ''); /*------------- END 请配置帐号、密码等信息 END -----------------*/ define('COM_API_URL', 'https://www.dnspod.com/api/'); define('CN_API_URL', 'https://dnsapi.cn/'); define('LOG_FILE', 'export_domain.log'); set_time_limit(0); $FileLog = new FileLog(LOG_FILE); try { $DNSPodCN = new DNSPodCN(CN_USER, CN_PASSWORD, CN_DTOKEN); $DNSPodCom = new DNSPodCOM(COM_USER, COM_PASSWORD); $domains = $DNSPodCN->getDomains(); } catch (Exception $e) { die('Initialization Exception: ' . $e->getMessage() . "\n"); } foreach ($domains as $domain) { $FileLog->newLine("Adding domain: {$domain};"); try { $ret = $DNSPodCom->addDomain($domain); $ret = true === $ret ? $FileLog->append(' Result: success') : $FileLog->append(" Result: error ({$ret})"); } catch (Exception $e) { $FileLog->append(' Exception: ' . $e->getMessage()); }
/** * 将需要的文件复制到压缩目录下 * * @param object $info * @param String $dir * @return Boolean */ public function zipProcessFile($info, $dir) { //import("@.Browse.Object"); import("@.File.FileLog"); //$info = Object::isUserAuthorized($objid,"view"); if (!$info || $info['object_type'] != 2) { return false; } $objid = $info['id']; $userid = $info['userid']; $fileName = $info['name']; $version = $info['version']; $filehDao = new File_historyDao(); $vo = $filehDao->find("objectid={$objid} and version={$version}", null, "id,md5sum"); if (!$vo) { return false; } $filePath = Tools::getStoragePath(DATA_DIR, $vo->id); if (md5_file($filePath) != $vo->md5sum) { FileLog::logEvent('OBJ_CHECKSUM_VERIFY_FAIL', $objid, null, $userid); return false; } if (Session::is_set('CLAMAV_SUPPORT')) { $r = Tools::clamAvScan(stripslashes($filePath)); if ($r === FALSE) { FileLog::logEvent('OBJ_VIRUS_ERROR', $objid, null, $userid); } elseif ($r == "clean") { FileLog::logEvent('OBJ_VIRUS_PASS', $objid, null, $userid); } else { FileLog::logEvent('OBJ_VIRUS_FAIL', $objid, null, $userid); return false; } } $fileName = Tools::convertCharset($fileName); $destFileName = "{$dir}/{$fileName}"; @copy($filePath, $destFileName); FileLog::logEvent('OBJ_VIEWED', $objid, null, $userid); return true; }
/** * Look for errors at shutdown and log them, plus echo the error if the page is editable * */ function ProcessWireShutdown() { $types = array(E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parse Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'ProcessWire Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Strict Warning', E_RECOVERABLE_ERROR => 'Recoverable Fatal Error'); $fatalTypes = array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR); $error = error_get_last(); $type = $error['type']; if (in_array($type, $fatalTypes)) { $config = wire('config'); $user = wire('user'); $userName = $user ? $user->name : 'Unknown User'; $page = wire('page'); $path = $page ? $page->url : '/?/'; $line = $error['line']; $file = $error['file']; $message = "{$error['message']}"; $debug = false; if ($type != E_USER_ERROR) { $message .= " (line {$line} of {$file})"; } if ($config) { $debug = $config->debug; $logMessage = "{$userName}:{$path}:{$types[$type]}:{$message}"; if ($config->adminEmail) { @mail($config->adminEmail, 'ProcessWire Error Notification', $logMessage); } $logMessage = str_replace("\n", " ", $logMessage); if ($config->paths->logs) { $log = new FileLog($config->paths->logs . "errors.txt"); $log->save($logMessage); } } if ($debug || !isset($_SERVER['HTTP_HOST']) || $user && $user->isSuperuser()) { // when in debug mode, we can assume the message was already shown, so we just say why. // when not in debug mode, we display the full error message since error_reporting and display_errors are off. if ($debug) { $message = "This error message was shown because the site is in DEBUG mode."; } else { $message .= "\n\nThis error message was shown because you are logged in as a Superuser."; } if (isset($_SERVER['HTTP_HOST'])) { $message = "<p class='WireFatalError'>" . nl2br($message) . "</p>"; } echo "\n\n{$message}\n\n"; } else { header("HTTP/1.1 500 Internal Server Error"); echo "Unable to complete this request due to an error"; } } }
/** * Look for errors at shutdown and log them, plus echo the error if the page is editable * */ function ProcessWireShutdown() { $types = array(E_ERROR => 'Error', E_WARNING => 'Warning', E_PARSE => 'Parse Error', E_NOTICE => 'Notice', E_CORE_ERROR => 'Core Error', E_CORE_WARNING => 'Core Warning', E_COMPILE_ERROR => 'Compile Error', E_COMPILE_WARNING => 'Compile Warning', E_USER_ERROR => 'Error', E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Strict Warning', E_RECOVERABLE_ERROR => 'Recoverable Fatal Error'); $fatalTypes = array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_PARSE, E_RECOVERABLE_ERROR); $error = error_get_last(); if (!$error) { return; } $type = $error['type']; if (!in_array($type, $fatalTypes)) { return; } $http = isset($_SERVER['HTTP_HOST']); $config = wire('config'); $user = wire('user'); $userName = $user ? $user->name : '?'; $page = wire('page'); $path = ($config ? $config->httpHost : '') . ($page ? $page->url : '/?/'); if ($config && $http) { $path = ($config->https ? 'https://' : 'http://') . $path; } $line = $error['line']; $file = $error['file']; $message = isset($types[$type]) ? $types[$type] : 'Error'; if (strpos($error['message'], "\t") !== false) { $error['message'] = str_replace("\t", ' ', $error['message']); } $message .= "\t{$error['message']}"; if ($type != E_USER_ERROR) { $message .= " (line {$line} of {$file}) "; } $debug = false; $log = null; $why = ''; $who = ''; if ($config) { $debug = $config->debug; if ($config->ajax) { $http = false; } if ($config->adminEmail) { $logMessage = "Page: {$path}\nUser: {$userName}\n\n" . str_replace("\t", "\n", $message); @mail($config->adminEmail, 'ProcessWire Error Notification', $logMessage); } if ($config->paths->logs) { $logMessage = "{$userName}\t{$path}\t" . str_replace("\n", " ", $message); $log = new FileLog($config->paths->logs . 'errors.txt'); $log->setDelimeter("\t"); $log->save($logMessage); } } // we populate $who to give an ambiguous indication where the full error message has been sent if ($log) { $who .= "Error has been logged. "; } if ($config && $config->adminEmail) { $who .= "Administrator has been notified. "; } // we populate $why if we're going to show error details for any of the following reasons: if ($debug) { $why = "site is in debug mode (\$config->debug = true; in /site/config.php)."; } else { if (!$http) { $why = "you are using the command line API."; } else { if ($user && $user->isSuperuser()) { $why = "you are logged in as a Superuser."; } else { if ($config && is_file($config->paths->root . "install.php")) { $why = "/install.php still exists."; } else { if ($config && !is_file($config->paths->assets . "active.php")) { // no login has ever occurred or user hasn't logged in since upgrade before this check was in place // check the date the site was installed to ensure we're not dealing with an upgrade $installed = $config->paths->assets . "installed.php"; if (!is_file($installed) || filemtime($installed) > time() - 21600) { // site was installed within the last 6 hours, safe to assume it's a new install $why = "Superuser has never logged in."; } } } } } } if ($why) { // when in debug mode, we can assume the message was already shown, so we just say why. // when not in debug mode, we display the full error message since error_reporting and display_errors are off. $why = "This error message was shown because {$why} {$who}"; if ($http) { $why = "<em>{$why}</em>"; } if ($debug) { $message = $why; } else { $message .= "\n\n{$why}"; } if ($http) { $message = "<p class='error WireFatalError'>" . nl2br($message) . "</p>"; } echo "\n\n{$message}\n\n"; } else { header("HTTP/1.1 500 Internal Server Error"); echo "Unable to complete this request due to an error. {$who}"; } return true; }
foreach ($car1 as $p => $v) { echo "Свойство {$p} = {$v} <br>"; } echo Car::CNAME, "<br>"; abstract class Log { abstract function write($txt); } class FileLog extends Log { function write($txt) { file_put_contents("log.txt", $txt); } } $fileLog = new FileLog(); $fileLog->write('q'); interface Numerable { function showList(); } class Books implements Numerable { public $bList = []; function __construct($l) { $this->bList = $l; } function showList() { foreach ($this->bList as $v) {