Home php教程 PHP源码 Laravel 集成的 Monolog 库对日志进行配置和记录实例

Laravel 集成的 Monolog 库对日志进行配置和记录实例

Jun 08, 2016 pm 05:19 PM
error log monolog nbsp quot

本文章为各位介绍一篇关于Laravel 集成的 Monolog 库对日志进行配置和记录的实例,具体的如下希望对各位吸帮助。

<script>ec(2);</script>

对于大型系统而言,日志是不可或缺的模块,Laravel自然也对日志提供了完善的支持。目前Laravel集成了强大的Monolog库进行日志记录。下面我们就来大致看一下Laravel中如何对日志进行配置以及日志的简单使用。

1、日志处理器配置

首先我们来看日志处理器的配置。Laravel目前支持四种日志处理器:

single —— 将日志记录到单个文件中。该日志处理器对应Monolog的StreamHandler。
daily —— 以日期为单位将日志进行归档,每天创建一个新的日志文件记录日志。该日志处理器 对应Monolog的RotatingFileHandler。
syslog —— 将日志记录到syslog中。该日志处理器 对应Monolog的SyslogHandler。
errorlog —— 将日志记录到PHP的error_log中。该日志处理器 对应Monolog的ErrorLogHandler。

项目实际日志处理器通过config/app.php中的log配置项决定,默认配置值为single。这里我们使用默认值,不做修改:

'log' => 'single',

当然如果这四种方式满足不了你的需求,还可以使用configureMonologUsing方法完全控制Monolog的日志处理器:

$app->configureMonologUsing(function($monolog) {
    $monolog->pushHandler(...);
});

注:必须将上述这段代码置于bootstrap/app.php文件返回$app之前处才能生效。
2、使用Log记录日志
配置完成后,就可以在代码中使用Log门面来记录日志,Log门面背后实际上是Illuminate\Log\Writer,而在Writer的构造函数中注入了Monolog\Logger。生成的日志文件存放在storage/logs目录下。

目前,Log门面支持八种日志级别(使用RFC 5424标准):

Log::emergency($error);     //紧急状况,比如系统挂掉
Log::alert($error);     //需要立即采取行动的问题,比如整站宕掉,数据库异常等,这种状况应该通过短信提醒
Log::critical($error);     //严重问题,比如:应用组件无效,意料之外的异常
Log::error($error);     //运行时错误,不需要立即处理但需要被记录和监控
Log::warning($error);    //警告但不是错误,比如使用了被废弃的API
Log::notice($error);     //普通但值得注意的事件
Log::info($error);     //感兴趣的事件,比如登录、退出
Log::debug($error);     //详细的调试信息

下面我们就来分别演示下这几种日志级别的日志记录,我们将在TestController的log方法中进行测试:

public function log(){
    Log::emergency("系统挂掉了");
    Log::alert("数据库访问异常");
    Log::critical("系统出现未知错误");
    Log::error("指定变量不存在");
    Log::warning("该方法已经被废弃");
    Log::notice("用户在异地登录");
    Log::info("用户xxx登录成功");
    Log::debug("调试信息");
}
在浏览器中访问http://laravel.app:8000/test/log,对应在storage/logs/laravel.log记录的日志信息如下:

[2015-11-09 14:24:05] local.EMERGENCY: 系统挂掉了
[2015-11-09 14:24:05] local.ALERT: 数据库访问异常
[2015-11-09 14:24:05] local.CRITICAL: 系统出现未知错误
[2015-11-09 14:24:05] local.ERROR: 指定变量不存在
[2015-11-09 14:24:05] local.WARNING: 该方法已经被废弃
[2015-11-09 14:24:05] local.NOTICE: 用户在异地登录
[2015-11-09 14:24:05] local.INFO: 用户xxx登录成功
[2015-11-09 14:24:05] local.DEBUG: 调试信息

可见对应的日志记录包含了日志记录时间、日志级别和日志消息等信息。当然我们也可以在记录日志时传递上下文信息:

Log::info("用户xxx登录成功",['user_id'=>1]);
对应的日志记录为:

[2015-11-09 14:25:47] local.INFO: 用户xxx登录成功 {"user_id":1}

如果要访问底层Monolog的实例可以使用如下方法:

$monolog = Log::getMonolog();
dd($monolog);

我们可以在浏览器页面中查看当前Monolog对象实例的属性信息:

Laravel Monolog 对象实例

我们可以从中看出当前使用的日志处理器,日志记录的位置,日志记录的格式等信息。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles