PHP打印日志类

Posted 目前在腾讯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘) . "]	" . $msg . "
";
        $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");
    }
}

 

以上是关于PHP打印日志类的主要内容,如果未能解决你的问题,请参考以下文章

PHP打印日志类

怎么让php打印错误日志?

PHP代码-psysh调试代码片段工具

JAVA之AOP

webstorm代码片段的创建

java怎么运用切面编程生成日志