对于 laravel/lumen 而言,所有的日志文件信息(八种日志级别 emergency、alert、critical、error、warning、notice、info 和 debug )都是存在 /storage/logs/laravel.log 或 /storage/logs/lumen.log 中的.类似这样的存储方式

[2018-09-10 06:45:44] lumen.INFO: test {"ss":"dd"} 
[2018-09-10 06:47:32] lumen.EMERGENCY: testsss {"ss":"dd"} 

这样所有的问题都存储在一个文件中,对于寻找日志信息不是很方便,我们可以自定义 Monolog 配置让不同级别的日志存在不用的文件中.

配置方式,以 lumen 为例,laravel 类似.

lumen

复制以下代码到 /bootstrap/app.php 文件 中

// 需要引入的类文件
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
 $app->configureMonologUsing(function($monolog) {
        $debug = new StreamHandler(storage_path('logs/debug.log'), Logger::DEBUG,false);
        $debug->setFormatter(new LineFormatter(null, null, true, true));
        $notice =  new StreamHandler( storage_path("/logs/notice.log"), Monolog\Logger::NOTICE, false);
        $notice->setFormatter(new LineFormatter(null, null, true, true));
        $info =  new StreamHandler( storage_path("/logs/info.log"), Monolog\Logger::INFO, false);
        $info->setFormatter(new LineFormatter(null, null, true, true));
        $warning =  new StreamHandler( storage_path("/logs/warming.log"), Monolog\Logger::WARNING, false);
        $warning->setFormatter(new LineFormatter(null, null, true, true));
        $error =  new StreamHandler( storage_path("/logs/error.log"), Monolog\Logger::ERROR, false);
        $error->setFormatter(new LineFormatter(null, null, true, true));
        $critical =  new StreamHandler( storage_path("/logs/critical.log"), Monolog\Logger::CRITICAL, false);
        $critical->setFormatter(new LineFormatter(null, null, true, true));
        $alert =  new StreamHandler( storage_path("/logs/alert.log"), Monolog\Logger::ALERT, false);
        $alert->setFormatter(new LineFormatter(null, null, true, true));
        $emergency =  new StreamHandler( storage_path("/logs/emergency.log"), Monolog\Logger::EMERGENCY, false);
        $emergency->setFormatter(new LineFormatter(null, null, true, true));
        $monolog->pushHandler($debug);
        $monolog->pushHandler($notice);
        $monolog->pushHandler($info);
        $monolog->pushHandler($warning);
        $monolog->pushHandler($critical);
        $monolog->pushHandler($alert);
        $monolog->pushHandler($emergency);
        return $monolog;

 如此,当我们在调用类似

Log::emergency("testsss",["ss" => "dd"]); 

记录日志时,变会发现此时的 emergency 级别的日志时单独记录在 emergency.log 文件中了.

 在 Laravel5.5+ 中还可以将代码加入到 config/app.php 最后,也可以起到相同的作用.如下

use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; return [ |-------------------------------------------------------------------------- | Application Name |-------------------------------------------------------------------------- | This value is the name of your application. This value is used when the | framework needs to place the application's name in a notification or | any other location as required by the application or its packages. 'name' => env('APP_NAME', 'Laravel'), |-------------------------------------------------------------------------- | Application Environment |-------------------------------------------------------------------------- | This value determines the "environment" your application is currently | running in. This may determine how you prefer to configure various | services your application utilizes. Set this in your ".env" file. 'env' => env('APP_ENV', 'production'), |-------------------------------------------------------------------------- | Application Debug Mode |-------------------------------------------------------------------------- | When your application is in debug mode, detailed error messages with | stack traces will be shown on every error that occurs within your | application. If disabled, a simple generic error page is shown. 'debug' => env('APP_DEBUG', false), |-------------------------------------------------------------------------- | Application URL |-------------------------------------------------------------------------- | This URL is used by the console to properly generate URLs when using | the Artisan command line tool. You should set this to the root of | your application so that it is used when running Artisan tasks. 'url' => env('APP_URL', 'http://localhost'), |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. We have gone | ahead and set this to a sensible default for you out of the box. 'timezone' => 'UTC', |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | The application locale determines the default locale that will be used | by the translation service provider. You are free to set this value | to any of the locales which will be supported by the application. 'locale' => 'en', |-------------------------------------------------------------------------- | Application Fallback Locale |-------------------------------------------------------------------------- | The fallback locale determines the locale to use when the current one | is not available. You may change the value to correspond to any of | the language folders that are provided through your application. 'fallback_locale' => 'en', |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | This key is used by the Illuminate encrypter service and should be set | to a random, 32 character string, otherwise these encrypted strings | will not be safe. Please do this before deploying an application! 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC', |-------------------------------------------------------------------------- | Logging Configuration |-------------------------------------------------------------------------- | Here you may configure the log settings for your application. Out of | the box, Laravel uses the Monolog PHP logging library. This gives | you a variety of powerful log handlers / formatters to utilize. | Available Settings: "single", "daily", "syslog", "errorlog" 'log' => env('APP_LOG', 'single'), 'log_level' => env('APP_LOG_LEVEL', 'debug'), |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | The service providers listed here will be automatically loaded on the | request to your application. Feel free to add your own services to | this array to grant expanded functionality to your applications. 'providers' => [ * Laravel Framework Service Providers... Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Notifications\NotificationServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, * Package Service Providers... * Application Service Providers... App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, * self Providers Dingo\Api\Provider\LaravelServiceProvider::class, Maatwebsite\Excel\ExcelServiceProvider::class, |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. 'aliases' => [ 'App' => Illuminate\Support\Facades\App::class, 'Artisan' => Illuminate\Support\Facades\Artisan::class, 'Auth' => Illuminate\Support\Facades\Auth::class, 'Blade' => Illuminate\Support\Facades\Blade::class, 'Broadcast' => Illuminate\Support\Facades\Broadcast::class, 'Bus' => Illuminate\Support\Facades\Bus::class, 'Cache' => Illuminate\Support\Facades\Cache::class, 'Config' => Illuminate\Support\Facades\Config::class, 'Cookie' => Illuminate\Support\Facades\Cookie::class, 'Crypt' => Illuminate\Support\Facades\Crypt::class, 'DB' => Illuminate\Support\Facades\DB::class, 'Eloquent' => Illuminate\Database\Eloquent\Model::class, 'Event' => Illuminate\Support\Facades\Event::class, 'File' => Illuminate\Support\Facades\File::class, 'Gate' => Illuminate\Support\Facades\Gate::class, 'Hash' => Illuminate\Support\Facades\Hash::class, 'Lang' => Illuminate\Support\Facades\Lang::class, 'Log' => Illuminate\Support\Facades\Log::class, 'Mail' => Illuminate\Support\Facades\Mail::class, 'Notification' => Illuminate\Support\Facades\Notification::class, 'Password' => Illuminate\Support\Facades\Password::class, 'Queue' => Illuminate\Support\Facades\Queue::class, 'Redirect' => Illuminate\Support\Facades\Redirect::class, 'Redis' => Illuminate\Support\Facades\Redis::class, 'Request' => Illuminate\Support\Facades\Request::class, 'Response' => Illuminate\Support\Facades\Response::class, 'Route' => Illuminate\Support\Facades\Route::class, 'Schema' => Illuminate\Support\Facades\Schema::class, 'Session' => Illuminate\Support\Facades\Session::class, 'Storage' => Illuminate\Support\Facades\Storage::class, 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, 'Excel' => Maatwebsite\Excel\Facades\Excel::class, // 自定义 log 等级目录 $app->configureMonologUsing(function($monolog) { $debug = new StreamHandler(storage_path('logs/debug.log'), Logger::DEBUG,false); $debug->setFormatter(new LineFormatter(null, null, true, true)); $notice = new StreamHandler( storage_path("/logs/notice.log"), Monolog\Logger::NOTICE, false); $notice->setFormatter(new LineFormatter(null, null, true, true)); $info = new StreamHandler( storage_path("/logs/info.log"), Monolog\Logger::INFO, false); $info->setFormatter(new LineFormatter(null, null, true, true)); $warning = new StreamHandler( storage_path("/logs/warming.log"), Monolog\Logger::WARNING, false); $warning->setFormatter(new LineFormatter(null, null, true, true)); $error = new StreamHandler( storage_path("/logs/error.log"), Monolog\Logger::ERROR, false); $error->setFormatter(new LineFormatter(null, null, true, true)); $critical = new StreamHandler( storage_path("/logs/critical.log"), Monolog\Logger::CRITICAL, false); $critical->setFormatter(new LineFormatter(null, null, true, true)); $alert = new StreamHandler( storage_path("/logs/alert.log"), Monolog\Logger::ALERT, false); $alert->setFormatter(new LineFormatter(null, null, true, true)); $emergency = new StreamHandler( storage_path("/logs/emergency.log"), Monolog\Logger::EMERGENCY, false); $emergency->setFormatter(new LineFormatter(null, null, true, true)); $monolog->pushHandler($debug); $monolog->pushHandler($notice); $monolog->pushHandler($info); $monolog->pushHandler($warning); $monolog->pushHandler($critical); $monolog->pushHandler($alert); $monolog->pushHandler($emergency); return $monolog; 对于 laravel/lumen 而言,所有的日志文件信息(八种日志级别 emergency、alert、critical、error、warning、notice、info 和 debug )都是存在 /storage/logs/laravel.log 或 /storage/logs/lumen.log 中的.类似这样的存储方式[2018-09-10 06:45:44] lumen.INF...
Laravel 活动记录器 Laravel logger 是 LaravelLumen 应用程序的活动事件记录器。 它开箱即用,可与仪表板一起使用以查看您的活动。 Laravel 记录器可以作为中间件添加或通过特​​征调用。 轻松拥有活动日志。 这个包很容易配置和定制。 支持 Laravel 5.3、5.4、5.5、5.6、5.7、5.8、6 和 7+ 身份验证中间件使用 打开一个问题 Laravel 活动记录器功能 记录登录页面访问 记录用户登录 记录用户注销 路由事件可以使用中间件记录 记录活动时间戳 记录活动描述 记录活动详细信息(可选) 使用爬虫检测记录活动用户类型。 记录活动方法 记录活动路线 记录活动IP地址 记录活动用户代理 记录活动浏览器语言 记录活动引用 可定制的活动模型 活动面板仪表板 个人活动明细
对本地开发而言,你应该设置环境变量 APP_DEBUG 值为 true。在生产环境,该值应该被设置为 false。如果在生产环境被设置为 true,就有可能将一些敏感的配置值暴露给终端用户。
Laravel/Lumen日志简单系统介绍: Laravel/Lumen日志默认是基于Monolog进行了一层封装,如果要求不高,用起来还是十分容易的,本文基于laravel5.6/Lumen5.6版本进行解说。5.6版对日志系统做了升级,将日志的配置单独放以了config/logging.php 配置文件中,所以现在实用多了。 基本配置(解决日志路径文件名和保存周期等) 开始使用Lar...
本系列教程所有的PHPUnit测试基于PHPUnit6.5.9版本,Lumen 5.5框架 模块下的目录是符合Lumen的模块结构的如:Controllers、Models、Logics等是Lumen模块目录下的结构目录如果有自己的目录同级分配即可,如我这里的Requests ├── BaseCase.php 重写过L...
Laravel 提供可立即使用的 single、daily、syslog 和 errorlog 日志模式。例如,如果你想要每天保存一个日志文件,而不是单个文件,则可以在 config/app.php 配置文件内设置 log 变量: 'log' => 'dai 当你开始一个新的Lumen项目的时候,错误和异常功能,已经在框架中注入了。此外,Lumen还集成了Monolog日志函数,支持和提供多种强大的日志处理功能。 大量的错误信息在你的应用程序中是否显示,取决于你在.env文件中的APP——DEBUG参数配置。 大家在本地开发的时候,应该吧APP_DEBUG参数设置为true。在你线上环境中,应该设置为false。 Mo...
在本文中,我们将探讨Laravel Web框架最重要和讨论最少的功能之一-异常处理。 Laravel带有内置的异常处理程序,使您可以轻松友好地报告和呈现异常。 在本文的前半部分,我们将探讨异常处理程序提供的默认设置。 实际上,我们首先将遍历默认的Handler类,以了解Laravel如何处理异常。 在本文的后半部分,我们将继续学习如何创建一个自定义异常处理程序,以允许您捕获自定义异常。...
go get connectex: A connection attempt failed because the connected party did not properly respond 84291 postman Unable to load workspaces as you’re offline error. Can’t start a Workspaces JayeJOJO: 我用的方法是删除本地数据,重新安装 postman Unable to load workspaces as you’re offline error. Can’t start a Workspaces 浅歌一梦: 不能连互联网,加白名单有啥用? 分享内容到 Facebook/Twitter/Instagram/Reddit m0_73706315: 解决了吗,如何带图片 ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION 错误解决 qq_36348946: