File: /home/dnlightw-124/dn.lightweb.kr/app/Services/Logger.php
<?php
namespace App\Services;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
class Logger
{
public string $sectionTitle;
public Carbon $startTime;
public bool $doLogging;
public string $logChannel;
public function __construct($sectionTitle, $channel = 'load_timer', $doLogging = null)
{
$this->sectionTitle = $sectionTitle;
$this->startTime = Carbon::now();
$this->doLogging = $doLogging ?? config('app.debug');
$this->logChannel = $channel;
Config::set('logging.channels.'.$channel,[
'driver' => 'daily',
'path' => storage_path('logs/'.$channel.'.log'),
'level' => 'debug',
'days' => 2,
'replace_placeholders' => true,
]);
if($this->doLogging) Log::channel($this->logChannel)->alert("Starting $sectionTitle");
}
public function getPrefix(): string
{
return "LOG ID : " .$this->startTime->timestamp . " | " . $this->sectionTitle . " |";
}
public function log($message = ''): void
{
if($this->doLogging) Log::channel($this->logChannel)->info($this->getPrefix() . " +" . $this->startTime->diffInSeconds(Carbon::now()) . " seconds" . ($message ? " | $message" : ''));
}
public function json($object): void
{
if ($this->doLogging) {
$jsonObject = json_encode($object, JSON_PRETTY_PRINT);
Log::channel($this->logChannel)->debug($this->getPrefix() . " JSON Object: " . $jsonObject);
}
}
}