Symfony MonologBundle: Use a sentry processor inside Symfony's MonologBundle - symfony

I've configured a Sentry handler for Monolog via config.yml. Now I need to filter out some data from the log data. The data is added in sentry's raven_handler. Thus I need to use a Sentry processor to filter it out. Monolog processors are working before the log goes to the sentry.
I do not find a way to pass a Sentry processor via Monolog's .yml configuration.
Do you have any suggestions?

Related

Symfony logging with Monolog, confused about STDERR

I am trying to align my logging with the best practice of using STDERR.
So, I would like to understand what happens with the logs sent to STDERR.
Symfony official docs (https://symfony.com/doc/current/logging.html):
In the prod environment, logs are written to STDERR PHP stream, which
works best in modern containerized applications deployed to servers
without disk write permissions.
If you prefer to store production logs in a file, set the path of your
log handler(s) to the path of the file to use (e.g. var/log/prod.log).
This time I want to follow the STDERR stream option.
When I was writing to a specific file, I knew exactly where to look for that file, open it and check the logged messages.
But with STDERR, I don't know where to look for my logs.
So, using monolog, I have the configuration:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
nested:
type: stream
path: "php://stderr"
level: debug
Suppose next morning I want to check the logs. Where would I look?
Several hours of reading docs later, my understanding is as follows:
First, the usage of STDERR over STDOUT is preferred for errors because it is not buffered (gathering all output waiting for the script to end), thus errors are thrown immediately to the STDERR stream. Also, this way the normal output doesn't get mixed with errors.
Secondly, the immediate intuitive usage is when running a shell script, because in the Terminal one will directly see the STDOUT and STDERR messages (by default, both streams output to the screen).
But then, the non-intuitive usage of STDERR is when logging a website/API. We want to log the errors, and we want to be able to monitor the already occurred errors, that is to come back later and check those errors. Traditional practice stores errors in custom defined log-files. More modern practice recommends sending errors to STDERR. Regarding Symfony, Fabien Potencier (the creator of Symfony), says:
in production, stderr is a better option, especially when using
Docker, SymfonyCloud, lambdas, ... So, I now recommend to use
php://stderr
(https://symfony.com/blog/logging-in-symfony-and-the-cloud).
And he further recommends using STDERR even for development.
Now, what I believe to be missing from the picture (at least for me, as non-expert), is the guidance on HOW to access and check the error logs. Okay, we send the errors to STDERR, and then? Where am I going to check the errors next morning? I get it that containerized platforms (clouds, docker etc) have specific tools to easily and nicely monitor logs (tools that intercept STDERR and parse the messages in order to organize them in specific files/DBs), but that's not the case on a simple server, be it a local server or on a hosting.
Therefore, my understanding is that sending errors to STDERR is a good standardization when:
Resorting to using a third-party tool for log monitoring (like ELK, Grail, Sentry, Rollbar etc.)
When knowing exactly where your web-server is storing the STDERR logs. For instance, if you try (I defined a new STD_ERR constant to avoid any pre-configs):
define('STD_ERR', fopen('php://stderr', 'wb'));
fputs(STD_ERR, "ABC error message.");
you can find the "ABC error message" at:
XAMPP Apache default (Windows):
..\xampp\apache\logs\error.log
Symfony5 server (Windows):
C:\Users\your_user\.symfony5\log\ [in the most recent folder, as the logs rotate]
Symfony server (Linux):
/home/your_user/.symfony/log/ [in the most recent folder, as the logs rotate]
For Symfony server, you can actually see the logs paths when starting the server, or by command "symfony server:log".
One immediate advantage is that these STDERR logs are stored outside of the app folders, and you do not need to maintain extra writable folders or deal with the permissions etc. Of course, when developing/hosting multiple sites/apps, you need to configure the error log (the STDERR storage) location per app (in Apache that would be inside each <VirtualHost> conf ; with Symfony server, I am not sure). Personally, without a third-party tool for monitoring logs, I would stick with custom defined log files (no STDERR), but feel free to contradict me.

Finding status if MDB is running

I want to use mbeans on startup of j2ee application to check if all the MDBs are running and jms specification has been activated.
Any pointers will be very helpful
The only way I know of to do this would be to use the ServerEndpointControl MBean. This is a Liberty specific MBean for controlling the input sources for work into the runtime. This can also be used to get status on http listeners.
The best place to find the Javadoc for the MBean is here. To find out if an MBean is running you call the isPaused method providing the MDB name which is defined as:
ApplicationName#ModuleName#BeanName
if the MDB is running it'll return false.

What is the best practice to use profiler data from production system?

Assuming I have a running symfony application and it encounters an exception with following configuration:
framework:
profiler:
lifetime: 604800
only_exceptions: true
Then there should be a dump with profiling information.
But what happen next?
Just copy the file to your own local profiler data folder and start the profiler?
What are the best practices to handle and debug exceptions occuring on the production system?
I think enabling profiler even with only_exceptions: true should have performance impact, because to display something on exception is should be collected first in any way.
If you want to see the profiler data from another host, you can export and import it locally
For me more correct way is to just log events or email exception with stacktrace to admin via kernel exception listeners. Within listener you can access any info you need to send or log, i.e. request stack, logged in user info etc

How can I clear Sonatype Nexus log files

According to the Sonatype Nexus Docs - logging, I can set the logging levels (debug, warnings, errors). But how do I actually clear the logs that sit on my server without SSHing in and deleting them? They're approaching 1Gb.
Seems this should be a scheduled task, like empty the trash.
Typically if you set up Nexus for production usage you would add them into your logrotate setup so they are not getting bigger and bigger.
The Nexus user interface has no feature to purge the log.
A 1GB log file implies you have DEBUG logging enabled. First change back to INFO level logging of the ROOT logger using the Nexus UI, to keep the log file under control.
Automatic Log rotation and size limiting is also possible for Java Service Wrapper and Nexus logs. By default, Nexus ships with sensible log rotation defaults.
First decide which log is the problem. Then use the correct configuration to adjust this.
Java Service Wrapper - wrapper.log
http://wrapper.tanukisoftware.com/doc/english/props-logging.html
Nexus logs configured via logback and logback-nexus.xml/logback-overrides.xml
You may find this reference helpful:
https://support.sonatype.com/entries/25427167-Nexus-Logging-Guide

How can I see list of available events in Symfony2

How can I see all available events in Symfony2?
I found a command on google
php app\console container:debug --show-private
But it does not show all available events. Like event named "security.interactive_login" is not listed in it.
Is there a way to see available events?
Console command
You can run:
app/console debug:event-dispatcher
This will show you a detailed summary of every subscriber, in order of priority per event. Unfortunately this won't show you all possible events, since it's infeasible to query the container for any events that could be registered due to the inherently dynamic nature of the events system.
To understand events you'll need to refer to docs and code of each component and bundle.
Documentation is the best place to start
Symfony standard ships with a multitude of events. Each Symfony component and bundle may or may not define events — your best bet is to look at each component or bundle's documentation for references to events.
Some very common events can be found in the docs:
HTTP Kernel Events
Console Events
Form Events
Code analysis
I used PhpStorm to look for all subclasses of Symfony's base Event class (Symfony\Component\EventDispatcher\Event).
I generated an inheritance tree each child is a subclass of it's parent.
* note: prepend Symfony\Component\ to find the FQN
EventDispatcher\Event
EventDispatcher\GenericEvent
Console\Event\ConsoleEvent
Console\Event\ConsoleCommandEvent
Console\Event\ConsoleExceptionEvent
Console\Event\ConsoleTerminateEvent
Form\FormEvent
HttpKernel\Event\KernelEvent
HttpKernel\Event\FilterResponseEvent
HttpKernel\Event\FilterControllerEvent
HttpKernel\Event\FinishRequestEvent
HttpKernel\Event\GetResponseEvent
HttpKernel\Event\GetResponseForControllerResultEvent
HttpKernel\Event\GetResponseForExceptionEvent
HttpKernel\Event\PostResponseEvent
Security\Http\Event\SwitchUserEvent
Security\Http\Event\InteractiveLoginEvent
I make no claim that these are all public events you can/should hook into — this is just one way to programmatic examine 3rd party code and get a sense for potential idioms.
For instance I noticed that both the HttpKernel, Security, and Console components use namespaced constants to expose their keys, see:
Symfony\Component\HttpKernel\KernelEvents
Symfony\Component\Security\Http\SecurityEvents
Symfony\Component\Console\ConsoleEvents
The container:debug command shows all services that are registered to the dependency injection container. With the parameter show-private it will also show services that are flagged with public=false.
So as the most events might not be services the command you are using will not give you a list of available events. But to give you a possibility to search for available events you could try the following command:
php app/console container:debug --show-private | grep -i "listener"
As the most event handlers might have the word listener in their definitions you will find many of them. If you then want to get a more detailed information about the events which are handled by those listeners just call the command with specifying the service ID. For example if you are working with the FOSUserBundle this will give you a description for the interactive login listener:
php app/console container:debug fos_user.security.interactive_login_listener

Resources