Custom monolog handler for default monolog in Symfony 2 - php

I want to add a custom handler to a default monolog in Symfony 2.
In my config.yaml file, I have:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
myHandler:
type: Acme\MyBundle\Monolog\MyCustomHandler
level: error
My class looks like below:
// Acme\MyBundle\Monolog\MyCustomHandler
use Monolog\Logger;
use Monolog\Handler\SocketHandler;
use Monolog\Formatter\LineFormatter;
class MyCustomHandler extends AbstractProcessingHandler
{
...
}
But even before I fill my class in I get an error:
invalid handler type "acme\mybundle\monolog\mycustomhandler" given for handler "myHandler"
How do I add a custom handler to the default monolog without creating a new monolog service?

Try this:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
custom:
type: service
id: my_custom_handler
services:
my_custom_handler:
class: Acme\MyBundle\Monolog\MyCustomHandler
If you want to use it as default handler then you should change a bit monolog section I wrote above.
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
handler: custom
custom:
type: service
id: my_custom_handler
I hope it helps you.

I just found out that Monolog ships with a set of various handlers so you might wanna use one of those instead of writing your own. I am using the LogEntriesHandler for logging to logentries.com but there are a few more as documented here: https://github.com/Seldaek/monolog#log-specific-servers-and-networked-logging
My Symfony2 config for that looks like that:
monolog:
main:
type: fingers_crossed
level: debug
handler: nested
custom:
type: service
id: monolog.handler.logentries
level: error
services:
monolog.handler.logentries:
class: Monolog\Handler\LogEntriesHandler
arguments:
token: %logentries_token%

Related

monolog configuration in symfony and customization

I am working on a web app built using Symfony 2.6 and there are different configuration inside app/config folder. How do I know which one is being used.
Inside app/config I see config.yml config_dev.yml config_prod.yml and the monolog entry I see in config_dev.yml and also in monolog_prod.yml. It is as below.
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
Now I want to use monolog to input some logs in a controller and put those logs separate form other logs. How can this be done?.
You know which one is used on your environment.
If you are in dev mode you are using config_dev.yml merged with config.yml
You can check it into your virtual host if the file is on app.php you are in prod mode, if the file points to app_dev.php you are in dev mode usually
To log into other files you can create a channel like this:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
your_handler:
level: debug
type: stream
path: '%kernel.logs_dir%/custom.log'
channels: ['your_channel']
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
And into your controller you can call it in this way:
$logger = $this->get('monolog.logger.your_handler');
$logger->debug('your custom message into your custom log file');

Symfony3 amendment of the logger convention

Welcome,
My task is to change the convention of loggers in the application. Currently, loggers are configured as dedicated services. I would like to define the loggers in the Symfony convention and ensure backward compatibility by defining DI directing the old names of logger services to the new ones in the monolog.logger.channel convention. I have the loggers in Symfony do things for the first time or can anyone give me an example of how to do it? Thank you in advance and I greet you below with my current configuration:
app/config/config_dev.yml:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ['!my_channel']
my_handler:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.my_handler.log"
level: debug
channels: ['my_channel']
console:
type: console
bubble: false
app/config/config_prod.yml:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
channels: ['!my_channel']
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
my_channel:
type: fingers_crossed
action_level: error
handler: my_channel_stream
my_channel_stream:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.my_channel.log"
level: debug
channels: ['my_channel']
src/bundle/Resources/config/logger.yml:
services:
logger:
class: Symfony\Bridge\Monolog\Logger
arguments: [logger]
calls:
- [pushHandler, ['#monolog.handler.console']]
- [pushHandler, ['#logger_handler']]
logger_handler:
class: Monolog\Handler\StreamHandler
arguments: ["%kernel.logs_dir%/%kernel.environment%.cof.log", 200]
src/bundle/Resources/config/config.yml:
monolog:
channels: ['my_channel']

Set monolog formatter in config.yml

Is there a possibility to simply just add formatter: json on config.yml of Symfony, in order to define what formatter to use on every Monolog instance?
app/config/config.yml
services:
json:
class: Monolog\Formatter\JsonFormatter
monolog:
handlers:
file:
type: stream
level: debug
formatter: json

Symfony2 Monolog configure to use raven handler (Sentry)

I want to use sentry to evaluate possible errors, exceptions, etc.
I tried to use the KunstmaanSentryBundle and it's great to catch all kind of errors like undefined functions and so on, but I want to define my own Monolog channel with it's own handler, but unfortunately I haven't found documentation about it.
config.yml would be something like:
monolog:
handlers:
sentry:
type: stream
level: error
//Log to database parameter (raven for sentry)
Does anybody knows the right configuration?
This is part of config_prod.yml:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped_main
sentry:
type: raven
dsn: 'http://user:pass#url/1'
level: notice
# Groups
grouped_main:
type: group
members: [sentry, streamed_main]
# Streams
streamed_main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: error
Enjoy! :)
Wanted to add this as a comment on the accepted answer, but not enough rep, so:
From \Symfony\Bundle\MonologBundle\DependencyInjection\Configuration :
"Possible handler types and related configurations (brackets indicate optional params):
raven:
dsn: connection string
client_id: Raven client custom service id (optional)
[level]: level name or int value, defaults to DEBUG
[bubble]: bool, defaults to true
"
Example config would be:
monolog:
handlers:
sentry:
type: raven
dsn: '%sentry_api_key%'
client_id: 'your.raven.client.custom.service.id'
level: notice
bubble: false

yaml/symfony2: Override configurations

I wanto to override some configurations from config_dev.yml in my config_test.yml. So, imagine the following part in the config_dev.yml:
monolog:
handlers:
main:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
firephp:
type: firephp
level: info
In my test environment, I want no logger at all. So I tried
monolog: ~
with no effect. I also tried:
monolog:
handlers:
main: ~
firephp: ~
again without any effect. Then I tested
monolog:
handlers:
main:
type: ~
path: ~
level: ~
firephp:
type: ~
level: ~
and I get a ErrorException Couldn't find constant Monolog\Logger::. If anybody could point out a way to override the monolog settings I would very much appreciate it. Thanks!
It's better to define handlers as empty array:
monolog:
handlers: []
UPD1: There are special type of loggers: test and null, you can use them:
monolog:
handlers:
test:
type: test
level: debug
If you're using the Symfony2 Standard Edition
Your config_dev.yml looks something like this for monolog out of the box:
# config_dev.yml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
As you can see this defines the handlers main and nested where nested is only used because it's referenced by main.
config_dev.yml is imported from config_test.yml so
if you want to override the configuration for your test environment you need to override the mainhandler in config_test.yml:
# config_text.yml
monolog:
handlers:
main:
type: test
This will stop monolog from creating a log file.
Have you tried:
monolog:
handlers: ~
It should work (I think).
Look here
Without handlers, monolog is not load.

Categories