Skip to content

Instantly share code, notes, and snippets.

@plencovich
Last active November 29, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plencovich/5cbf9c544be03b7bcca3ace0f21d3734 to your computer and use it in GitHub Desktop.
Save plencovich/5cbf9c544be03b7bcca3ace0f21d3734 to your computer and use it in GitHub Desktop.
Google Cloud Logging / Stackdriver Logs / Codeigniter / PHP -> by Plen.co
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// Libreria Cloud Log para Codeigniter
use Google\Cloud\Logging\LoggingClient;
class Cloudlog
{
private $projectId;
private $logging;
public function __get($var)
{
return get_instance()->$var;
}
public function __construct() {
$this->projectId = 'project-id-google';
$this->logging = new LoggingClient([
'keyFile' => json_decode(file_get_contents('./key/gcloud.json'), true),
'projectId' => $this->projectId
]);
}
function write_log($loggerName, $message,$severity)
{
$logger = $this->logging->logger($loggerName, [
'resource' => [
'type' => 'global'
]
]);
$logger->write(['message' => $message],['severity' => $severity]);
}
function delete_logger($loggerName)
{
$logger = $this->logging->logger($loggerName);
$logger->delete();
printf("Deleted a logger '%s'." . PHP_EOL, $loggerName);
}
}
{
"require": {
"google/cloud-logging": "^1.14"
}
}
<?php
// Carga libreria
$this->load->library('Cloudlog');
// Escribe LOG
$this->cloudlog->write_log('logger_name','message_text',500); // Code severity https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
// Borra LOG
$this->cloudlog->delete_logger('logger_name');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment