fahl-design / log4php-slack-appender
A log4php appender build as wrapper for maknz/slack package
Installs: 2 337
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: ^7.1 || ^8.0
- ext-curl: *
- ext-json: *
- alek13/slack: ^1
- apache/log4php: ^2.3
Requires (Dev)
- codacy/coverage: 1.*
- codeception/codeception: ^4
- codeception/mockery-module: ^0
- codeception/module-asserts: ^1
- friendsofphp/php-cs-fixer: 3.*
- mockery/mockery: ^1.0
- monolog/monolog: 1.*
- roave/security-advisories: dev-latest
- symfony/var-dumper: ^4.0 || ^5.3
README
Status
Master Branch
Develop Branch
Description
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.
Requirements
PHP >= 7.1
Installation
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
XML appender config example
<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 (config.php) appender config example
<?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 ] ] ] ];
Usage
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); }