How do I log to different files with Monolog in Symfony2? - symfony

I want to log messages to separate files depending on the context. Currently everything is going into the environment's log file. How can this be accomplished with Monolog in Symfony2?

In your config files (config.yml or config_dev.yml and config_prod.yml) under monolog: register new handlers for each file you want to log to.
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
the environment's log file will receive everything from debug level up, but the main logger service will only log with action level error and up.
Add your own handlers for the files you want to log to in your config files.
cron_logger:
type: stream
path: "%kernel.logs_dir%/cron.log"
level: notice
channels: "cronchan"
You can choose which level of messages will be logged in the file. You should specify which channel will log to this handler. In this case the channel name chosen was "cronchan". At the same level as handlers, register your new channel (you need MonologBundle 2.4 for this).
monolog:
handlers:
...
cron_logger: ...
channels: ["cronchan"]
You can now log specifically to the cron.log file by getting the correct channel's service in a controller: $logger = $this->get('monolog.logger.cronchan');
Now everything from notice level and up will be logged in the cron.log file. The messages will possibly also be logged in the environment's log file, but you can remove the channel from the file's handler's channels.

Related

How to configure sending errors by e-mail with Symfony 5.1, Monolog and Mailer component?

Do you have an example configuration to send logs by email with the Mailer component of Symfony 5.1.
In the Symfony blog we announce this feature, but I can't put the right config in monolog.yaml
https://symfony.com/blog/new-in-symfony-5-1-misc-improvements-part-3 :
That's why in Symfony 5.1 we added a new Monolog log handler which uses the Mailer component to send the logs via email.
Unfortunately this addition only covers the actuall MailerHandler class in the monolog-bridge. This does not cover the possibility to configure it in the monolog-bundle (that's the drawback if those components are distributed over multiple packages).
The PR for the change in the monolog-bundle is still open, and can be found here: Add Symfony Mailer support #354.
If you don't want to wait for the change in the monolog-bundle you could already use it by defining the handler as a service and then using it with the service type in the monolog configuration.
So define your service:
services:
# this configures the "mail" as a prototype, so you can
# define the sender and recipient mail addresses here
symfony_mailer_service_template:
class: Symfony\Component\Mime\Email
calls:
- ['from', ['webapp#example.com']]
- ['to', ['ops#example.com']]
- ['subject', ['Logs']]
symfony_mailer_service:
class: Symfony\Bridge\Monolog\Handler\MailerHandler
arguments:
- '#mailer.mailer'
- '#symfony_mailer_service_template'
- !php/const Monolog\Logger::DEBUG # log level
- true # bubble
And then in your mailer configuration you could use it like this:
monolog:
handlers:
main:
type: fingers_crossed
handler: deduplicated
deduplicated:
type: deduplication
handler: symfony_mailer
symfony_mailer:
type: service
id: symfony_mailer_service

Log user actions in separate files by userId and date in Symfony2

I need to separate my Logs of my Symfony2 project by user and day.
For example:
- 122-20150519.log (userId: 122, date: 2015-05-19)
I have now separate logs by day with the Monolog setup in config.yml like this:
my_log:
type: rotating_file
path: "%kernel.logs_dir%/my_log_%kernel.environment%.log"
channels: [my_log]
But now I need to separate it by userId.
Do you know how can it be ?
Thanks ¡¡

Symfony2 authenticate user through an API on integration testing

I would like to authenticate the user through an API (external user provider) running my integration tests.
my config_test.yml
imports:
- { resource: config.yml }
- { resource: parameters_test.yml }
framework:
test: ~
session:
storage_id: session.storage.mock_file
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: info
After the login (SUCCEED) the next step is ask for user information and I got:
"Full authentication is required to access this resource."
I guess something is happening with the stored session.
I am using Redis on dev and prod to store sessions.
Mocking sessions on the test environment because of
"Failed to start the session because headers have already been sent by "/path/to/bin/phpunit" at line 2."
Doing it manually is working like a charm.
session.storage.mock_file uses by default %kernel.cache_dir%/sessions for a storing of session's file. Check if this folder is writable for both server user and cli user.
'Setting up Permissions' http://symfony.com/doc/current/book/installation.html#configuration-and-setup
Another 2 possible reasons of error:
stateless: true on your firewall in firewalls section. The ContextListener is not created and Token is not saved to the session. AnonymousToken is assigned to next request.
stateless: true you also can have RememberMeToken instead of AnonymousToken if remember_me feature enabled. This token is also not full fledged.
UPDATE
Ensure that intercept_redirects is false in config_test.yml as it may break all redirects.
web_profiler:
toolbar: false
intercept_redirects: false

symfony2 exceptions without logging

There are several places in my application where I throw an exception as part of the business logic, but I don't need Symfony2 to generate and log an exception.
For example, when the user attempts an action he can't do temporarily, I call $this->createAccessDeniedException() with a nice error message. This is not an application failure and I don't need it logged. Mostly it's a message to the user saying "try again later, or after you completed this other step first".
Can I teach it to not log certain cases or am I using exceptions badly and if so, what should I do instead?
update: My config:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped
grouped:
type: group
members: [streamed, buffered]
streamed:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
buffered:
type: buffer
handler: swift
swift:
type: swift_mailer
from_email: xxx#xxx.xxx
to_email: xxx#xxx.xxx
subject: "Error"
level: info
As you can see in the ExceptionListener, the AccessDeniedException should not be logged as a normal exception, but as a special message on the DEBUG level.
If you are seeing these messages in your production log files, you should check the minimal level configured for Monolog. I recommend to set it NOTICE.

Disable log in Symfony2

This question may have been asked before. I have searched for answers, but I haven't found what I was looking for.
In Symfony 2.3, is there a way to disable the logger for specific requests? I mean, I am using a SOAP service for my project. When I send a request to login, the username and password are dumped straight as plain text into the log file. Is there a way to stop logging this kind of specific requests?
For example, when I send a request for login, the logger should be disabled, but for all other request it works again. Is this possible?
depending if your are in Prod or Dev environement but everything is in config.yml or config_dev.yml :
to disable logging just remove monolog configuration like this :
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
bubble: false
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
but in my opinion , you shouln't do this because logging allows you to improve significantly your code !
Logging except for a specific service :
You need to create a specific log channel for your service as described there :
http://symfony.com/doc/current/cookbook/logging/channels_handlers.html
and there :
http://symfony.com/doc/current/reference/dic_tags.html#dic-tags-monolog
you ll be able to separate your soap log from others and eventually send it to null

Resources