fahl-design/log4php-slack-appender

A log4php appender build as wrapper for maknz/slack package

0.2.0 2017-07-16 22:50 UTC

README

Status

Dependency Status Latest Stable Version Total Downloads Latest Unstable Version License

Monthly Downloads Daily Downloads

Code Climate Code Climate

PHP-Eye PHP-Eye

Master Branch

Build Status StyleCI codecov

Develop Branch

Build Status StyleCI codecov

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 >= 5.5 || >= 7

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 
    return [
        'rootLogger' => [
            'appenders' => ['default'],
        ],
        'myLogger' => [
            'appenders' => ['default'],
        ],
        'appenders' => [
            'default' => [
                'class' => 'LoggerAppenderSlack',
                'params' => [
                    'endpoint' => 'https://hooks.slack.com/services/XXXXXXXXXXXXXXX/XXXXXXXXXXXXXXX/XXXXXXXXXXXXXXX',
                    'channel' => '#yourChannel',
                    'username' => 'log4php',
                    'icon' => ':ghost:', // emoji or an icon url
                ]
            ]
        ]
    ];

Usage

Check example (src/examples)

require_once __DIR__.'/../../../vendor/autoload.php';

Logger::configure(__DIR__.'/../resources/appender_slack.xml');

$logger = Logger::getLogger('myLogger');
$logger->debug('debug-message');
$logger->info('info-message');
$logger->warn('warn-message');
$logger->error('error-message');
$logger->fatal('fatal-message');