# Logging

# Content

# Tracy

First of all, we need to register our universal tuned logger for the future purpose.

extensions:
    logging: Contributte\Logging\DI\TracyLoggingExtension
1
2

After that, we need to setup logDir.

logging:
    logDir: %appDir%/../log
1
2

Basically, it overrides Tracy's default logger by our universal, pluggable logger.

Original logger is still in DIC with logging.originalLogger key.

# Default loggers

There are 3 types of loggers defined by default.

  • FileLogger - creates <priority>.log file
  • BlueScreenFileLogger - creates exception-*.html from all throwable
  • SendMailLogger - sends throwable/message to email

You can redefine these loggers in logging.loggers.

logging:
    loggers: 
        - Contributte\Logging\FileLogger(%logDir%)
        - Contributte\Logging\BlueScreenFileLogger(%logDir%)
        - Contributte\Logging\SendMailLogger(
            Contributte\Logging\Mailer\TracyMailer(
                from@email,
                [to@email, to2@email]
            ),
            %logDir%
        )
        - App\Model\MyCustomerLogger
1
2
3
4
5
6
7
8
9
10
11
12

This configuration is functionally equal to original Tracy's logger, only separated to multiple classes.

# SendMailLogger

Our SendMailLogger also allows configure priority levels.

services: 
    sendMaillogger:
        setup: 
            - setAllowedPriority([
                Contributte\Logging\ILogger::WARNING,
                Contributte\Logging\ILogger::ERROR
            ])
1
2
3
4
5
6
7

# Custom logger

To create your custom logger you have to implement Contributte\Logging\ILogger.

<?php

namespace App\Model;

use Contributte\Logging\ILogger;

class MyDatabaseLogger implements ILogger
{

    /**
     * @param mixed $message
     * @return void
     */
    public function log($message, string $priority = self::INFO): void
    {
        // store exception to database...
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

And register it in neon.

logging:
    loggers: 
        - App\Model\MyDatabaseLogger(@connection)
1
2
3

# Slack

extensions:
    logging: Contributte\Logging\DI\TracyLoggingExtension
    slack: Contributte\Logging\DI\SlackLoggingExtension
1
2
3

There is a configuration you have to fill in.

Key Requirements Default
url required -
channel required -
username optional Tracy
icon_emoji optional 🚀
icon_url optional -
slack:
    url: https://hooks.slack.com/services/<code1>/<code2>/<code3>
    channel: tracy
1
2
3

# Formatters

By default, there are 5 formatters for your slack-channel-pleasure.

You can disable it like this:

slack:
    formatters: []
1
2

And configure your own formatters. They will be loaded automatically, if you implement needed interface (Contributte\Logging\Slack\Formatter\IFormatter).

services:
    - App\Slack\MySuperTrouperFormatter
1
2

# Contributte\Logging\Slack\Formatter\ContextFormatter

  • Setup context with all configured data (channel, icon, etc).

# Contributte\Logging\Slack\Formatter\ColorFormatter

  • danger -> ILogger::CRITICAL
  • #ff0000 -> ILogger::EXCEPTION
  • warning -> ILogger::ERROR

# Contributte\Logging\Slack\Formatter\ExceptionFormatter

ContextFormatter

# Contributte\Logging\Slack\Formatter\ExceptionPreviousExceptionsFormatter

ContextFormatter

# Contributte\Logging\Slack\Formatter\ExceptionStackTraceFormatter

ContextFormatter

# Sentry

extensions:
    logging: Contributte\Logging\DI\TracyLoggingExtension
    sentry: Contributte\Logging\DI\SentryLoggingExtension
1
2
3

This extension requires to have sentry installed.

composer require sentry/sentry:"^1.0"
1

Now you should go to project Settings page -> Client Keys (DSN) section. There you obtained DNS url. Put the url into neon file.

sentry:
    url: https://<key>@sentry.io/<project>
1
2

SentryLoggingExtension adds SentryLogger with url configuration. It works as SendMailLogger.

It means that it sends messages/throwable with ILogger::ERROR, ILogger::EXCEPTION, ILogger::CRITICAL priorities.

But if you need other priorities, you can change configuration.

services: 
    sentry.logger:
        setup: 
            - setAllowedPriority(
                Contributte\Logging\ILogger::WARNING,
                Contributte\Logging\ILogger::ERROR
            )
1
2
3
4
5
6
7