Skip to content

some missing formats #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions cookbook/logging/monolog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ allows you to log the messages in several ways easily.

.. code-block:: yaml

# app/config/config.yml
monolog:
handlers:
applog:
Expand All @@ -75,8 +76,10 @@ allows you to log the messages in several ways easily.
syslog:
type: syslog
level: error

.. code-block:: xml

<!-- app/config/config.xml -->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
Expand Down Expand Up @@ -109,6 +112,32 @@ allows you to log the messages in several ways easily.
</monolog:config>
</container>

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'applog' => array(
'type' => 'stream',
'path' => '/var/log/symfony.log',
'level' => 'error',
),
'main' => array(
'type' => 'fingers_crossed',
'action_level' => 'warning',
'handler' => 'file',
),
'file' => array(
'type' => 'stream',
'level' => 'debug',
),
'syslog' => array(
'type' => 'syslog',
'level' => 'error',
),
),
));

The above configuration defines a stack of handlers which will be called
in the order where they are defined.

Expand Down Expand Up @@ -137,6 +166,7 @@ easily. Your formatter must implement

.. code-block:: yaml

# app/config/config.yml
services:
my_formatter:
class: Monolog\Formatter\JsonFormatter
Expand All @@ -149,6 +179,7 @@ easily. Your formatter must implement

.. code-block:: xml

<!-- app/config/config.xml -->
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
Expand All @@ -158,6 +189,7 @@ easily. Your formatter must implement
<services>
<service id="my_formatter" class="Monolog\Formatter\JsonFormatter" />
</services>

<monolog:config>
<monolog:handler
name="file"
Expand All @@ -168,6 +200,22 @@ easily. Your formatter must implement
</monolog:config>
</container>

.. code-block:: php

// app/config/config.php
$container
->register('my_formatter', 'Monolog\Formatter\JsonFormatter');

$container->loadFromExtension('monolog', array(
'handlers' => array(
'file' => array(
'type' => 'stream',
'level' => 'debug',
'formatter' => 'my_formatter',
),
),
));

Adding some extra data in the log messages
------------------------------------------

Expand Down Expand Up @@ -243,6 +291,59 @@ using a processor.
level: debug
formatter: monolog.formatter.session_request

.. code-block:: xml

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<services>
<service id="monolog.formatter.session_request" class="Monolog\Formatter\LineFormatter">
<argument>[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n</argument>
</service>

<service id="monolog.processor.session_request" class="Acme\MyBundle\SessionRequestProcessor">
<argument type="service" id="session" />
<tag name="monolog.processor" method="processRecord" />
</service>
</services>

<monolog:config>
<monolog:handler
name="main"
type="stream"
path="%kernel.logs_dir%/%kernel.environment%.log"
level="debug"
formatter="monolog.formatter.session_request"
/>
</monolog:config>
</container>

.. code-block:: php

// app/config/config.php
$container
->register('monolog.formatter.session_request', 'Monolog\Formatter\LineFormatter')
->addArgument('[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n');

$container
->register('monolog.processor.session_request', 'Acme\MyBundle\SessionRequestProcessor')
->addArgument(new Reference('session'))
->addTag('monolog.processor', array('method' => 'processRecord'));

$container->loadFromExtension('monolog', array(
'handlers' => array(
'main' => array(
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
'formatter' => 'monolog.formatter.session_request',
),
),
));

.. note::

If you use several handlers, you can also register the processor at the
Expand Down
59 changes: 59 additions & 0 deletions cookbook/logging/monolog_email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ it is broken down.
/>
</monolog:config>
</container>

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'mail' => array(
'type' => 'fingers_crossed',
'action_level' => 'critical',
'handler' => 'buffered',
),
'buffered' => array(
'type' => 'buffer',
'handler' => 'swift',
),
'swift' => array(
'type' => 'swift_mailer',
'from_email' => '[email protected]',
'to_email' => '[email protected]',
'subject' => 'An Error Occurred!',
'level' => 'debug',
),
),
));


The ``mail`` handler is a ``fingers_crossed`` handler which means that
it is only triggered when the action level, in this case ``critical`` is reached.
Expand Down Expand Up @@ -154,6 +179,40 @@ get logged on the server as well as the emails being sent:
</monolog:config>
</container>

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('monolog', array(
'handlers' => array(
'main' => array(
'type' => 'fingers_crossed',
'action_level' => 'critical',
'handler' => 'grouped',
),
'grouped' => array(
'type' => 'group',
'members' => array('streamed', 'buffered'),
),
'streamed' => array(
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
),
'buffered' => array(
'type' => 'buffer',
'handler' => 'swift',
),
'swift' => array(
'type' => 'swift_mailer',
'from_email' => '[email protected]',
'to_email' => '[email protected]',
'subject' => 'An Error Occurred!',
'level' => 'debug',
),
),
));


This uses the ``group`` handler to send the messages to the two
group members, the ``buffered`` and the ``stream`` handlers. The messages will
now be both written to the log file and emailed.
Expand Down
70 changes: 57 additions & 13 deletions cookbook/templating/global_variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,45 @@ How to Inject Variables into all Templates (i.e. Global Variables)
Sometimes you want a variable to be accessible to all the templates you use.
This is possible inside your ``app/config/config.yml`` file:

.. code-block:: yaml
.. configuration-block::

# app/config/config.yml
twig:
# ...
globals:
ga_tracking: UA-xxxxx-x
.. code-block:: yaml

# app/config/config.yml
twig:
# ...
globals:
ga_tracking: UA-xxxxx-x

.. code-block:: xml

<!-- app/config/config.xml -->
<twig:config ...>
<!-- ... -->
<twig:global key="ga_tracking">UA-xxxxx-x</twig:global>
</twig:config>

.. code-block:: php

// app/config/config.php
$container->loadFromExtension('twig', array(
...,
'globals' => array(
'ga_tracking' => 'UA-xxxxx-x',
),
));

Now, the variable ``ga_tracking`` is available in all Twig templates:

.. code-block:: html+jinja
.. configuration-block::

.. code-block:: html+jinja

<p>The google tracking code is: {{ ga_tracking }}</p>

<p>The google tracking code is: {{ ga_tracking }} </p>
.. code-block:: html+php

<p>The google tracking code is: <?php echo $ga_tracking; ?></p>

It's that easy! You can also take advantage of the built-in :ref:`book-service-container-parameters`
system, which lets you isolate or reuse the value:
Expand All @@ -30,12 +56,30 @@ system, which lets you isolate or reuse the value:
[parameters]
ga_tracking: UA-xxxxx-x

.. code-block:: yaml
.. configuration-block::

.. code-block:: yaml

# app/config/config.yml
twig:
globals:
ga_tracking: "%ga_tracking%"

.. code-block:: xml

<!-- app/config/config.xml -->
<twig:config ...>
<twig:global key="ga_tracking">%ga_tracking%</twig:global>
</twig:config>

.. code-block:: php

# app/config/config.yml
twig:
globals:
ga_tracking: "%ga_tracking%"
// app/config/config.php
$container->loadFromExtension('twig', array(
'globals' => array(
'ga_tracking' => '%ga_tracking%',
),
));

The same variable is available exactly as before.

Expand Down
Loading