Coder Social home page Coder Social logo

monolog-stackdriver's Introduction

MonologStackdriver

This package enables you to push your Monolog log entries to Stackdriver which is part of the Google Cloud Platform.

The supplied StackdriverHandler copies the given log level into the Stackdriver's severity based on your log method.

It also respects the context argument which allows you to send extra contextual data with your log message. This will be stored in the log message under jsonPayload.data.


Configuration

Service account

With our samples we assume you have a service account Google Developers Console JSON key file available within your project to point at with read rights.

If you don't have this file yet, you can create it via Google Cloud Platform - IAM & Admin - Service accounts. Please make sure you have at least the role of Logs writer enabled.

Google\Cloud\Logging\LoggingClient options

Please read the documentation for the Google\Cloud\Logging\LoggingClient for other authentication options and further specific connection and setup.

Google\Cloud\Logging\Logger options

Please read the documentation for the Google\Cloud\Logging\Logger setup via Google\Cloud\Logging\LoggingClient for specific details about these options.

This set of options will allow you to set the default resource type and it's related labels that apply to all the logs. Please read Method: monitoredResourceDescriptors.list and do the "Try this API" to get a full list of the specific labels per resource.

Google\Cloud\Logging\Entry options

Please read the documentation for the Google\Cloud\Logging\Entry setup via Google\Cloud\Logging\Logger for specific details about these options.

By default, you can add Stackdriver specific log entry options by adding these wrapped in the stackdriver-key inside the context array. Very useful to add log entry specific labels for instance.

$context['stackdriver'] = [
    // stackdriver related entry options
];

If you need to, you can override this key name by setting $entryOptionsWrapper to your own value (string) when using StackdriverHandler::__construct.

It is also possible to set path to credentials and project id via the global constant.

define('GOOGLE_APPLICATION_CREDENTIALS', '/path/to/service-account-key-file.json'); 
define('GOOGLE_CLOUD_PROJECT', 'eg-my-project-id-148223');

Pick your framework for some specific setup

Vanilla usage

use Monolog\Logger;
use CodeInternetApplications\MonologStackdriver\StackdriverHandler;

// ( ... )

// GCP Project ID
$projectId = 'eg-my-project-id-148223';

// See Google\Cloud\Logging\LoggingClient::__construct
$loggingClientOptions = [
    'keyFilePath' => '/path/to/service-account-key-file.json'
];

// init handler
$stackdriverHandler = new StackdriverHandler(
    $projectId,
    $loggingClientOptions
);

// init logger with StackdriverHandler
$logger = new Logger('stackdriver', [$stackdriverHandler]);

// basic info log with contextual data
$logger->info('New order', ['orderId' => 1001]);
// ( ... )

// add specific log entry options, eg labels
$context = ['orderId' => 1001];

// add a 'stackdriver' entry to the context to append
// log entry specific options
$context['stackdriver'] = [
    'labels' => [
        'action' => 'paid'
    ]
];
$logger->info('Order update', $context);
// ( ... )

// add specific log entry options, eg labels and operation
$context = ['orderId' => 1001];
$context['stackdriver'] = [
    'labels' => [
        'order' => 'draft'
    ],
    'operation' => [
        'id' => 'order-1001',
        'first' => true,
        'last' => false
    ]
];
$logger->info('Order update', $context);

// update both label and operation
$context['stackdriver'] = [
    'labels' => [
        'order' => 'paid'
    ],
    'operation' => [
        'id' => 'order-1001',
        'first' => false,
        'last' => false
    ]
];
$logger->info('Order update', $context);

// update both label and operation again
$context['stackdriver'] = [
    'labels' => [
        'order' => 'fulfilled'
    ],
    'operation' => [
        'id' => 'order-1001',
        'first' => false,
        'last' => true
    ]
];
$logger->info('Order update', $context);

monolog-stackdriver's People

Contributors

martinatcode avatar codeinternetapplications avatar ivanbooxi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.