码迷,mamicode.com
首页 > Web开发 > 详细

PHP打印日志类

时间:2019-09-26 23:27:23      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:filename   public   ica   cti   pre   vat   ===   func   create   

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");
    }
}

 

PHP打印日志类

标签:filename   public   ica   cti   pre   vat   ===   func   create   

原文地址:https://www.cnblogs.com/LanTianYou/p/11594996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!