<commit_before><?php

namespace Matchingood\SQSLogger;

use Illuminate\Http\Request;
use Auth;
use Log;
use Aws\Sqs\SqsClient;

class SQSLogger
{
    private $sqs;
    private $url;

    public function __construct()
    {
        if (config('sqslogger.env') === 'prod') {
            $this->sqs = new SqsClient([
                'credentials' => [
                    'key' => config('sqslogger.aws.access_key'),
                    'secret' => config('sqslogger.aws.access_secret')
                ],
                'version' => config('sqslogger.aws.sqs.version'),
                'region' => config('sqslogger.aws.sqs.region')
            ]);
            $obj = $this->sqs->getQueueUrl([
                'QueueName' => config('sqslogger.aws.sqs.queue_name')
            ]);
            $this->url = $obj['QueueUrl'];
        } else {
            $this->sqs = null;
            $this->url = '';
        }
    }

    public function access(Request $request, $extraInfo = null)
    {
        $info = ['method' => $request->method(), 'accessUrl' => $request->url()];

        if (!is_null($extrainfo)) {
            $info += $extraInfo;
        }

        $body = $this->createBody('ACCESS', "", $info);

        $this->sendMessage([
            'MessageBody' => $body,
            'QueueUrl' => $this->url
        ]);
    }

    public function error($message, $extraInfo = null)
    {
        $this->sendMessage([
            'MessageBody' => $this->createBody('ERROR', $message, $extraInfo),
            'QueueUrl' => $this->url
        ], 'ERROR');
    }

    public function info($message, $extraInfo = null)
    {
        $this->sendMessage([
            'MessageBody' => $this->createBody('INFO', $message, $extraInfo),
            'QueueUrl' => $this->url
        ], 'INFO');
    }

    private function sendMessage($data, $level = 'debug', $extraInfo = null)
    {
        if (env('APP_ENV') === 'prod') {
            $this->sqs->sendMessage($data, $extraInfo);
        } elseif ($level === 'ERROR') {
            Log::error($data['MessageBody']);
        } elseif ($level === 'INFO') {
            Log::info($data['MessageBody']);
        } else {
            Log::debug($data['MessageBody']);
        }
    }

    private function createBody($level, $message, $extraInfo = null)
    {
        $id = null;
        if (Auth::check()) {
            $id = auth()->user()->id;
        }

        $bt = debug_backtrace();

        $body = [
            'level' => $level,
            'time' => date('Y-m-d H:i:s'),
            'userId' => $id,
            'file' => $bt[1]['file'],
            'line' => $bt[1]['line'],
            'message' => $message
        ];

        if (!is_null($extraInfo)) {
            $body += $extraInfo;
        }

        return json_encode($body, JSON_UNESCAPED_SLASHES);
    }
}
<commit_msg>add CRITICAL level<commit_after><?php

namespace Matchingood\SQSLogger;

use Illuminate\Http\Request;
use Auth;
use Log;
use Aws\Sqs\SqsClient;

class SQSLogger
{
    private $sqs;
    private $url;

    public function __construct()
    {
        if (config('sqslogger.env') === 'prod') {
            $this->sqs = new SqsClient([
                'credentials' => [
                    'key' => config('sqslogger.aws.access_key'),
                    'secret' => config('sqslogger.aws.access_secret')
                ],
                'version' => config('sqslogger.aws.sqs.version'),
                'region' => config('sqslogger.aws.sqs.region')
            ]);
            $obj = $this->sqs->getQueueUrl([
                'QueueName' => config('sqslogger.aws.sqs.queue_name')
            ]);
            $this->url = $obj['QueueUrl'];
        } else {
            $this->sqs = null;
            $this->url = '';
        }
    }

    public function access(Request $request, $extraInfo = null)
    {
        $info = ['method' => $request->method(), 'accessUrl' => $request->url()];

        if (!is_null($extrainfo)) {
            $info += $extraInfo;
        }

        $body = $this->createBody('ACCESS', "", $info);

        $this->sendMessage([
            'MessageBody' => $body,
            'QueueUrl' => $this->url
        ]);
    }

    public function error($message, $extraInfo = null)
    {
        $this->sendMessage([
            'MessageBody' => $this->createBody('ERROR', $message, $extraInfo),
            'QueueUrl' => $this->url
        ], 'ERROR');
    }

    public function info($message, $extraInfo = null)
    {
        $this->sendMessage([
            'MessageBody' => $this->createBody('INFO', $message, $extraInfo),
            'QueueUrl' => $this->url
        ], 'INFO');
    }

    public function critical($message, $extraInfo = null)
    {
        $this->sendMessage([
            'MessageBody' => $this->createBody('CRITICAL', $message, $extraInfo),
            'QueueUrl' => $this->url
        ], 'CRITICAL');
    }

    private function sendMessage($data, $level = 'debug', $extraInfo = null)
    {
        if (env('APP_ENV') === 'prod') {
            $this->sqs->sendMessage($data, $extraInfo);
        } elseif ($level === 'ERROR') {
            Log::error($data['MessageBody']);
        } elseif ($level === 'INFO') {
            Log::info($data['MessageBody']);
        } else {
            Log::debug($data['MessageBody']);
        }
    }

    private function createBody($level, $message, $extraInfo = null)
    {
        $id = null;
        if (Auth::check()) {
            $id = auth()->user()->id;
        }

        $bt = debug_backtrace();

        $body = [
            'level' => $level,
            'time' => date('Y-m-d H:i:s'),
            'userId' => $id,
            'file' => $bt[1]['file'],
            'line' => $bt[1]['line'],
            'message' => $message
        ];

        if (!is_null($extraInfo)) {
            $body += $extraInfo;
        }

        return json_encode($body, JSON_UNESCAPED_SLASHES);
    }
}

{'fraction_non_alphanumeric': 0.18104906937394247, 'fraction_numerical': 0.0006768189509306261, 'mean_word_length': 1.7829566854990584, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2438056', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1789, 'n_words': 317}