PHP code example of kirubha7 / cloudwatch-logs-laravel

1. Go to this page and download the library: Download kirubha7/cloudwatch-logs-laravel 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/ */

    

kirubha7 / cloudwatch-logs-laravel example snippets




use kirubha7\CloudwatchLogsLaravel\Cloudwatchlogs;
use Aws\CloudWatchLogs\CloudWatchLogsClient;

$client = [
    'region' => 'AWS_REGION',
    'version' => '2014-03-28',
    'credentials' => [
        'key' => 'AWS_KEY',
        'secret' => 'AWS_SECRET',
    ]
];
$client  = new CloudWatchLogsClient($client);//Intialize AWS Clinet
$logs = new Cloudwatchlogs($client);
try{

	//YOUR CONTEXT 
    $datas = [
        'test' => 'package',
        'version' => 1,
    ];
    $retentionDays = 30;//By Default retention days upto 14 days

    /**
      Generally putLog function returns associate array
      If Logs sends successfully => ['status' => true,'message' => 'Log sended successfully']
      If Logs failed to sends => ['status' => false,'error' => 'EXCEPTION_MESSAGE']
    */
    $res = $logs->putLog('YOUR_LOGGROUP_NAME','YOUR_LOGSTREAM_NAME',$retentionDays,$datas);

    if($res['status']){//If log sends successfully
        echo $res['message'];
    }else{//If log fails to sends
        echo $res['error'];
    }
    
}catch(\Exception $e){
    echo $e->getMessage();
}


    /**
      Generally checkLogGroupExists function returns associate array
      If Logs group exists => ['status' => true,'message' => 'Log Group Exists']
      If Logs group not exists => ['status' => false,'error' => 'EXCEPTION_MESSAGE']
    */

    $res = $logs->checkLogGroupExists('YOUR_LOG_GROUP_NAME');

    if($res['status']){//If log group is exists
        echo $res['message'];
    }else{//If log group not exists
        echo $res['error'];
    }


    /**
      Generally checkLogStreamExists function returns associate array
      If Logs stream exists => ['status' => true,'message' => 'Log Stream Exists']
      If Logs stream not exists => ['status' => false,'error' => 'EXCEPTION_MESSAGE']
    */
    
    $res = $logs->checkLogStreamExists('YOUR_LOG_STREAM_NAME');

    if($res['status']){//If log stream is exists
        echo $res['message'];
    }else{//If log stream not exists
        echo $res['error'];
    }


    /**
      Generally createLogGroup function returns associate array
      If Logs group created successfully => ['status' => true,'message' => 'Log Group Created Successfully']
      If Logs group fails to create  => ['status' => false,'error' => 'EXCEPTION_MESSAGE']
    */

    $res = $logs->createLogGroup('YOUR_LOG_GROUP_NAME');

    if($res['status']){//If log group created successfully
        echo $res['message'];
    }else{//If log group fails to create
        echo $res['error'];
    }


     /**
      Generally createLogStream function returns associate array
      If Logs stream created successfully => ['status' => true,'message' => 'Log Stream Created Successfully']
      If Logs stream fails to create  => ['status' => false,'error' => 'EXCEPTION_MESSAGE']
    */

    $res = $logs->createLogStream('YOUR_LOG_GROUP_NAME','YOUR_LOG_STREAM_NAME');

    if($res['status']){//If log stream created successfully
        echo $res['message'];
    }else{//If log stream fails to create
        echo $res['error'];
    }
json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:DescribeLogGroups"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:DescribeLogStreams",
                "logs:PutRetentionPolicy"
            ],
            "Resource": "{LOG_GROUP_ARN}"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:PutLogEvents"
            ],
            "Resource": [
                "{LOG_STREAM_1_ARN}",
                "{LOG_STREAM_2_ARN}"
            ]
        }
    ]
}