我的解决方案是对源代码的破解,应该避免。@Sven 的答案应该涵盖大多数情况,但对我来说,我的日志调用必须通过辅助方法。
在 LoggerLoggingEvent.php 类文件中添加方法:
/**
* Set the event location info
* @param LoggerLocationInfo $locationInfo
*/
public function setLocationInformation(LoggerLocationInfo $locationInfo) {
$this->locationInfo = $locationInfo;
}
然后在您的日志类方法中使用:
/**
* Log an INFO message
* @param string $msg The message to log
* @return none
*/
public function log($msg, $level='info'){
// Manually construct a logging event
$level = LoggerLevel::toLevel($level);
$logger = Logger::getLogger(__CLASS__);
$event = new LoggerLoggingEvent(__CLASS__, $logger, $level, $msg);
// Override the location info
$bt = debug_backtrace();
$caller = array_shift($bt);
$location = new LoggerLocationInfo($caller);
$event->setLocationInformation($location);
// Log it
$logger->logEvent($event);
}