0

I'm failing miserable on something that I think should be possible. I have 2 Symfony applications that have a set of bundles both use. One of these is a log bundle with some interfaces and messagehandlers. I use symfony/monolog on both applications and the bundle.

Since both applications use the same monolog configuration, I would like for the bundle to be the one creating it.

I tried for example, creating a monolog.yaml file on my bundle and loading is on the extension:

<?php

namespace Vendor\LogsBundle\DependencyInjection;

use Exception;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class LogsExtension extends Extension
{
    /**
     * @param array            $configs
     * @param ContainerBuilder $container
     *
     * @throws Exception
     */
    public function load(array $configs, ContainerBuilder $container): void
    {
        // Load the bundle's service declarations
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/../../config')
        );

        $loader->load('services.yaml');
        $loader->load('monolog.yaml');
    }
}

But when I do this, I get this error message:

There is no extension able to load the configuration for "monolog" (in "/path_to_bundle/bundles/logs/src/DependencyInjection/../../config/packages/monolog.yaml"). Looked for namespace "monolog", found "none".

Edit: adding monolog config

monolog:
  channels:
    - deprecation
    - channel_a
    - integration
    - channel_b
    - slack_error
    - syslog_server
    - php_stderr
  handlers:
    # grouping the two main posters
    grouped_messages:
      type: group
      members: [main_messages_filter, slack_error, syslog_server]

    slack_error:
      type: slackwebhook
      webhook_url: '%env(SLACK_MONOLOG_WEBHOOK)%'
      channel: '#%env(SLACK_MONOLOG_CHANNEL)%'
      bot_name: '%env(SLACK_BOT_NAME)%'
      level: error
      include_extra: true
      formatter: monolog.formatter.json
      channels: [ 'slack_error' ]

    main_messages_filter:
      type: filter
      handler: php_stderr
      min_level: info

    console:
      type: console
      process_psr_3_messages: false
      channels: ["!event", "!doctrine", "!console"]

    deprecation:
      type: stream
      channels: [deprecation]
      path: php://stderr

# changes for production related to the channel_a and channel_b
when@prod:
  monolog:
    handlers:
    channel_a:
      level: info
      handler: grouped_messages
      channels: [ "channel_a" ]
      bubble: false

    channel_b:
      level: info
      handler: grouped_messages
      channels: [ "channel_b" ]
      bubble: false

    # configure the stderr
    php_stderr:
      path: php://stderr
      level: info
      formatter: monolog.formatter.json

    syslog_server:
      type: syslogudp
      host: myhostname
when@dev:
  monolog:
    handlers:
      channel_a:
        type: stream
        path: "%kernel.logs_dir%/channel_a.log"
        level: info
        formatter: monolog.formatter.json
        channels: ["channel_a"]
      channel_b:
        type: stream
        path: "%kernel.logs_dir%/channel_b.log"
        level: info
        formatter: monolog.formatter.json
        channels: ["channel_b"]
      php_stderr:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%.log"
        level: error
        formatter: monolog.formatter.json
        channels: ["!channel_a", "!channel_b"]
      syslog_server:
        type: syslogudp
        host: myhostname

Yohan Leafheart
  • 860
  • 1
  • 11
  • 27

0 Answers0