Sending messages to microsoft teams using Symfony Monolog - symfony

Alright so I am trying to send messages to Microsoft teams using Symfony Monolog, but I cannot seem to find any sort of guides pointing how to do it or such.
I have tried doing it the same way as you would do it using Slack
I am using Symfony 3.4
monolog:
handlers:
main:
type: fingers_crossed
action_level: critical
handler: mongo
# nested:
# type: stream
# path: %kernel.logs_dir%/%kernel.environment%.log
# level: critical
teams:
type: teams
level: critical
token: '%teams_token%'
channel: '#errors'
include_extra: true

You have to create your own Handler like the SlackHandler.
<?php
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
class MicrosoftTeamsHandler extends AbstractProcessingHandler
{
...
Check this response Custom monolog handler for default monolog in Symfony 2 to register your custom handler and use it

Related

How to use NotifierHandler with monolog in Symfony 5.4

i can't use this handler in monolog configuration :
https://github.com/symfony/symfony/blob/5.4/src/Symfony/Bridge/Monolog/Handler/NotifierHandler.php
monolog:
handlers:
discord:
type: notifier
action_level: error
this is the error message : There is no handler class defined for handler "notifier".
How can i inject this handler ?
BR, Thomas
You can add the notifier handler to services.yaml
See: https://manyou.blog/send-symfony-logs-to-slack-with-symfony-notifier-as-a-monolog-handler#heading-add-notifierhandler-as-service

Symfony 4 enable logging with Monolog's Redis handler

I have a working ELK stack connected to Redis.
I also have a working stateless Symfony 4 application and I want to send all the production logs to my Redis.
I know Monolog has a Redis handler, but I don't know how I'm supposed to tweak the config/prod/monolog.yaml file to accomplish this of if there’s another approach.
This is how it looks right now:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404]
nested:
type: stream
path: "php://stderr"
level: debug
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
deprecation:
type: stream
path: "php://stderr"
deprecation_filter:
type: filter
handler: deprecation
max_level: info
channels: ["php"]
The approach I took was, first installing the predis client:
composer require predis/predis
Then create a custom service class that extends the RedisHandler class that comes with the Monolog package:
namespace App\Service\Monolog\Handler;
use Monolog\Handler\RedisHandler;
use Monolog\Logger;
use Predis\Client as PredisClient;
class Redis extends RedisHandler
{
public function __construct( $host, $port = 6379, $level = Logger::DEBUG, $bubble = true, $capSize = false)
{
$predis = new PredisClient( "tcp://$host:$port" );
$key = 'logstash';
parent::__construct($predis, $key, $level, $bubble, $capSize);
}
}
Next, activate the service we just created on the services.yml config file:
services:
monolog.handler.redis:
class: App\Service\Monolog\Handler\Redis
arguments: [ '%redis.host%' ]
Be sure the parameter redis.host is set and points to your Redis server. In my case, my parameter value is the IP of my Redis server.
I added other parameters to the class like port and log level. You can set it at the moment of instantiating your service like with the host parameter.
Finally, configure your custom log handler service in your monolog.yaml config file. In my case, I need it only the production logs with the config as follow:
handlers:
custom:
type: service
id: monolog.handler.redis
level: debug
channels: ['!event']

How can one use a defined channel, in monolog handler from a tagged service?

I have the following monolog handler definitions:
# config_prod.yml
app_generic:
type: rotating_file
max_files: 15
path: "%param.app_logging_config.log_generic_file%"
level: info
channels: [app]
app_api:
max_files: 15
path: "%param.app_logging_config.log_api_file%"
level: info
channels: [app]
level: info
app_response:
max_files: 15
path: "%param.app_logging_config.log_response_file%"
channels: [app]
level: info
And in service.yml, my intention is to inject monolog (#logger) with an array of the above defined handlers.
#service.yml
app.app_logger:
class: AppBundle\Classes\AppLogger
arguments: ['#logger': ['#app_generic', '#app_api', '#app_response']]
calls:
- [init, ['%app_logging_config%']
tags:
- { name: monolog.logger, channel: app }
How does one pass arguments to an injected argument?
Update:
Re-reading the description, I was going for this approach, by just tagging on the service definition:
app.logger:
arguments: ['#logger']
tags:
- { name: monolog.logger, channel: app }
channels: ['app']
Or even ( if I understood correctly), adding a channels: ['app'] key and just having this in service argument:
app.logger:
arguments: ['#monolog.logger.app']
I have not been able to use ( or see via dump ) the handlers defined in config_prod.yml. I have placed these at top because of other "fingers_crossed" handlers I thought may interfere.
I woud like to know, why neither of above (documented) approaches seem to work?
Handlers in
monolog:
handlers:
handler1: ...
handler2: ...
are automatically injected in #logger service.
It looks like you need new custom logger. Please read about The DependencyInjection Component
Create dependencies
services:
app_generic:
....
app_api:
....
app_response:
....
Create custom_logger service
custom_logger:
class: Monolog\Logger
arguments: ["my logger", ["#app_generic", "#app_api", "#app_response"]
Inject you custom logger in you service
app.app_logger:
class: AppBundle\Classes\AppLogger
arguments: ['#custom_logger']
calls:
- [init, ['%app_logging_config%']

Symfony 2 - Don't pass logging to next handler

I've defined a service to handle logging of records with specific text (e.g. "matched route" and such). I'm successfully writing those records out to a separate file.
However, what I want to avoid is writing them a second time to the primary log file. I've tried tagging my service with a channel and telling the main stream log to ignore that channel, but it occurs to me that it won't work since, in essence, the main stream logging handler matches everything.
Any advice on how to proceed?
EDIT: Here's what I have at the moment:
monolog:
channels: ['noise']
handlers:
eliminate_noise_logger:
type: service
id: common.eliminate_noise_logger
channels: ['noise']
streamed:
type: stream
level: info
path: "%kernel.logs_dir%/%kernel.environment%.log"
formatter: monolog.formatter.session_request
channels: ['!noise']
console:
type: console
verbosity_levels:
VERBOSITY_NORMAL: INFO
And my services definition:
common.eliminate_noise_logger:
class: path\to\class
arguments: ["%kernel.logs_dir%/%kernel.environment%.noise.log", ["str1", "str2", "str3"]]
tags:
- { name: monolog.logger, channel: noise }
This is how I do
#config.yml
monolog:
use_microseconds: false
channels: ['secondary']
handlers:
main:
path: %kernel.logs_dir%/%kernel.language%/%kernel.environment%.log
type : fingers_crossed
action_level : error
handler : nested
channels : ['!secondary']
secondary:
type: rotating_file
max_files: 10
path: %kernel.logs_dir%/%kernel.language%/%kernel.environment%.secondary.log
level: info
channels: [secondary]

Symfony2 + Monolog: Different actions for different error levels

I have set up Monolog for our Symfony2 project to email out critical errors to us when they occur.
However I would also like to log non-critical errors also, and to email these errors out to different recipients. I am struggling to see this in the documentation however it looks like it should be possible. I have set up the config as follows:
parameters:
error_mail_sender: error#mysite.com
error_mail_recipients: [siteerrors#mysite.com]
critical_error_mail_recipients: [siteerrors#mysite.com, developer#developers.com]
monolog:
handlers:
main_critical:
type: fingers_crossed
action_level: critical
handler: grouped_critical
bubble: false
main_error:
type: fingers_crossed
action_level: error
handler: grouped_error
grouped_critical:
type: group
members: [streamed, buffered_critical]
grouped_error:
type: group
members: [streamed, buffered_error]
streamed:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
buffered_critical:
type: buffer
handler: swift_critical
buffered_error:
type: buffer
handler: swift_error
swift_critical:
type: swift_mailer
from_email: %error_mail_sender%
to_email: %error_mail_recipients%
subject: Critical error occurred!
level: debug
swift_error:
type: swift_mailer
from_email: %error_mail_sender%
to_email: %critical_error_mail_recipients%
subject: Non-critical error occurred
level: debug
With this set up we receive the critical errors but don't get the non-critical errors.
This set up was loosely based on the (unaccepted) answer to this question: How to include the severity of a log in the e-mail subject?. (I would have up-voted the answer had it worked for me!!)
Can anyone spot what is wrong with this?
Thanks!
The problem seems to be bubble: false in the first handler, that means it'll stop propagating messages to other handlers.
Another note, I would remove the group and the streamed handler from grouped_critical, because it will already receive errors from grouped_error, resulting in duplicate entries in the log file.
So this should work:
parameters:
error_mail_sender: error#mysite.com
error_mail_recipients: [siteerrors#mysite.com]
critical_error_mail_recipients: [developer#developers.com]
monolog:
handlers:
main_critical:
type: fingers_crossed
action_level: critical
handler: buffered_critical
buffered_critical:
type: buffer
handler: swift_critical
swift_critical:
type: swift_mailer
from_email: %error_mail_sender%
to_email: %error_mail_recipients%
subject: Critical error occurred!
level: debug
main_error:
type: fingers_crossed
action_level: error
handler: grouped_error
grouped_error:
type: group
members: [streamed, buffered_error]
streamed:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
buffered_error:
type: buffer
handler: swift_error
swift_error:
type: swift_mailer
from_email: %error_mail_sender%
to_email: %critical_error_mail_recipients%
subject: Non-critical error occurred
level: debug
I reordered them to make it easier to see the two handler chains as well.

Resources