Customizing Monolog HTML formatter for email

Monolog can send emails when there’s an error, but in the provided HTML formatter I missed the REQUEST_URI and REMOTE_ADDR that triggered the error.

Some info is in the exceptions section, but not always and needs horizontal scrolling in the email client 🙂

So I just replicated the HtmlFormatter and added some lines (dirty hacks).

Added service class

 <service id="app.monolog.formatter.html" class="App\Service\MonologEmailFormatter" public="false" />

Change formatter service ID

# app/config/config_prod.yml
swift:
    type: swift_mailer
    from_email: "php@example.com"
    to_email: ["info@example.com"]
    subject: 'Error'
    level: error
    formatter: custom.monolog.formatter.html
    content_type: text/html

UPDATE: You might be interested in https://stefanoalletti.wordpress.com/2017/03/09/symfony-and-monolog-how-use-processor-in-your-project/

Advertisements

Hack Symfony2 database password for admin

Had to get in the admin of a Symfony2 version 2.6 app and I had only code and database access.
I just added some rows to the loginAction() method to get the salt and password for my own plaintext password.

public function loginAction(Request $request) {
    $user = new User();
    $plainPassword = 'YOUR_pass_HERE';
    $encoder = $this->container->get('security.password_encoder');
    echo 'Salt:<br>'.$user->getSalt();
    echo '<br>Password:<br>'.$encoder->encodePassword($user, $plainPassword);
    exit;

    // loginAction code continues ....

 

Symfony2 password change
Salt and encoded password for user

Changed the values in the database and login was successful.
You could also flush and persist the new user if you would like, but username needs to be set.
For more help visit the awesome docs page at http://symfony.com/doc/2.6/components/security/authentication.html#using-password-encoders