<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/3/27
* Time: 14:15
*/
namespace IMooc;
class Loader
{
static function autoload($class){
require_once BASEDIR.'/'.str_replace('\\','/',$class).'.php';
}
}
2.配置文件类(Config.php)
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/3/27
* Time: 22:55
*/
namespace IMooc;
class Config implements \ArrayAccess
{
protected $config = array();
private $path;
public function __construct($path){
$this->path = $path;
}
public function offsetGet($key){
if(empty($this->config[$key])){
$file_path = $this->path.'/'.$key.'.php';
$this->config[$key] = require $file_path;
return $this->config[$key];
}
}
public function offsetExists($key){}
public function offsetSet($key, $value){
throw new \Exception('config not found!');
}
public function offsetUnset($key){}
}
3.自定义日志类(Log.php)=>以天为记录单位
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/3/31
* Time: 18:11
*/
namespace IMooc;
use \IMooc\Config;
class Log
{
private static $log_path ;
/*
* 构造log文件路径
* @param $log_name string log路径别称
* */
public function __construct($log_name){
self::setLog($log_name);
}
/*
* 设置log的路径
* @param $log_name string log路径别称
* */
private static function setLog($log_name){
//引入Logs配置文件
$config = new Config(BASEDIR.'/Configs');
if(empty($config['log'])){
$cofigs = $config['log'];
if(isset($cofigs[$log_name]))
self::$log_path = $cofigs[$log_name];
else
self::$log_path = BASEDIR."/Logs/".date("Y-m-d").'/';
}else{
throw new \Exception("Logs Dir is not exist !");
}
}
/*
* 将信息写入log文件中
* @param $message mixture log信息
* @param $log_name string log路径别称
* */
public static function log($message = "" ,$log_name = ""){
if(!empty($log_name))
self::setLog($log_name);
$date = date("Y-m-d");
$log_dir = self::$log_path;
if(!is_dir($log_dir))
mkdir($log_dir, 0777,true);
$log_file_name = $log_dir.$date.'.log';
if(!file_exists($log_file_name))
touch($log_file_name);
$fp = fopen($log_file_name, "a");
flock($fp, LOCK_EX);
if(is_array($message))
$message = self::arrayToString($message);
$message = sprintf("执行日期:[%s]:%s",date("Y-m-d H:i:s"),$message."\n");
fwrite($fp, $message);
fclose($fp);
}
/*
* 将数组转化为特定结构的字符串
* @param $arr array 待转化数组
* return $str string 转化后的字符串
* */
public static function arrayToString($arr){
$str = "";
if(!is_array($arr))
return $str;
foreach($arr as $key => $value){
if(is_array($value))
$str .= $key.":".self::arrayToString($value);
else
$str .= " ".$key.":".$value." ";
}
return "[".$str."]";
}
}
4.在根目录下创建Config文件夹,在这里建立一个log.php文件
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/3/31
* Time: 19:30
*/
$config = [
//不同的下标代表不同的日志
'log01' => BASEDIR."/Logs/log01/",
'log02' => BASEDIR."/Logs/log02/",
'Imooc' => BASEDIR."/Logs/Imooc/",
];
return $config;
?php>
5.在index.php文件中调用Log类
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2016/3/27
* Time: 13:43
*/
//定义项目的根文件路径=>根据自己的项目路径进行定义
define('BASEDIR',__DIR__);
//载入自动加载类
include BASEDIR.'/IMooc/Loader.php';
spl_autoload_register('\\IMooc\\Loader::autoload');
$date = date("Y-m-d H:i:s");
$message=['message'=>'OK','status'=>'200','info'=>['name'=>"DekingChne", "age" => 18]];
//将数组写入log中
\IMooc\Log::log($message,'Imooc');//通过第二个参数进行选择配置的日志路径
?php>
6.运行结果如下:
在根目录下建立了Logs/Imooc/2016-03-31.log日志文件:
2024 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123