-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0ca023
commit 94e71e9
Showing
8 changed files
with
179 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Andiwijaya\AppCore\Facades; | ||
|
||
use Andiwijaya\AppCore\Models\ScheduledTask; | ||
use Andiwijaya\AppCore\Notifications\SlackNotification; | ||
use App\Notifications\LogToSlackNotification; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Notification; | ||
|
||
class Log{ | ||
|
||
private static $settings = []; | ||
|
||
public static function make($message, $type = 'info', $settings = null){ | ||
|
||
$detail = ''; | ||
if(is_array($message)){ | ||
$detail = $message[1] ?? ''; | ||
$message = $message[0] ?? ''; | ||
} | ||
|
||
$formattedMessage = implode("\t", [ | ||
"[" . Carbon::now()->format('Y-m-d H:i:s') . "]", | ||
$message | ||
]); | ||
|
||
if(isset(self::$settings['output']) || isset($settings['output'])){ | ||
$output = $settings['output'] ?? self::$settings['output']; | ||
if(is_callable([ $output, $type ])) | ||
call_user_func_array([ $output, $type ], [ $formattedMessage ]); | ||
} | ||
|
||
if(isset(self::$settings['path']) || isset($settings['path'])){ | ||
$path = $settings['path'] ?? self::$settings['path']; | ||
$fp = fopen(self::$settings['path'], 'a+'); | ||
if($fp){ | ||
fwrite($fp, $formattedMessage . "\n"); | ||
fclose($fp); | ||
} | ||
} | ||
|
||
if(isset(self::$settings['table']) || isset($settings['table'])){ | ||
$table = $settings['table'] ?? self::$settings['table']; | ||
DB::table($table)->insert([ | ||
'type'=>1, | ||
'data'=>json_encode([ 'type'=>$type, 'message'=>$message ]), | ||
'created_at'=>Carbon::now()->format('Y-m-d H:i:s') | ||
]); | ||
} | ||
|
||
if(isset(self::$settings['slack']) || isset($settings['slack'])){ | ||
$slack = $settings['slack'] ?? self::$settings['slack']; | ||
ScheduledTask::runOnce(function($result) use($slack, $type, $message, $detail){ | ||
Notification::route('slack', $slack) | ||
->notify(new SlackNotification($message, $type, $detail)); | ||
}); | ||
} | ||
} | ||
|
||
public static function error($message) | ||
{ | ||
return self::make($message, 'error'); | ||
} | ||
|
||
public static function warning($message) | ||
{ | ||
return self::make($message, 'warning'); | ||
} | ||
|
||
public static function info($message) | ||
{ | ||
return self::make($message); | ||
} | ||
|
||
public static function set(array $settings, $overwrite = false){ | ||
|
||
foreach($settings as $key=>$value){ | ||
switch($key){ | ||
|
||
case 'path': | ||
case 'table': | ||
case 'output': | ||
case 'slack': | ||
if(!isset(self::$settings[$key]) || $overwrite) | ||
self::$settings[$key] = $value; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Andiwijaya\AppCore\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Messages\SlackMessage; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
|
||
class SlackNotification extends Notification | ||
{ | ||
use Queueable; | ||
|
||
protected $message; | ||
protected $type; | ||
protected $detail; | ||
|
||
public function __construct($message, $type = 'info', $detail = '') | ||
{ | ||
$this->message = $message; | ||
$this->type = $type; | ||
$this->detail = $detail; | ||
} | ||
|
||
public function via($notifiable) | ||
{ | ||
return ['slack']; | ||
} | ||
|
||
public function toSlack($notifiable) | ||
{ | ||
$message = (new SlackMessage) | ||
->content($this->message); | ||
|
||
switch($this->type){ | ||
case 'info': $message->info(); break; | ||
case 'error': $message->error(); break; | ||
case 'warning': $message->warning(); break; | ||
} | ||
|
||
if($this->detail) | ||
$message | ||
->attachment(function($attachment){ | ||
$attachment->title('Detail') | ||
->content($this->detail); | ||
}); | ||
|
||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters