Symfony2 Monolog configure to use raven handler (Sentry) - php

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

Related

Saving command logger output to log file and console

I wrote a very simple test command which has LoggerInterface injected in its constructor.
How am I suppose to change the monolog.yaml configuration to save this logger output to both log file and to output it to console?
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
formatter: monolog.line.formatter
handler: terminal
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
terminal:
type: stream
path: "php://stderr"
level: debug
console:
type: console
process_psr_3_messages: false
channels: [ "!event", "!doctrine" ]
The commands will always stderr by default (if you specify the -vvv option)
If you need to write the logs in a file only on error (with the stack error) you can use the finger_crossed handler :
handlers:
main:
# fingers_crossed allow to log only if action_level defined is reach
type: fingers_crossed
# minimum level to activate the handler
# available level (emergency|alert|critical|error|warning|notice|info|debug){1}
action_level: error
# wrapped handler's name
handler: nested
nested:
# stream allow to write log in file
type: stream
# path to the log file
path: "%kernel.logs_dir%/%project_name%_%kernel.environment%.log"
# available level (emergency|alert|critical|error|warning|notice|info|debug){1}
level: debug
If you want to filter a bit the logs shown in the stderr you can use the default config for the console :
console:
type: console
process_psr_3_messages: false
channels: ['!event', '!doctrine', '!console']
I'll allow you to have nicer console logs (and avoid too many "useless" logs such as event or doctrine which are very verbose)
You can use the "group" handler https://github.com/symfony/monolog-bundle/blob/master/DependencyInjection/Configuration.php#L137
Check an implementation here https://symfony.com/doc/current/logging/monolog_email.html
You can do this like your terminal handler does. copy the terminal handler and set the path to the file. Both handlers will be executed.
monolog:
handlers:
main:
...
terminal:
...
terminal_file:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%-terminal.log"
level: debug
You can also do this to log different channels to different files.
Sometimes i use this, to log doctrine debug messages to a different file, by adding the doctrine channel.
monolog:
handlers:
...
doctrine_debug:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%-doctrine.log"
level: debug
channels: ["doctrine"]

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');

Symfony do not log deprecations

i want the log to behave like the regular php-log in production.
There is a logrotate in place and the server-logs are analyzed with other tools.
So i set up my config_prod.yml like that:
monolog:
use_microseconds: false
handlers:
main:
type: error_log
level: WARNING
deduplicated:
type: deduplication
time: 60
handler: main
Somehow the deprecations (via #trigger_error) appear in my log, too.
Can i disable the logging of the deprecations somehow?
I don't need them in prod but only in dev.
Kind regards.
Patrick
You can try to exclude a channel php from your handler
use_microseconds: false
handlers:
main:
type: error_log
level: WARNING
deduplicated:
type: deduplication
time: 60
handler: main
channels: ['!php']

Graylog / Symfony2 / Gelf: How to configure logger to ignore errors?

I'm trying to stream logs from a symfony 2 app to a graylog 2 server using the gelf format.
My monolog configuration looks as follows:
monolog:
handlers:
# --- 8< ---
# ...
# --- >8 ---
graylog:
type: gelf
publisher:
hostname: my-graylog-server.com
port: 12201
level: debug
formatter: app.gelf_formatter
When the graylog server is not available, I get (understandably) a connection refused error
[2017-07-28 16:03:25] app.ERROR: Failed to write to socket: fwrite(): send of 153 bytes failed with errno=111 Connection refused (8) [] []
which results in an internal server error (500 response code to the request causing the log).
(See also this question: Prevent Internal Server Error with Symfony 2 / Monolog on failed gelf connection)
gelf-php provides an IngoreErrorTransportLogger, which seems to be build for this exact purpose.
How can I configure this in Symfony's monolog config?
It turns out that Symfony has WhatFailureGroup handler wrapper a handler which ignores errors from the logs of all logging handlers that it wraps.
Our config now looks like this, it just drops errors during logging (probably not ideal, but better than failing outright due to an error during logging).
monolog:
handlers:
main:
type: fingers_crossed
action_level: warning
handler: grouped
grouped:
type: whatfailuregroup
members: [file, graylog]
file:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
graylog:
type: gelf
publisher:
hostname: my-graylog-server.com
port: 12201
level: debug
formatter: app.gelf_formatter
Essentially we replaced type: group with type: whatfailuregroup.
You can additionally use UDP transport in Graylog.
First you'd need to configure GELF UDP input in Graylog, then configure it like this:
monolog:
handlers:
# ...
graylog:
type: gelf
publisher:
id: gelf_publisher
nested: true
services:
gelf_publisher:
class: Gelf\Publisher
arguments:
- '#gelf_transport'
gelf_transport:
class: Gelf\Transport\UdpTransport
arguments:
- '%env(GRAYLOG_HOSTNAME)%'
- '%env(GRAYLOG_PORT)%'

Custom monolog handler for default monolog in Symfony 2

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%

Categories