下图为文件结构:
核心为件为ShinowLog.php,以下为该文件代码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* ShinowCMS日志系统
* 采用驱动配置的调用模式,日志写入模式包括文件、数据库、远程写入三种
* @author LIUTAO 2016/1/19
*/
class ShinowLog extends CI_Driver_Library {
/**
* 验证驱动类型属性
*/
protected $valid_drivers = array('file', 'db', 'remote');
/**
* 默认适配器名称
*/
protected $_adapter = 'file';
/**
* 日志前缀索引,用于区别不同站点日志
*/
public $key_prefix = DOMAIN;
/**
* 存储配置文件中内容
* @var type ARRAY
*/
protected $cfg = array();
/**
* 日志构造器
* 根据配置文件初始化成员变量
*/
public function __construct($config = array()) {
$this->cfg = &get_shinowlog();
if (isset($config['adapter']) && in_array($config['adapter'], $this->valid_drivers)) {
$this->_adapter = $config['adapter'];
} else {
if (isset($this->cfg['shinowlog_adapter']) && in_array($this->cfg['shinowlog_adapter'], $this->valid_drivers)) {
$this->_adapter = $this->cfg['shinowlog_adapter'];
} else {
throw new Exception('非法的适配器!');
}
}
}
/**
*
* @param type $level
* @param type $msg
* @param type $runtime
* @return boolean
* @comment 写入日志统一入口
*/
public function write_log($level, $msg, $runtime = null) {
if($this->_is_needed($level)){
if($this->_adapter!="file"){
$return_msg = $this->{$this->_adapter}->write_log($level, $msg, $runtime);
if($return_msg!="ok"){
$this->_adapter = 'file';
$this->{$this->_adapter}->write_log($level, $msg, $runtime);
}
return true;
}else{
$return_msg = $this->{$this->_adapter}->write_log($level, $msg, $runtime);
}
} else {
return false;
}
}
/**
* 根据配置文件判断是否需要写入日志
*/
private function _is_needed($level){
// print_r($this->cfg);
$level = strtoupper($level);
if(isset($this->cfg['shinowlog_threshold']) && isset($this->cfg['levels'])){
//echo "aaa";
if(is_array($this->cfg['shinowlog_threshold'])){
$diff = array_diff($this->cfg['shinowlog_threshold'],$this->cfg['levels']);
if(empty($diff)){
if(in_array($this->cfg['levels'][$level],$this->cfg['levels'])&& in_array($this->cfg['levels'][$level],$this->cfg['shinowlog_threshold'])){
return true;
}else {
return false;
}
}else {
return false;
}
}else {
// var_dump($level == $this->cfg['levels'][$level]);
if($this->cfg['shinowlog_threshold']==$this->cfg['levels']['ALL']){
return true;
}else if(in_array($this->cfg['shinowlog_threshold'],$this->cfg['levels'])){
return true;
}else {
return false;
}
}
}else {
return false;
}
}
}
这是适配器drivers文件
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* *
* ShinowLog文件日志适配器
*/
class CI_ShinowLog_file extends CI_Driver {
/**
* 保存日志的路径
*
* @var string
*/
protected $_shinowlog_path;
/**
* 日志文件权限
*
* @var int
*/
protected $_file_permissions = 0644;
/**
* 日志级别
*
* @var int
*/
protected $_threshold = 5;
/**
* 日志级别数组
*
* @var array
*/
protected $_threshold_array = array();
/**
* 文件日志保存的时间格式
*
* @var string
*/
protected $_date_fmt = 'Y-m-d H:i:s';
/**
* 文件方扩展名
*
* @var string
*/
protected $_file_ext;
/**
* 是否确定写入日志,主要依赖于所配置的日志级别
*
* @var bool
*/
protected $_enabled = TRUE;
/**
* 日志级别信息
*
* @var array
*/
protected $_levels = null;
/**
* 服务器编号
* 注:如果值为null,则写入日志时,值不可见
*/
protected $_serverID = 'NULL';
// --------------------------------------------------------------------
/**
*类的初始化
*
* @return void
*/
public function __construct()
{
$config =& get_shinowlog();
$this->_levels = $config['levels'];
$this->_shinowlog_path = ($config['shinowlog_path'] !== '') ? $config['shinowlog_path'] : APPPATH.'logs/';
$this->_serverID = isset($config['server_id'])?$config['server_id']:'NULL';
$this->_file_ext = (isset($config['shinowlog_file_extension']) && $config['shinowlog_file_extension'] !== '')
? ltrim($config['shinowlog_file_extension'], '.') : 'php';
file_exists($this->_shinowlog_path) OR mkdir($this->_shinowlog_path, 0755, TRUE);
if ( ! is_dir($this->_shinowlog_path) OR ! is_really_writable($this->_shinowlog_path))
{
$this->_enabled = FALSE;
}
if (is_numeric($config['shinowlog_threshold']))
{
$this->_threshold = (int) $config['shinowlog_threshold'];
}
elseif (is_array($config['shinowlog_threshold']))
{
$this->_threshold = 0;
$this->_threshold_array = array_flip($config['shinowlog_threshold']);
}
if ( ! empty($config['shinowlog_date_format']))
{
$this->_date_fmt = $config['shinowlog_date_format'];
}
if ( ! empty($config['shinowlog_file_permissions']) && is_int($config['shinowlog_file_permissions']))
{
$this->_file_permissions = $config['shinowlog_file_permissions'];
}
}
// --------------------------------------------------------------------
/**
* 日志写入
*
* 一般情况下日志会在全局函数shinowlog_message()中调用
*
* @return bool
*/
public function write_log($level, $msg,$runtime=null)
{
if ($this->_enabled === FALSE)
{
return FALSE;
}
$level = strtoupper($level);
if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
&& ! isset($this->_threshold_array[$this->_levels[$level]]))
{
return FALSE;
}
$filepath = $this->_shinowlog_path.'sys-'.date('Y-m-d').'.'.$this->_file_ext;
$message = '';
if ( ! file_exists($filepath))
{
$newfile = TRUE;
// Only add protection to php files
if ($this->_file_ext === 'php')
{
$message .= "<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>\n\n";
}
}
if ( ! $fp = @fopen($filepath, 'ab'))
{
return FALSE;
}
// Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
if (strpos($this->_date_fmt, 'u') !== FALSE)
{
$microtime_full = microtime(TRUE);
$microtime_short = sprintf("%06d", ($microtime_full - floor($microtime_full)) * 1000000);
$date = new DateTime(date('Y-m-d H:i:s.'.$microtime_short, $microtime_full));
$date = $date->format($this->_date_fmt);
}
else
{
$date = date($this->_date_fmt);
}
$runtime = $runtime==null?'NULL':$runtime;
$message .= DOMAIN.'|'.$this->_serverID.'|'.$level.' | '.APPURI.'|'.REFERER.'|'.$runtime.'|'.$date.' --> '.$msg."\n";
flock($fp, LOCK_EX);
for ($written = 0, $length = strlen($message); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($message, $written))) === FALSE)
{
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
if (isset($newfile) && $newfile === TRUE)
{
chmod($filepath, $this->_file_permissions);
}
return is_int($result);
}
}