Skip to content

Commit

Permalink
Merge pull request #1 from cr0wst/initial-implementation
Browse files Browse the repository at this point in the history
Initial implementation
  • Loading branch information
cr0wst committed Nov 5, 2017
2 parents 72e39ca + e5409c9 commit b536ead
Show file tree
Hide file tree
Showing 20 changed files with 749 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/node_modules
/public/hot
/public/storage
/storage/*.key
/vendor
/.idea
/.vagrant
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.env
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Laravel Slack Log

[![Latest Stable Version](https://poser.pugx.org/smcrow/laravel-slack-log/v/stable)](https://packagist.org/packages/smcrow/laravel-slack-log) [![Latest Unstable Version](https://poser.pugx.org/smcrow/laravel-slack-log/v/unstable)](https://packagist.org/packages/smcrow/laravel-slack-log) [![Total Downloads](https://poser.pugx.org/smcrow/laravel-slack-log/downloads)](https://packagist.org/packages/smcrow/laravel-slack-log) [![Build Status](https://travis-ci.org/cr0wst/laravel-slack-log.svg?branch=master)](https://travis-ci.org/cr0wst/laravel-slack-log)

This package utilizes Laravel's native ability to create slack notifications to log at different log levels. This was inspired by Log4j.

## Log Levels
There are several different log levels that can be used. The desired level of logging is defined in the config.

Here are some example uses of the different log levels:
* `ERROR` - Indicates that an error occured.
* `WARN` - Indicates that an error occured but it was recoverable.
* `INFO` - Informational message usually explaining something in a *business rule* sense.
* `TRACE` - Used for reporting where, in the code, the execution is taking place. Could report entering and leaving functions.
* `DEBUG` - Used for dumping the contents of a variable.

Each message is implemented in the same way. The log levels are for you, the developer, to decide how to use.

The configured log level is checked before the message is sent. Levels are inclusive in that a specific level will include all message levels above it. For example, `INFO` will include `INFO`, `WARN`, and `ERROR`.

In some cases, it can be non-trivial to generate the message string. Logging guards have been provided to assist with this.

# Installation Steps
## Install Through Composer
```
composer require smcrow/laravel-slack-log
```

## Register the Service Provider

### Laravel 5.5
Laravel 5.5 allows for the auto-discovery of service providers. The `SlackLogServiceProvider` will automatically be discovered.

### Pre Laravel 5.5
You'll need to register the command in order for it to be usable. Modify the `register` method of `AppServiceProvider`:
```php
public function register()
{
$this->app->register(SlackLogServiceProvider::class);
}
```

## Registering the Facade
You can register the optional facade by adding the following to your `app.php` aliases:
```php
'SlackLog' => \Smcrow\SlackLog\Facades\SlackLog::class
```

## Configuration
Use `php artisan vendor:publish` to create a new configuration file. The file will be `config/slack-log.php`.

In this file you will need to provide the webhook url. I recommend reading [Slack Incoming Webhooks](https://my.slack.com/services/new/incoming-webhook/) for more information on how to setup the webhook.

# Example Usage
With logging guards and using the facade:

```php
$user = ['name' => 'John', 'age' => 65];

if (SlackLog::isDebugEnabled()) {
SlackLog::debug('User: ' . print_r($user, true));
}
```
Produces the following:

![Debug Example](example.png)
# Feedback and Contributions
Please feel free to offer suggestions by submitting an Issue. Alternatively, submit a pull request with any features you wish to add. This is a work-in-progress, and I would welcome any and all feedback.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "smcrow/laravel-slack-log",
"description": "Utilizes Laravel's notifications to provide logging to slack at various levels. Inspired by Log4j.",
"minimum-stability": "stable",
"keywords": ["laravel", "slack", "notifications", "logging"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Steve Crow",
"email": "steve@smcrow.net"
}
],
"require": {
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "~5.0"
},
"require-dev": {
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~6.0",
"squizlabs/PHP_CodeSniffer": "^3.1"
},
"autoload": {
"psr-4": {
"Smcrow\\SlackLog\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Smcrow\\SlackLog\\SlackLogServiceProvider"
]
}
}
}
249 changes: 249 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added example.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/Constants/LogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Smcrow\SlackLog\Constants;

class LogLevel
{
/**
* The various log levels for the application. Setting a level will allow all lower levels to become enabled.
*
* Example: A log level of 0 will disable logging entirely, while a log level of 2 will enable ERROR, WARN,
* and TRACE.
*/
public const NONE = 0;
public const ERROR = 1;
public const WARN = 2;
public const INFO = 3;
public const TRACE = 4;
public const DEBUG = 5;
}

0 comments on commit b536ead

Please sign in to comment.