PHP code example of kejaksaan-dev / portal-logger

1. Go to this page and download the library: Download kejaksaan-dev/portal-logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kejaksaan-dev / portal-logger example snippets


	// in `.env`

	PORTAL_LOGGER_URL={URL_LOGGING_SERVER}
	PORTAL_LOGGER_APP_ID={APPID_LOGGING_SERVER}
	PORTAL_LOGGER_APP_KEY={APPKEY_LOGGING_SERVER}
	PORTAL_LOGGER_REQUEST_TIMEOUT=10
	PORTAL_LARAVEL_VERSION=10 //if laravel 9 then value is 9
	
	

	// in `config/app.php`

	// ...

	'providers' => [
		//...
		KejaksaanDev\PortalLogger\PortalLoggerServiceProvider::class,
	]
	
	

	return [

		'logger' => [
			'url' => env('PORTAL_LOGGER_URL',
			'https://api.kejaksaanri.id/portal-logger'),
			'path' => env('PORTAL_LOGGER_PATH', '/api/v1/logs/create'),
			'app_id' => env('PORTAL_LOGGER_APP_ID', 'DEFAULT'),
			'default_status_code' => env('PORTAL_LOGGER_STATUS_CODE', '200'),
			'is_ajax_log' => env('PORTAL_LOGGER_IS_AJAX_LOG', false),
			'user_guard' => env('PORTAL_LOGGER_USER_GUARD', 'web'),
			'user_id' => env('PORTAL_LOGGER_USER_ID_VAR', 'id'),
			'user_satker_var' => env('PORTAL_LOGGER_USER_SATKER_VAR', 'satker_id'),
			'user_nip_var' => env('PORTAL_LOGGER_USER_NIP_VAR', 'nip'),
			'user_username_var' => env('PORTAL_LOGGER_USER_USERNAME_VAR', 'username'),
			'user_name_var' => env('PORTAL_LOGGER_USER_NAME_VAR', 'name'),
			'request_timeout' => env('PORTAL_LOGGER_REQUEST_TIMEOUT', 30),
			'request_async' => env('PORTAL_LOGGER_REQUEST_ASYNC', true),
			'cache_user' => env('PORTAL_LOGGER_CACHE_USER', 86400),
			'is_enable' => env('PORTAL_LOGGER_IS_ENABLE', true)
		],
		'http_logger' => [
			'channel' => env('PORTAL_HTTP_LOGGER_CHANNEL', 'portal-logger'),
			'level' => env('PORTAL_HTTP_LOGGER_LEVEL', 'info'),
			'log_profile' => \KejaksaanDev\PortalLogger\Logs\PortalLogProfile::class,
			'log_writer' => \KejaksaanDev\PortalLogger\Logs\PortalLogWriter::class,
		]
	];
	

	// in `app/Http/Kernel.php`

	protected $middlewareGroups = [
	    'web' => [
			    // ...
			    \KejaksaanDev\PortalLogger\Http\Middleware\PortalLogger::class
		]
	];
	

		// in `config/logging.php`
		return [
			'channels' => [
			
				// ...
				
				'portal-logger' => [
					'driver' => 'monolog',
					'level' => 'debug',
					'handler' => env('PORTAL_LARAVEL_VERSION', 10) >= 10 
							? KejaksaanDev\PortalLogger\PortalLoggerHandler::class 
							: KejaksaanDev\PortalLogger\PortalLoggerL10BellowHandler::class
				],
			]
		]
		

		// in `app\Exceptions\Handler.php`
		
		use Illuminate\Support\Facades\Log;
		use KejaksaanDev\PortalLogger\Http\Traits\WriterLogger;
		use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
		use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
		use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
		use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
		use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
		
		//...
		
		public  function  report(Throwable  $e)
		{
			try {
				$statusCode = 500;
				$className = get_class($e);

				if ($e instanceof BadRequestHttpException) {
					$statusCode = 400;
				} else  if ($e instanceof MethodNotAllowedHttpException) {
					$statusCode = 405;
				} else  if ($e instanceof NotFoundHttpException) {
					$statusCode = 404;
				} else  if ($e instanceof UnauthorizedHttpException) {
					$statusCode = 401;
				}

				WriterLogger::writeLog(request(), (string) $statusCode, 'error', null, $className, $e->getMessage());
			}
			catch (\Exception  $ex) {
				Log::error("Failed log exception handler cause : {$ex->getMessage()}");
			}
		}
		
bash
	php artisan vendor:publish --provider="KejaksaanDev\PortalLogger\PortalLoggerServiceProvider" --tag="portal-logger-config"