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.
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:
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
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:
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.
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.
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!"
Live masterclass
Switch from service to product-based MAANG companies
by Pranav Malik, SDE3 @ Oracle, Ex- Microsoft
17 Oct, 2024
01:30 PM
Predictive Analytics: Forecast ticket sales for an event like Coldplay
by Megna Roy, Data Analyst @ Amazon
15 Oct, 2024
01:30 PM
Switch from service to product-based MAANG companies
by Pranav Malik, SDE3 @ Oracle, Ex- Microsoft
17 Oct, 2024
01:30 PM
Predictive Analytics: Forecast ticket sales for an event like Coldplay