- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我调用 logger
服务在日志文件中获取此信息消息它工作但将此消息写入日志文件中:
php.INFO: User Deprecated: The "logger" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: The \"logger\" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead. at /home/****/###/PROJECT/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:275)"} []
最佳答案
正如 Symfony 3.4 中所述,logger
MonologBundle
提供的服务和所有其他服务,默认设置为私有(private)。 [sic]
要解决此问题,推荐的方法是使用依赖注入(inject)。
http://symfony.com/doc/3.4/logging.html
namespace AppBundle\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction(LoggerInterface $logger)
{
$logger->info('Your Message');
}
}
源代码引用:
https://github.com/symfony/monolog-bundle/blob/v3.1.0/Resources/config/monolog.xml#L17
autowire
时可用已启用。
[sic]
#app/config/services.yml
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
#enables dependency injection in controller actions
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
#all of your custom services should be below this line
#which will override the above configurations
#optionally declare an individual service as public
#AppBundle\Service\MyService:
# public: true
#alternatively declare the namespace explicitly as public
#AppBundle\Service\:
# resource: '../../src/AppBundle/Service/*'
# public: true
然后要将依赖项注入(inject)服务,您将参数的类型提示添加到构造函数。
namespace AppBundle\Service;
use Psr\Log\LoggerInterface;
class MyService
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
如果
autowire
已禁用,您可以手动定义服务以注入(inject)记录器别名。
#app/config/services.yml
services:
AppBundle\Service\MyService:
arguments: ['@logger']
public: true
#app/config/services.yml
services:
#...
logger:
alias: 'monolog.logger'
public: true
// src/Kernel.php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;
public function process(ContainerBuilder $container)
{
// in this method you can manipulate the service container:
// for example, changing some container service:
$container->getDefinition('logger')->setPublic(true);
}
}
Symfony 捆绑包
// src/AppBundle/AppBundle.php
namespace AppBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use AppBundle\DependencyInjection\Compiler\CustomPass;
class AppBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new CustomPass());
}
}
// src/AppBundle/DependencyInjection/Compiler/CustomPass.php
namespace AppBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CustomPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->getDefinition('logger')->setPublic(true);
}
}
关于Symfony 3.4 记录器服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743626/
有没有办法配置 python 记录器来调用自定义函数并在它记录时将记录消息传递给它? 谢谢! 最佳答案 子类 logging.Handler 并实现 emit方法: import logging cl
我有一个这样的记录器设置: import logging from logging.handlers import RotatingFileHandler import sys # root logg
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我一直在使用 Netbeans 作为 Java IDE 进行开发。当 Netbeans 在 try-catch block 中包含一条语句时,它在捕获区域中使用类似 Logger.getLogger(
我正在记下远程日志记录的功能从头开始构建时可能需要库。我查了这个:http://www.aggsoft.com/serial-data-logger.htm 我想知道a之间有什么区别远程日志记录库和远
我需要跟踪包含数千个 JAR 和 .CLASS 文件的已编译 Java 应用程序,您知道有什么合适的工具可以附加到 JVM 来跟踪函数调用(无需源代码)吗? 最佳答案 是的。 Jprofiler无需源
我想使用记录器并设置其属性,但不想在每个对象类中创建记录器,我的目标是:在Spring中创建一个bean,创建属性文件我如何使用注释,可以做到吗? 最佳答案 是的,你可以做到, 在你的
首先,我已经阅读了real python article on the subject . 了解到记录器具有层次结构后,我想在这样的层次结构中创建一个名为 MyProjectLogger 的新记录器:
我设置了一个记录器。像这样: def initLogger(self): self.logger = logging.getLogger('MyApp') if not self.lo
是否允许为您的日志创建一个静态类? public final class Log { public static final Logger LOGGER = Logger.getLogger(
我对 java.util.logging 有一些无法解释的行为。让我们看一下这两个示例: 首先: boolean var = false; log.log( Level.WARNING, "Cant
我正在尝试开始在 python 中使用日志记录并阅读了几篇博客。一个让我感到困惑的问题是是按功能还是按模块创建记录器。在这个Blog: Good logging practice in Python建
我正在使用 https://pub.dartlang.org/packages/logging ,但它不会在我的日志中显示任何内容。这是我的代码: class Test { final Logge
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElem
我是 Poco 的新手,我在 Poco 在线帮助中看到了以下示例: int main(int argc, char** argv) { AutoPtr pChannel(new SimpleF
我有一个带有函数、文档字符串和文档测试的 Julia 模块文件。我加载它并在 Julia 帮助中显示文档字符串,但 Documenter.jl 找不到文档字符串。 一个示例模块文件,src/my_mo
我正在尝试在我的 Django 项目( django 1.11 、 Python 3.6 )中实现日志记录。我正在使用默认的 django 记录器。 获取 username在日志中,我使用了 djan
是否可以有多个 serilog 记录器?目前,在我的 WebApi 中,我可以调用 Log.Information 来记录信息事件,但是有没有一种方法可以改为制作不同的日志并从我的 Controlle
我有一个 python 应用程序,其文件结构类似于以下内容: /Project file1.py file2.py file3.py ... 该应用程序使用 Pytho
如何访问 python Bokeh 记录器? 我尝试使用 basicConfig包装器,但它似乎不起作用。 from bokeh.util import logconfig logconfig.bas
我是一名优秀的程序员,十分优秀!