laravel5日志设置篇(1) - 记录开始和结束日志
2017-10-19 23:27
330 查看
想要实现的功能
记录Application的开始和结束日志- 开始日志: URL请求或命令行记录
- 结束日志: 处理时间和内存使用峰值记录
例如:
[2017-10-19 23:06:00] local.INFO: -- Startup {"command":"php artisan list"} [2017-10-19 23:06:00] local.INFO: -- Shutdown {"time":"36.955[ms]","memory":"10[mb]"} [2017-10-19 23:11:37] local.INFO: -- Startup {"method":"GET","uri":"/v1/stocks"} [2017-10-19 23:11:37] local.INFO: -- Shutdown {"time":"21.765[ms]","memory":"4096[kb]"}
环境
php 7.0.22laravel 5.1.*
具体实现
日志采集类编写Kernel类的bootstrappers属性里添加日志采集类
日志采集类
app\Bootstrap\ApplicationLog.php
<?php namespace App\Bootstrap; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\Facades\Log; /** * 获取include时的时间 */ if (!defined('APP_START_TIME')) { define('APP_START_TIME', microtime(true)); } /** * 日志采集类 * * @package app.Bootstrap */ class ApplicationLog { /** * 警告阈值: 内存100M */ const THRESHOLD_WARNING_MEMORY = 100; /** * The application instance. * * @var \Illuminate\Contracts\Foundation\Application */ protected $app; /** * Bootstrap the given application. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ public function bootstrap(Application $app) { $this->app = $app; // 当是测试时,不采集日志 if (!$app->environment('testing')) { // 追加记录结束日志 register_shutdown_function([$this, 'handleShutdownLog']); // 开始日志 $this->startupLog(); } } /** * 开始日志 */ public function startupLog() { if ($this->app->runningInConsole()) { $command = sprintf('php %s', implode(' ', (array)array_get($GLOBALS, 'argv'))); $params = compact('command'); } else { $method = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; $params = compact('method', 'uri'); } app('log')->info('-- Startup', $params); } /** * 结束日志 * * @return void */ public function handleShutdownLog() { $time = $this->getProcessingTime(); $memory = $this->getPeekMemory(); if ($this->isOverUsingMemory(self::THRESHOLD_WARNING_MEMORY)) { app('log')->warning('using a large amount of memory.', compact('memory')); } app('log')->info('-- Shutdown', compact('time', 'memory')); } /** * 添加时间单位 * * @param float $time * @return string */ protected function formatTime($time) { if ($time < 1) { return sprintf('%0.3f[ms]', $time * 1000); } else if ($time < (1 / 1000)) { return sprintf('%0.3f[μs]', $time * 1000 * 1000); } return sprintf('%0.3f[s]', $time); } /* 4000 * * 添加内存单位 * * @param float $value * @return string */ protected function formatMemory($value) { if ($value < 1024 * 10) { return sprintf('%s', $value); } else if ($value < (1024 * 1024 * 10)) { return sprintf('%s[kb]', round($value / 1024, 1)); } else { return sprintf('%s[mb]', round($value / (1024 * 1024), 2)); } } /** * @param int $threshold 阈值(MB) * @return bool */ protected function isOverUsingMemory($threshold) { return round(memory_get_peak_usage(1) / 1024 / 1024) > $threshold; } /** * 获取处理时间 * * @return string */ protected function getProcessingTime() { return $this->formatTime(microtime(true) - APP_START_TIME); } /** * 获取内存使用峰值 * * @return float */ protected function getPeekMemory() { return $this->formatMemory(memory_get_peak_usage(true)); } }[b]Kernel类里追加日志采集模块Http Kernel
app\Http\Kernel.php
/** * 从父类的$bootstrappers复制 */ protected $bootstrappers = [ 'Illuminate\Foundation\Bootstrap\DetectEnvironment', 'Illuminate\Foundation\Bootstrap\LoadConfiguration', 'Illuminate\Foundation\Bootstrap\ConfigureLogging', 'Illuminate\Foundation\Bootstrap\HandleExceptions', 'Illuminate\Foundation\Bootstrap\RegisterFacades', 'Illuminate\Foundation\Bootstrap\RegisterProviders', 'Illuminate\Foundation\Bootstrap\BootProviders', //新增日志采集类 \App\Bootstrap\ApplicationLog::class, ];Console Kernel
app\Console\Kernel.php
/** * 从父类的$bootstrappers复制 */ protected $bootstrappers = [ 'Illuminate\Foundation\Bootstrap\DetectEnvironment', 'Illuminate\Foundation\Bootstrap\LoadConfiguration', 'Illuminate\Foundation\Bootstrap\ConfigureLogging', 'Illuminate\Foundation\Bootstrap\HandleExceptions', 'Illuminate\Foundation\Bootstrap\RegisterFacades', 'Illuminate\Foundation\Bootstrap\SetRequestForConsole', 'Illuminate\Foundation\Bootstrap\RegisterProviders', 'Illuminate\Foundation\Bootstrap\BootProviders', //新增日志采集类 \App\Bootstrap\ApplicationLog::class, ];以上就是所有配置,有兴趣的朋友可以试试
查看原文:https://www.huuinn.com/archives/330
更多技术干货:风匀坊
关注公众号:风匀坊
相关文章推荐
- laravel5日志设置篇(3/3) – 精确到微秒及日志输出位置记录
- 使用Jquery EasyUI中DateTimeBox控件设置考试开始结束时间,并对其验证
- 开始学习使用写日志的方式来记录写程序做过的修改
- K2 BlackPearl中日志记录的设置与扩展
- apache 访问日志如何设置每日新增一个日志文件、如何限制大小、设置记录内容
- VGScene 学习日志一:关于TvgScene设置为透明后,里面的按钮不能点击的问题记录
- AVAudioPlayer 的开始时间和结束时间设置
- Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能
- 如何管理和记录 SSIS 各个 Task 的开始执行时间和结束时间以及 Task 中添加|删除|修改的记录数
- 从壹开始前后端分离【 .NET Core2.0 Api + Vue 2.0 + AOP + 分布式】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
- 今天开始用日志记录我工作中遇见的故障和排除方法
- 从下周开始就要采用网上记录值班日志了
- layui.2.0在开始和结束时间动态设置问题
- MethodLogger 修改dll 记录方法的开始和结束(Hook into a method)
- nginx设置日志不记录404或者200或者其他日志信息
- Apache 日志记录相关设置
- Apache日志不记录图片文件设置方法和来源日志的配置
- Python:通过自定义系统级快捷键来控制程序开始或停止记录日志(使用小技巧解决一个貌似无解的问题)
- project设置工期为1个工作日,但开始时间与结束时间不是同一天,如何解决或者是设置?
- SqlServer 同表记录 上条记录结束日期为下条记录的开始日期的前一天