开发过程中,查找程序问题最常用的方法是通过追踪日志文件。通过当前接触的项目是基于thinkphp5.0。
从入口开始入手,循序渐进:
1、 /public/index.php
2 、 /../thinkphp/start.php
从入口中寻找配置,/base.php 中包含 日志相关配置:
defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DS); defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH . 'log' . DS);
log 日志文件目录:
/runtime/log
通过项目配置文件:
config.php 找到 log 的配置:
日志 是 File 形式。路径是 上面配置中的 runtime/log
打开thinkphp/library/think/Log.php,这里面通过初始化,根据type来加载不同的模式
根据
$class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type);
找到路径,可以看出,type有 三种类型:FIle,Socket,Test。
官网文档里说,把type 改为 Test,就可以关闭日志。打开Test.php 可以看到:
根本都不存。
查看 File.php
文件路径和名字。
写日志内容。
在日志中是不是发现了该文件的一些信息。
上图中下面的message是 经过处理过的。处理方法在:
处理方法仅供参考。全文件内容如下:
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- namespace think; use think\exception\ClassNotFoundException; /** * Class Log * @package think * * @method void log($msg) static * @method void error($msg) static * @method void info($msg) static * @method void sql($msg) static * @method void notice($msg) static * @method void alert($msg) static */ class Log { const LOG = 'log'; const ERROR = 'error'; const INFO = 'info'; const SQL = 'sql'; const NOTICE = 'notice'; const ALERT = 'alert'; const DEBUG = 'debug'; // 日志信息 protected static $log = []; // 配置参数 protected static $config = []; // 日志类型 protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert', 'debug']; // 日志写入驱动 protected static $driver; // 当前日志授权key protected static $key; protected static $instance; const LOG_TYPE_NETLOG = true; //开始时间 protected $startTime; protected $clientIP; protected $logId; /** * 日志初始化 * @param array $config */ public static function init($config = []) { $type = isset($config['type']) ? $config['type'] : 'File'; $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type); self::$config = $config; unset($config['type']); if (class_exists($class)) { self::$driver = new $class($config); } else { throw new ClassNotFoundException('class not exists:' . $class, $class); } // 记录初始化信息 App::$debug && Log::record('[ LOG ] INIT ' . $type, 'info'); } /** * @return CLog */ public static function getInstance() { if (self::$instance === null) { $startTime = defined('PROCESS_START_TIME') ? PROCESS_START_TIME : microtime(true) * 1000; self::$instance = new Log($startTime); } return self::$instance; } /** * Constructor * * @param array $conf * @param uint $startTime */ private function __construct($startTime) { $this->startTime = $startTime; $this->logId = $this->__logId(); $this->clientIP = $this->getClientIP(); } /** * 获取日志信息 * @param string $type 信息类型 * @return array */ public static function getLog($type = '') { return $type ? self::$log[$type] : self::$log; } /** * 记录调试信息 * @param mixed $msg 调试信息 * @param string $type 信息类型 * @return void */ public static function record($msg, $type = 'log') { //格式化日志格式by yaojiaming $msg = Log::getInstance() -> getFormatMsgData($msg); self::$log[$type][] = $msg; if (IS_CLI) { // 命令行下面日志写入改进 self::save(); } } /** * 清空日志信息 * @return void */ public static function clear() { self::$log = []; } /** * 当前日志记录的授权key * @param string $key 授权key * @return void */ public static function key($key) { self::$key = $key; } /** * 检查日志写入权限 * @param array $config 当前日志配置参数 * @return bool */ public static function check($config) { if (self::$key && !empty($config['allow_key']) && !in_array(self::$key, $config['allow_key'])) { return false; } return true; } /** * 保存调试信息 * @return bool */ public static function save() { if (!empty(self::$log)) { if (is_null(self::$driver)) { self::init(Config::get('log')); } if (!self::check(self::$config)) { // 检测日志写入权限 return false; } if (empty(self::$config['level'])) { // 获取全部日志 $log = self::$log; if (!App::$debug && isset($log['debug'])) { unset($log['debug']); } } else { // 记录允许级别 $log = []; foreach (self::$config['level'] as $level) { if (isset(self::$log[$level])) { $log[$level] = self::$log[$level]; } } } $result = self::$driver->save($log); if ($result) { self::$log = []; } Hook::listen('log_write_done', $log); return $result; } return true; } /** * 实时写入日志信息 并支持行为 * @param mixed $msg 调试信息 * @param string $type 信息类型 * @param bool $force 是否强制写入 * @return bool */ public static function write($msg, $type = 'log', $force = false) { //格式化日志格式by yaojiaming $msg = Log::getInstance() -> getFormatMsgData($msg); $log = self::$log; // 封装日志信息 if (true === $force || empty(self::$config['level'])) { $log[$type][] = $msg; } elseif (in_array($type, self::$config['level'])) { $log[$type][] = $msg; } else { return false; } // 监听log_write Hook::listen('log_write', $log); if (is_null(self::$driver)) { self::init(Config::get('log')); } // 写入日志 $result = self::$driver->save($log); if ($result) { self::$log = []; } return $result; } /** * 静态调用 * @param $method * @param $args * @return mixed */ public static function __callStatic($method, $args) { if (in_array($method, self::$type)) { array_push($args, $method); return call_user_func_array('\\think\\Log::record', $args); } } private function __logId() { $arr = gettimeofday(); return ((($arr['sec']*100000 + $arr['usec']/10) & 0x7FFFFFFF)); } private function getFormatMsgData($msg) { $timeUsed = microtime(true)*1000 - $this->startTime; $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''; $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; $str = sprintf( "date[%s] host[%s] ip[%s] logId[%u] uri[%s] time_used[%d] %s", date('Y-m-d H:i:s:', time()), $host, $this->clientIP, $this->logId, $uri, $timeUsed, $msg); return $str; } //获取用户ip地址 public static function getClientIP() { $ip = '127.0.0.1'; if(isset($_SERVER['HTTP_CDN_FORWARDED_FOR'])){ $ip = $_SERVER['HTTP_CDN_FORWARDED_FOR']; }else{ if(isset($_SERVER['HTTP_CDN_SRC_IP'])) { $ip = $_SERVER['HTTP_CDN_SRC_IP']; } elseif (isset($_SERVER['HTTP_CLIENTIP'])) { $ip = $_SERVER['HTTP_CLIENTIP']; } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown',$arr); if(false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); } elseif (isset($_SERVER['HTTP_H_FORWARDED_FOR']) && !empty($_SERVER['HTTP_H_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_H_FORWARDED_FOR']); $pos = array_search('unknown',$arr); if(false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } } return $ip; } }