简体   繁体   English

Laravel 5>使用monolog内省处理器

[英]Laravel 5 > Using monolog introspection processor

I have configured Laravel 5 to use a custom logging configuration (default is way too simple). 我已将Laravel 5配置为使用自定义日志记录配置(默认情况太简单了)。 I've added monolog's IntrospectionProcessor to log the file name and line number of the log call. 我添加了monolog的IntrospectionProcessor来记录日志调用的文件名和行号。

The problem is that all lines get the same file and line number: 问题是所有行都获得相同的文件和行号:

[2015-06-29 17:31:46] local.DEBUG (/home/vagrant/project/vendor/laravel/framework/src/Illuminate/Log/Writer.php#201): Loading view...  [192.168.10.1 - GET /loans/create]

Is there a way to config the IntrospectionProcessor to print the actual lines and not the facade ones? 有没有办法配置IntrospectionProcessor来打印实际的线而不是外观线?

If I do Log::getMonolog()->info('Hello'); 如果我做Log::getMonolog()->info('Hello'); it works and prints the correct file and line number... but I don't know how safe is to avoid calling the Writer.writeLog function because it fires a log event (is it safe to not fire that event?). 它工作并打印正确的文件和行号...但我不知道如何安全避免调用Writer.writeLog函数,因为它触发一个日志事件(是不是可以安全地触发该事件?)。

(Only tried in Laravel 4.2!) (仅在Laravel 4.2中尝试过!)

When pushing the Introspection Processor to Monolog it is possible to give an skipClassesPartial array as second parameter in the IntrospectionProcessor contructor. 将Introspection Processor推送到Monolog时,可以在IntrospectionProcessor构造函数中将skipClassesPartial数组作为第二个参数。 With this array it is possible to skip the Laravel Illuminate classes and the logger logs the class calling the log method. 使用此数组,可以跳过Laravel Illuminate类,并且记录器记录调用log方法的类。

$log->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, array('Illuminate\\')));

also see: https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/IntrospectionProcessor.php 另见: https//github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/IntrospectionProcessor.php

This is actually the expected functionality unless you're having the handler process the logs directly (check out the comments at the top of IntrospectionProcessor.php). 这实际上是预期的功能,除非您让处理程序直接处理日志(请查看IntrospectionProcessor.php顶部的注释)。 My guess is you have a wrapper function around the logger and you're calling it from Writer.php -- BUT 我的猜测是你有一个围绕记录器的包装函数,你从Writer.php调用它 - 但是

If you look at the code for IntrospectionProcessor.php you'll see a bit of code on lines 81 to 87 that decides how to format that stack trace, and it still has access to the stack. 如果你看一下IntrospectionProcessor.php的代码,你会看到第81到87行的一些代码决定了如何格式化堆栈跟踪,它仍然可以访问堆栈。 If you bump the $i values for $trace[$i - 1] / $trace[$i] up one (aka $trace[$i]/$trace[$i + 1] respectively) you can 'climb' the stack back to where you want. 如果你将$ trace [$ i - 1] / $ trace [$ i]的$ i值分别上升一个(也就是$ trace [$ i] / $ trace [$ i + 1]),你就可以'攀登'堆叠回你想要的地方。

It's important to note that the 'class' and 'function' parts of the trace need to be one level of the stack higher than the 'file' and 'line.' 值得注意的是,跟踪的“类”和“函数”部分需要比“文件”和“行”更高一层。

On a personal (plz dont mod me bruhs) note, I'd like to see functionality to include a stack offset when throwing the log in. I know what function I want to blame if an error shoots out when I write the error_log('ut oh') but I might(will) forget that by the time the 'ut oh' comes. 在个人(plz dont mod me bruhs)注意事项中,我希望在投入登录时看到包含堆栈偏移的功能。我知道当我写错误_log时,如果出现错误,我想要责备什么功能。噢哦')但是当'噢'来的时候我可能会遗忘。

I know this is an old question but I thought I'd give a quick update because it's pretty easy to get this done now. 我知道这是一个老问题,但我想我会快速更新,因为现在很容易完成这项工作。

I haven't tried with Laravel but My own logging mechanism is within a LoggingService wrapper class. 我没有尝试使用Laravel但我自己的日志记录机制是在LoggingService包装器类中。 As such the introspection was only giving details about the service rather than the caller. 因此,内省只提供有关服务的详细信息而不是来电者。

after reading Matt Topolski's answer, I had a look in the IntrospectionProcessor.php . 在阅读了Matt Topolski的回答之后,我看了一下IntrospectionProcessor.php the constructor looks like this: 构造函数看起来像这样:

__construct($level = Logger::DEBUG, array $skipClassesPartials = array(), $skipStackFramesCount = 0)

All I had to do was add the processor like this: 我所要做的就是像这样添加处理器:

log->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, array(), 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM