Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applied revision comments #7

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
composer.lock
vendor
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
# nette-slack-logger
Log your errors directly into Slack room
Log your errors directly into a Slack room

## Installation

`composer require greeny/nette-slack-logger`
`composer require ondrej-bouda/nette-slack-logger`

And register extension to your config.neon:

```yaml
extensions:
slackLogger: greeny\NetteSlackLogger\DI\SlackLoggerExtension
slackLogger: OndrejBouda\NetteSlackLogger\DI\SlackLoggerExtension
```

By default the logger is just turned off, since you probably do not want to log errors from dev environment. If you want to enable it, add following lines to config.local.neon at your production server:
By default the logger is just turned off, since you probably do not want to log errors from dev environment. If you want
to enable it, add following lines to config.local.neon at your production server:

```yaml
slackLogger:
enabled: true
slackUrl: https://hooks.slack.com/services/XXX
logUrl: http://path/to/your/logs/directory/__FILE__
enabled: true
slackUrl: https://hooks.slack.com/services/XXX
logUrl: http://path/to/your/logs/directory/__FILE__
channel: "#somechannel"
username: "PHP Bot"
icon: ":joystick:"
pretext: "Error at example.com"
```

Of course replace `slackUrl` with payload URL from your incomming webhook from Slack.

You can leave `logUrl` empty, but if you have your logs accessible through web (of course e.g. protected by HTTP auth or available only from company IPs), you can define this URL here. `__FILE__` will be replaced by filename of file with exception.
Details:
- `slackUrl` must contain your incoming webhook URL - see https://api.slack.com/incoming-webhooks.
- `logUrl`, if specified, tells the URL at which the log file will be available. The substring `__FILE__` within the URL
will be replaced with the actual log file basename. The resulting URL gets appended to the message posted to Slack.
Note the file should not be available for public as it contains sensitive information. It is your responsibility to
protect the file, e.g., by HTTP auth or restricting access by IP addresses.
- `channel`: Name or ID of channel to post to. If not specified, the message gets posted to the default channel
according to the incoming webhook specification.
- `username`: Username to use for the post. Optional.
- `icon`: Icon to use besides the post instead of the default icon. Optional.
- `pretext`: Pretext for the message. Useful for distinguishing, e.g., the site.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "greeny/nette-slack-logger",
"description": "Log your error messages directly into Slack room",
"name": "ondrej-bouda/nette-slack-logger",
"description": "Log your error messages directly into a Slack room",
"license": "MIT",
"keywords": ["Nette", "Slack", "logger"],
"autoload": {
"psr-4": {
"greeny\\NetteSlackLogger\\": "src/"
"OndrejBouda\\NetteSlackLogger\\": "src/"
}
},
"require": {
"php": "^7.0",
"tracy/tracy": "^2.3"
}
}
66 changes: 46 additions & 20 deletions src/DI/SlackLoggerExtension.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
<?php
/**
* @author Tomáš Blatný
* @author Ondřej Bouda <[email protected]>
*/

namespace greeny\NetteSlackLogger\DI;
namespace OndrejBouda\NetteSlackLogger\DI;

use greeny\NetteSlackLogger\SlackLogger;
use OndrejBouda\NetteSlackLogger\SlackLogger;
use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Utils\AssertionException;
use Nette\Utils\Validators;
use Tracy\Debugger;


class SlackLoggerExtension extends CompilerExtension
{
private $defaults = [
'enabled' => false,
'logUrl' => null,
'channel' => null,
'username' => null,
'icon' => null,
'pretext' => null,
'interval' => null,
'file' => null,
'showIntervalWarning' => false,
];


private $defaults = [
'enabled' => FALSE,
];
public function afterCompile(ClassType $class)
{
$config = $this->getConfig($this->defaults);

Validators::assertField($config, 'enabled', 'boolean');

public function afterCompile(ClassType $class)
{
$config = $this->getConfig($this->defaults);

Validators::assertField($config, 'enabled', 'boolean');

if ($config['enabled']) {
Validators::assertField($config, 'slackUrl', 'string');
Validators::assertField($config, 'logUrl', 'string');

$init = $class->getMethod('initialize');
$init->addBody(Debugger::class . '::setLogger(new ' . SlackLogger::class . '(?, ?));', [$config['slackUrl'], $config['logUrl']]);
}
}

if ($config['enabled']) {
Validators::assertField($config, 'slackUrl', 'string');
Validators::assertField($config, 'logUrl', 'string|null');
Validators::assertField($config, 'channel', 'string|null');
Validators::assertField($config, 'username', 'string|null');
Validators::assertField($config, 'icon', 'string|null');
Validators::assertField($config, 'pretext', 'string|null');
Validators::assertField($config, 'file', 'string|null');
Validators::assertField($config, 'interval', 'int|string|null');
Validators::assertField($config, 'showIntervalWarning', 'boolean|null');

$init = $class->getMethod('initialize');
$init->addBody('?::setLogger(new ?(?, ?, ?, ?, ?, ?, ?, ?, ?));', [
new PhpLiteral(Debugger::class),
new PhpLiteral(SlackLogger::class),
$config['slackUrl'],
$config['logUrl'],
$config['channel'],
$config['username'],
$config['icon'],
$config['pretext'],
$config['file'],
$config['interval'],
$config['showIntervalWarning'],
]);
}
}
}
Loading