PHP打印日志类

PHP简单封装个打印日志类,方便查看日志:

<?php
/**
 * Created by PhpStorm.
 * User: zenkilan
 * Date: 2019/9/26
 * Time: 11:36
 */

class ZenkiLog
{
    private $rootDir;
    private $fileName;
    private $folder;
    private $dir;

    public function __construct($fileName, $folder)
    {
        $this->rootDir = "./zenkiLogs/";
        $this->fileName = $fileName;
        $this->folder = $folder;
        $this->dir = $this->rootDir . $this->folder;
        if (is_dir($this->dir) === FALSE) {
            mkdir($this->dir, 0777, true);
        }
    }

    public function zLog($msg)
    {
        $msg = "[" . date('Y-m-d H:i:s') . "]\t" . $msg . "\n";
        $logFile = $this->dir . '/' . $this->fileName . date('Y-m-d') . '.txt';
        if (file_exists($logFile)) {
            file_put_contents($logFile, $msg, FILE_APPEND);
        } else {
            $newLogFile = fopen($logFile, "w");
            fwrite($newLogFile, $msg);
            fclose($newLogFile);
        }
    }
}

在指定的路径下可以通过tail -f命令查看日志文件内容。

调用方法:

<?php
require BASEPATH . '../application/libraries/ZenkiLog.php';

/**
 * Created by PhpStorm.
 * User: zenkilan
 * Date: 2019/9/25
 * Time: 19:45
 */
class Test
{
    public function test()
    {
        $log = new ZenkiLog("zenki_wx_log", "");
        $log->zLog("test");
    }
}

 

posted @ 2019-09-26 22:47  天外归云  阅读(607)  评论(0编辑  收藏