PHP如何让这个类更好?建议/反馈欢迎 [英] PHP how could I make this class better? Suggestions/Feedback welcome

查看:132
本文介绍了PHP如何让这个类更好?建议/反馈欢迎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最佳建议是接受的答案

类:

<?php 

class LogInfo {
    private $first_run;         // Flag to add line break at the beginning of script execution
    private $calling_script;    // Base name of the calling script
    private $log_file;          // log file path and name
    private $log_entry;         // information to be logged
    private $log_level;         // Log severity levels: error, warning, notice, info (default)
    private $log_level_arr;     // Array of accepted log levels
    private $fh;                // File handle

    private $file_name;         // File path and name
    private $file_parts;        // Array of $file_name
    private $script_name;       // Script Name
    private $script_parts;      // Array of $script_name    

    function __construct() {
        $this->first_run        = true;
        $this->log_level_arr    = array(
                                    'info'=>'info', // default
                                    'error'=>'error',
                                    'warning'=>'warning',
                                    'notice'=>'notice',
                                    );
        $this->calling_script   = '';       
        $this->log_file         = '';
        $this->log_entry        = '';
        $this->log_level        = '';
        $this->fh               = '';
        $this->file_name        = '';
        $this->file_parts       = '';
        $this->script_name      = '';
        $this->script_parts     = '';                   
    }

    /**
     * log detailed information into calling script log files
     */
    public function logInfo($info, $level = 'info') {

        if(array_key_exists($level,$this->log_level_arr)) {
            $this->log_level = $level;
        } else {
            $this->log_level = 'undefined';
        }

        $this->calling_script = $this->getScriptBaseName();
        $this->log_file = dirname(__FILE__)."/logs/".$this->calling_script.".log";
        $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file);

        if($this->first_run) {
            $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][" .$this->log_level."]:\t".$info."\n";
        } else {
            $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][" .$this->log_level."]:\t".$info."\n";
        }

        fwrite($this->fh, $this->log_entry);
        fclose($this->fh);

        $this->first_run = false;
    }

    /**
     * return the base name of the calling script
     */
    private function getScriptBaseName() {
        $this->file_name    = $_SERVER["SCRIPT_NAME"];
        $this->file_parts   = explode('/', $this->file_name);
        $this->script_name  = $this->file_parts[count($this->file_parts) - 1];
        $this->script_parts = explode('.', $this->script_name);

        return $this->script_parts[0];
    }
}

?>

如何调用:

$log = new LogInfo();

$txt = "This is comment";
$log->logInfo($txt." 1",'error');
$log->logInfo($txt." 2",'new');
$log->logInfo($txt." 3",'warning');
$log->logInfo($txt." 4",'notice');
$log->logInfo($txt." 5",'info');
$log->logInfo($txt." 6");


推荐答案

我将对每个日志级别使用单独的方法,将更容易使用。

I would use separate methods for each log level, that would be easier to use.

$ log->错误($ msg)例如。

$log->error($msg) for example.

这篇关于PHP如何让这个类更好?建议/反馈欢迎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆