Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Go: Using standard login
2.1.
Installing the package
2.2.
Using the Go logger
2.3.
Run on Google Cloud
2.4.
Run locally and elsewhere
2.5.
View the logs
3.
Node.js: Using Winston and Bunyan
3.1.
Using Bunyan
3.1.1.
Installing the plugin
3.1.2.
Configuring the plugin
3.1.3.
Using Bunyan and Express
3.2.
Using Winston
3.2.1.
Installing the plugin
3.2.2.
Configuring the plugin
4.
PHP: Using PSR-3 logger
4.1.
Creating a PSR-3 logger
4.2.
Enabling batching option
4.3.
Configuring the Cloud Logging library for PHP
4.4.
Using the PSR-3 logger
4.5.
Run on Google Cloud
4.6.
Run locally and elsewhere
4.7.
View the logs
5.
Python: Using the standard logging facility
5.1.
Installing the library
5.2.
Using the Python root logger
5.3.
Configuring the logging handler
5.4.
Run on Google Cloud
5.5.
Run locally and elsewhere
5.6.
View the logs
6.
Ruby: Using the Google Cloud’s operations suite gem
6.1.
Installing the library
6.2.
Enabling the library
6.3.
Using the Cloud Logging Logger
6.4.
Run on Google Cloud
6.5.
Run locally and elsewhere
6.6.
View the logs
7.
Frequently Asked Questions
7.1.
What is Google Cloud Platform?
7.2.
What are the GCP cloud storage libraries and tools?
7.3.
What is the pricing model in the GCP cloud?
8.
Conclusion
Last Updated: Mar 27, 2024

Advanced setups of Logging Libraries

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Google Cloud Platform is a public cloud vendor that provides a suite of computing services to do everything from delivering web and video over the web to AI and machine learning tools to data management. Customers can access computer resources housed in Google's data centers worldwide for free or on a pay-per-use basis.

Cloud logging allows you to manage log data from multiple cloud resources from a single location. Log data is critical for assessing and improving cloud performance and security. Let us study the details of each of these logging libraries in detail. 

Go: Using standard login

Installing the package

Use the following code:

go get cloud.google.com/go/logging

Using the Go logger

// Sample stdlogging writes log.Logger logs to the Cloud Logging.
package main
import (
        "context"
        "log"
        "cloud.google.com/go/logging"
)
func main() {
        ctx := context.Background()
        // Sets your Google Cloud Platform project ID.
        projectID := "YOUR_PROJECT_ID"
        // Creates a client.
        client, err := logging.NewClient(ctx, projectID)
        if err != nil {
                log.Fatalf("Failed to create client: %v", err)
        }
        defer client.Close()
        // Sets the name of the log to write to.
        logName := "my-log"
        logger := client.Logger(logName).StandardLogger(logging.Info)
        // Logs "hello world", log entry is visible at
        // Cloud Logs.
        logger.Println("hello world")
}

Run on Google Cloud

Using the Cloud Logging library for PHP should have the IAM Logs Writer role on Google Cloud. Most Google Cloud environments give this role by default.

Run locally and elsewhere

To use the Cloud Logging library for PHP outside of Google Cloud, including running the library on your workstation, on your data center's computers, or on the VM instances of another cloud provider, you must supply your appropriate service account credentials and Google Cloud project ID directly to the Cloud Logging library for PHP.

View the logs

After being deployed, you can view the logs in Logs Explorer.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Node.js: Using Winston and Bunyan

Using Bunyan

Bunyan is specialized in structured JSON logs. Log formatting can be performed by piping to the Bunyan CLI.

Installing the plugin

The most effortless method to install the Logging Bunyan plugin is with the help of npm:

npm install --save bunyan @google-cloud/logging-bunyan


Import the plugin and include it in your Bunyan configuration.

Configuring the plugin

You can customize the behavior of the Bunyn plugin using the same configuration options supported by the Cloud Logging API Cloud client library for Node.js. You can pass these options in the options object to the plugin's constructor.

Using Bunyan and Express

You can set up and use Bunyan with Logging in a Node.js Express application.

Using Winston

Winston is a general-purpose library implementing a variety of log formats and transports.

Installing the plugin

The most straightforward way to install the Logging Winston plugin is with npm

npm install --save @google-cloud/logging-winston winston


Import the plugin and add it to your Winston configuration.

Configuring the plugin

You can customize the behavior of the Winstonplugin using the same configuration options supported by the Cloud Logging API Cloud client library for Node.js. You can pass these options in the options object to the plugin's constructor.

PHP: Using PSR-3 logger

The Cloud Logging library for PHP gives an easy PSR-3 logger implementation for PHP web frameworks.

To write logs from the app, add the Cloud Logging library for PHP to the composer.json:

composer require google/cloud-logging

Creating a PSR-3 logger

Use the below code to create a PSR-3 logger:

use Google\Cloud\Logging\LoggingClient;
$logging = new LoggingClient([
    'projectId' => $projectId
]);
$logger = $logging->psrLogger('app');

Enabling batching option

The below code creates a PSR-3 logger which will batch multiple logs into a single RPC call:

$logger = LoggingClient::psrBatchLogger('app');

Configuring the Cloud Logging library for PHP

You can customize the behavior of the Cloud Logging library for PHP.

Using the PSR-3 logger

Once the logger is created, you can use the logger in your application:

$logger->info('Hello World');
$logger->error('Oh no');

Run on Google Cloud

Using the Cloud Logging library for PHP should have the IAM Logs Writer role on Google Cloud. Most Google Cloud environments give this role by default.

Run locally and elsewhere

To use the Cloud Logging library for PHP outside of Google Cloud, including running the library on your workstation, on your data center's computers, or on the VM instances of another cloud provider, you must supply your appropriate service account credentials and Google Cloud project ID directly to the Cloud Logging library for PHP.

View the logs

After being deployed, you can view the logs in Logs Explorer.

Python: Using the standard logging facility

Installing the library

To send all log entries to Cloud Logging by attaching the Cloud Logging handler to the Python root logger, use the setup_logging helper method:

# Imports the Cloud Logging client library
import google.cloud.logging
# Instantiates a client
client = google.cloud.logging.Client()
# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
client.setup_logging()

Using the Python root logger

After the handler is attached, any logs at INFO level or higher which are emitted in your application will be sent to Logging:

# Imports Python standard library logging
import logging
# The data to log
text = "Hello, world!"
# Emits the data using the standard logging module
logging.warning(text)

Configuring the logging handler

Go to the API library documentation to attach the Cloud Logging logging handler to only select Python loggers or to otherwise configure the logging handler.

Run on Google Cloud

Using the Cloud Logging library for PHP should have the IAM Logs Writer role on Google Cloud. Most Google Cloud environments give this role by default.

Run locally and elsewhere

To use the Cloud Logging library for PHP outside of Google Cloud, including running the library on your workstation, on your data center's computers, or on the VM instances of another cloud provider, you must supply your appropriate service account credentials and Google Cloud project ID directly to the Cloud Logging library for PHP.

View the logs

After being deployed, you can view the logs in Logs Explorer.

Ruby: Using the Google Cloud’s operations suite gem

Installing the library

We provide a Cloud Logging library for Ruby, which provides an easy-to-use logger implementation for Rack-based Ruby web frameworks:

Add the Google Cloud's operations suite gem to your Gemfile:

gem "stackdriver"

Use Bundler to install the gem:

bundle install

Enabling the library

If you're using Ruby on Rails, Bundler automatically loads the library into your application when it starts, which sets the default Rails.logger to an instance of Cloud Logging logger.

require "google/cloud/logging"
use Google::Cloud::Logging::Middleware

Using the Cloud Logging Logger

Once the Cloud Logging library for Ruby is enabled, you can use the logger in your application:

logger.info "Hello World!"
logger.error "Oh No!"

Run on Google Cloud

Using the Cloud Logging library for PHP should have the IAM Logs Writer role on Google Cloud. Most Google Cloud environments give this role by default.

Run locally and elsewhere

To use the Cloud Logging library for PHP outside of Google Cloud, including running the library on your workstation, on your data center's computers, or on the VM instances of another cloud provider, you must supply your appropriate service account credentials and Google Cloud project ID directly to the Cloud Logging library for PHP.

View the logs

After being deployed, you can view the logs in Logs Explorer.

Read about Batch Operating System here.

Frequently Asked Questions

What is Google Cloud Platform?

Google Cloud Platform is a Google cloud platform that allows users to access cloud systems and computing services. GCP provides a wide range of cloud computing services in the compute, database, storage, migration, and networking domains.

What are the GCP cloud storage libraries and tools?

Google Cloud Platform Console, which performs primary object and bucket operations.

GustilCommand-line Tool, which gives a command line interface for cloud storage. Cloud Storage Client Libraries provide programming support for various languages such as Java, Ruby, and Python.

What is the pricing model in the GCP cloud?

When working on Google Cloud, the user is charged by Google Compute Engine based on compute instance, network use, and storage. Google Cloud charges virtual machines on a per-second basis, with a minimum charge of one minute. The cost of storage is then calculated based on the amount of data stored.

The network cost is calculated based on the amount of data transferred between virtual machine instances communicating with one another over the network.

Conclusion

I hope this article gave you insights into the logging libraries supported by Google.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, System Design, JavaScript, etc. Enroll in our courses, refer to the mock test and problems available, interview puzzles, and look at the interview bundle and interview experiences for placement preparations.

We hope this blog has helped you increase your knowledge regarding AWS Step functions, and if you liked this blog, check other links. Do upvote our blog to help other ninjas grow. Happy Coding!"

Previous article
Basic setups of Logging Libraries
Next article
Cloud Audit logs in GCP
Live masterclass