This package allows you to use Slack for PHP easily and elegantly in your app as an log4php appender. Read the instructions below to get it set up.
PHP >= 7.1
You can install the package using the Composer package manager. You can install it by running this command in your project root:
composer require fahl-design/log4php-slack-appender
Then create an incoming webhook for each Slack team you'd like to send messages to. You'll need the webhook URL(s) in order to configure this package.
After you got your hook url add it as endpoint to your configuration
<log4php:configuration xmlns:log4php="http://logging.apache.org/log4php/" threshold="all">
<appender name="appender_slack" class="LoggerAppenderSlack">
<!-- get endpoint url from https://my.slack.com/services/new/incoming-webhook -->
<param name="endpoint" value="https://hooks.slack.com/services/XXXXXXXXXXXXXXX/XXXXXXXXXXXXXXX/XXXXXXXXXXXXXXX" />
<param name="channel" value="#yourChannel" />
<param name="username" value="Log4php" />
<!-- Url or emoji-->
<param name="icon" value=":do_not_litter:" />
<!-- flag to allow markdown (default 1) -->
<param name="allowMarkdown" value="1" />
<!-- flag to send log message as slack attachment (default 1) -->
<param name="asAttachment" value="1" />
</appender>
<logger name="myLogger">
<appender_ref ref="appender_slack" />
</logger>
</log4php:configuration>
<?php
declare(strict_types=1);
use WebProject\Log4php\Appender\Settings\Config;
use WebProject\Log4php\Appender\Slack;
return [
'rootLogger' => [
'level' => 'DEBUG',
'appenders' => ['slack_appender'],
],
'myLogger' => [
'appenders' => ['slack_appender'],
],
'appenders' => [
'slack_appender' => [
'class' => Slack::class,
'params' => [
Config::KEY_ENDPOINT => 'https://hooks.slack.com/services/XXXXXXXXXXXXXXX/XXXXXXXXXXXXXXX/XXXXXXXXXXXXXXX',
Config::KEY_CHANNEL => '#general',
Config::KEY_USERNAME => 'log4php',
Config::KEY_ICON => ':ghost:', // emoji or an icon url
Config::KEY_ALLOW_MARKDOWN => true,
Config::KEY_MARKDOWN_IN_ATTACHMENTS_FIELDS => [
Config::VALUE_MARKDOWN_IN_ATTACHMENTS_PRETEXT,
Config::VALUE_MARKDOWN_IN_ATTACHMENTS_TEXT,
Config::VALUE_MARKDOWN_IN_ATTACHMENTS_TITLE,
Config::VALUE_MARKDOWN_IN_ATTACHMENTS_FIELDS,
Config::VALUE_MARKDOWN_IN_ATTACHMENTS_FALLBACK
],
Config::KEY_AS_ATTACHMENT => true,
Config::KEY_LINK_NAMES => true,
Config::KEY_UNFURL_LINKS => false,
Config::KEY_UNFURL_MEDIA => true,
Config::KEY_SET_ICON_BY_LOG_LEVEL => true,
Config::KEY_ADD_LOGGER_TO_MESSAGE => true
]
]
]
];
Check example (src/examples)
<?php
declare(strict_types=1);
/**
* This script requires installation as composer package.
*/
require_once __DIR__.'/../../vendor/autoload.php';
try {
if (!\is_file(__DIR__.'/../resources/config.local.php')) {
throw new RuntimeException('local config file is missing');
}
Logger::configure(include __DIR__.'/../resources/config.local.php');
Logger::getRootLogger()->fatal('root-logger-fatal-message');
$logger = Logger::getLogger('myLogger');
$logger->warn('warn-message @channel *WATTT*');
$logger = Logger::getLogger('myLogger');
$logger->debug('debug-message');
$logger->info('info-message');
$logger->warn('warn-message @channel *WATTT*');
$logger->error('error-message');
$logger->fatal('fatal-message');
} catch (\Throwable $e) {
\print_r($e->getMessage());
\print_r($e->getTraceAsString());
exit(255);
}