【问题标题】:Create page that displays Codeigniter log files on webpage创建在网页上显示 Codeigniter 日志文件的页面
【发布时间】:2015-11-15 23:19:44
【问题描述】:

我有一个管理页面,我希望在我的 Codeigniter 的应用程序/日志文件夹中创建的所有日志消息都显示在该页面上。这样我就不必进入实际文件来查看它们,并且可以通过登录我的站点并单击“日志”页面来查看它们。

这可能吗?

【问题讨论】:

标签: php codeigniter error-handling


【解决方案1】:

这就是我的 error_log 并显示在视图上的方式

正如@Tpojka 建议的file_get_contents($file, FILE_USE_INCLUDE_PATH, null);

<?php

class Error_log extends CI_Controller {


    public function index() {   
        $data['title'] = 'Error Log';

        $data['clear'] = site_url('tool/error_log/clear');

        $data['log'] = '';

         // Current Filename;
        $file = FCPATH . 'application/logs/' . 'log-'.date('Y-m-d').'.php';

        if (file_exists($file)) {
            $size = filesize($file);

            if ($size >= 5242880) {
                $suffix = array(
                    'B',
                    'KB',
                    'MB',
                    'GB',
                    'TB',
                    'PB',
                    'EB',
                    'ZB',
                    'YB'
                );

                $i = 0;

                while (($size / 1024) > 1) {
                    $size = $size / 1024;
                    $i++;
                }

                $error_warning = 'Warning: Your error log file %s is %s!';

                $data['error_warning'] = sprintf($error_warning, basename($file), round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i]);
            } else {

                 // Updated from comment

                $log = file_get_contents($file, FILE_USE_INCLUDE_PATH, null); 
                $lines = explode("\n", $log); 
                $content = implode("\n", array_slice($lines, 1)); 
                $data['log'] = $content;
            }
        }

        $this->load->view('header', $data)
        $this->load->view('error_log', $data);
        $this->load->view('footer');
    }

}

错误日志查看

<div id="content">
    <div class="container-fluid">
        <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-exclamation-triangle"></i></h3>
        </div>
        <div class="panel-body">
            <textarea wrap="off" rows="15" readonly class="form-control"><?php echo $log; ?></textarea>
        </div>
        </div>
    </div>
</div>

我在 config.php 中有默认的日志路径,但你可以将它设置为你需要的。

$config['log_path'] = '';

$config['log_threshold'] = 2;

示例预览

【讨论】:

  • 太完美了!您是否删除了具有&lt;?php defined('BASEPATH') OR exit('No direct script access allowed'); ?&gt; 的第一行以使其不显示在屏幕上?
  • 我还没有完成删除 &lt;?php 的那部分工作,但我可以自己学习。
  • 我在 google 上看了一圈,找到了上面的代码,然后将其转换为 codeigniter。
  • 我发现了这个,它似乎有效,替换了你最后一个 else $log = file_get_contents($file, FILE_USE_INCLUDE_PATH, null); $lines = explode("\n", $log); $content = implode("\n", array_slice($lines, 1)); $this-&gt;data['log'] = $content; 中的所有内容
猜你喜欢
  • 2016-06-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多