TOP ▲ itcore TOPTIPStool_log.php  タグ:開発 tool ログ log

tool エラーlog表示 | itcore 2021年

<?php
//-----------------------------------------------------
// ログ確認 log.php
//-----------------------------------------------------
include_once "../.env.php"; // 環境依存インクルード
include_once "inc_common.php"; // 共通インクルード
list($datetime1, $datetime2, $self, $menu) = uInit(); // 初期設定
$title = "ログ確認";

$style = uStyle(); // CSS
$yyyy = date("Y");
$mm = date("m");
$mm3 = date("M");
$dd = date("d");
$line_num = uForm("line_num", 3);
$today1 = "$yyyy-$mm-$dd"; // 2021-04-29
$today2 = "$dd-$mm3-$yyyy"; // 29-Apr-2021
$today3 = "$mm3 $dd"; // Apr 29
$today4 = "$dd/$mm3/$yyyy"; // 29/Apr/2021
//echo "debug24 today1=$today1 today2=$today2 today3=$today3 today4=$today4<br>\n";
//--------------------
// ヘッダ
//--------------------
echo <<<EOT
<html><head>
  <link rel="icon" href="./img/arrow-down-up.svg" type="image/svg+xml" />
  <title>$title</title>
</head>
<body style="font-family: monospace, monospace; word-break: break-all;">
$style
<style>
  textarea {
    width: 100%;
  }
</style>
<h1 id=top>$title $datetime1</h1>
$menu
<hr>
<form action="$self" method="post">
<a href=#apache>apache</a>
<a href=#laravel>laravel</a>
<a href=#phpv>PHPバージョン</a>
<br>
表示ログ行数 <input type="text" name="line_num" value="$line_num" size="3">
<input type="submit" value="再表示">
EOT;
//--------------------
// PHP apache Laravel ログ
//--------------------
$html = "";
$html .= "<h2>PHPエラー</h2>";
$file = "/var/log/php-fpm/www-error.log";
$html .= u_cmd_file($file, $yyyy, $line_num);

$html .= "<h2 id=apache>apache 共通 <a href=#top>top</a></h2>";
$file = "/var/log/httpd/error_log";
$html .= u_cmd_file($file, $yyyy, $line_num);

$html .= "<h2>apache vhost</h2>";
$dir = "/etc/httpd/logs";
$a_fname = uOpendirArrayGet($dir);
foreach ($a_fname as $fname) {
  if ("log" != substr($fname, -3)) {
    // error_log-20210307 ローテーションされた過去ログは対象外
    continue;
  }
  //$html .= "debug47 fname=$fname<br>\n";
  $file = "$dir/$fname";
  // アクセスログの場合は404を表示する。
  if (false != strpos($fname, "access")) {
    $cmd = "grep 404 $file | tail -$line_num | tac";
    $html .= u_cmd($cmd);
    continue;
  }
  // エラーログ
  $html .= u_cmd_file($file, $yyyy, $line_num);
}

$html .= "<h2 id=laravel>Laravel <a href=#top>top</a></h2>";
$dir = "/var/prj";
$a_fname = uOpendirArrayGet($dir);
//$a_fname = [];
//$a_fname[] = "xxx";
foreach ($a_fname as $fname) {
  $dir1 = "$dir/$fname/storage";
  if (!is_dir($dir1)) {
    // storageディレクトリがなければLaravelがない
    continue;
  }
  //$html .= "debug64 fname=$fname<br>\n";
  $file = "$dir1/logs/laravel.log";
  $html .= u_cmd_file($file, $yyyy, $line_num);
  //$cmd = "(cd /$dir/$fname; php artisan route:list) 2>&1";
  //$html .= u_cmd($cmd);
}

$html .= "<h2 id=phpv>PHPバージョン <a href=#top>top</a></h2>";
$cmd = "php -v 2>&1";
$html .= u_cmd($cmd);
echo <<<EOT
  $html
  </form></body></html>
EOT;

//-------------------
// 個別関数
//-------------------
function u_cmd($cmd)
{
  global $today1, $today2, $today3, $today4;
  $a1 = [$today1, $today2, $today3, $today4];
  $html = "";
  $output = [];
  exec($cmd, $output, $status);
  //$html .= "<textarea rows=5>\n";
  foreach ($output as $line) {
    // 今日のログか調べる
    $is_today = false;
    foreach ($a1 as $s1) {
      if (false !== strpos($line, $s1)) {
        $is_today = true;
        break;
      }
    }
    $s1 = htmlspecialchars($line);
    $s1 = str_replace(" ", "&nbsp;", $s1);
    if ($is_today) {
      $s1 = "<font color=red>$s1</font>";
    }
    $html .= "$s1<br>\n";
  }
  //$html .= "</textarea><br>\n";
  // 結果が空ならひょうじさせない。
  if ("" == $html) {
    return $html;
  }
  $html = "<h3>$cmd</h3>\n$html";
  return $html;
}
function u_cmd_file($file, $yyyy, $line_num)
{
  $html = "";
  $cmd = "grep $yyyy $file | tail -$line_num | tac";
  $html .= u_cmd($cmd);
  //$cmd = "tail -100 $file | tac";
  //$html .= u_cmd($cmd);
  return $html;
}

?>