Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Setting Up Language Runtimes
2.1.
Writing logs
2.2.
Reading logs
3.
C#: Using .NET logging frameworks or calling the API
3.1.
Use a Google logging provider for .NET's standard logging framework
3.2.
Use a Google Log4Net Provider
3.3.
Use Google.Cloud.Logging.V2 to directly call the Logging API
4.
Java: Using Logback and java.util.logging
4.1.
Using Logback appender for Cloud Logging
4.1.1.
Installing the dependency
4.1.2.
Logback configuration
4.1.3.
Example
4.2.
Using the java.util.logging handler
4.2.1.
Installing the dependency
4.2.2.
java.util.logging configuration
5.
Frequently Asked Questions
5.1.
What is Google Cloud Platform?
5.2.
What are the GCP cloud storage libraries and tools?
5.3.
What is the pricing model in the GCP cloud?
6.
Conclusion
Last Updated: Mar 27, 2024

Basic setups of Logging Libraries

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. 

Setting Up Language Runtimes

Many programming languages have standard log-writing interfaces that you can reconfigure to use Logging.

Writing logs

  • Write your logs to existing log files such as syslog on your VM instance. The Logging agent sends the logs to Google Cloud's operations suite.
  • Configure a standard logging package to send the logs you write to Logging. 
  • Call the Logging API using Logging's Client Libraries for your programming language.
  • Call the Logging API REST endpoint directly. Use the gcloud CLI.

Reading logs

  • Use the Logs Explorer in the Google Cloud console.
  • Call the Logging API through the Client Libraries for your programming language.
  • Call the Logging API REST endpoints directly. 
  • Use the gcloud CLI.

C#: Using .NET logging frameworks or calling the API

There are three ways to use Cloud Logging from your .NET application:

  • Use a Google logging provider for .NET's standard logging framework. 
  • Use a Google Log4Net provider.
  • Use Google.Cloud.Logging.V2 to directly call the Logging API.

Use a Google logging provider for .NET's standard logging framework

You can enable Cloud Logging for .NET applications by using Google.Cloud.Diagnostics libraries for .NET.

  • For applications that aim at ASP.NET Core 3 or later, use Google.Cloud.Diagnostics.AspNetCore3 library. 
  • For applications that aim at earlier versions of ASP.NET Core, use Google.Cloud.Diagnostics.AspNetCore library. 
  • For applications that aim at non-ASP.NET Core platforms, use Google.Cloud.Diagnostics.Common library. 

Use a Google Log4Net Provider

The Google.Cloud.Logging.Log4Net library implements a Log4Net provider for Cloud Logging.

Use Google.Cloud.Logging.V2 to directly call the Logging API

You can also write logs by calling the Cloud Logging API using Google.Cloud.Logging.V2 client library. 

Java: Using Logback and java.util.logging

Using Logback appender for Cloud Logging

Installing the dependency

If you are utilizing Maven, add the following to your pom.xml file. 

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-logging-logback</artifactId>
  <version>0.127.0-alpha</version>
</dependency>


If you are utilizing Gradle, add the following to your dependencies:

implementation 'com.google.cloud:google-cloud-logging-logback:0.127.0-alpha'


If you are utilizing sbt, add the following to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-logging-logback" % "0.127.0-alpha"

Logback configuration

Logback can be configured using a script expressed in XML, Groovy, or programmatically.

You can customize the log name, minimum severity threshold, or provide additional enhancers. This is an example of Logback configuration in XML format:

<configuration>
  <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
    <!-- Optional : filter logs at or above a level -->
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
      <level>INFO</level>
    </filter>
    <log>application.log</log> <!-- Optional : default java.log -->
    <resourceType>gae_app</resourceType> <!-- Optional : default: auto-detected, fallback: global -->
    <enhancer>com.example.logging.logback.enhancers.ExampleEnhancer</enhancer> <!-- Optional -->
    <flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->
  </appender>
  <root level="info">
    <appender-ref ref="CLOUD" />
  </root>
</configuration>

Example

Once you have configured Logback to utilize the Cloud Logging Logback appender, you can then redirect logs using the SLF4J logging API. If you plan to run the example outside of Google Cloud or locally, provide Google Cloud configuration . This snippet shows how to log in using SLF4J facade within your application:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Quickstart {
  private static final Logger logger = LoggerFactory.getLogger(Quickstart.class);
  public static void main(String[] args) {
    logger.info("Logging INFO with Logback");
    logger.error("Logging ERROR with Logback");
  }
}

Using the java.util.logging handler

Installing the dependency

If you are using Maven with a BOM, add the following to your pom.xml file:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>25.4.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-logging</artifactId>
  </dependency>
  <!-- ...
</dependencies>


If you are using Gradle,  add the following to your dependencies:

implementation platform('com.google.cloud:libraries-bom:25.4.0')
implementation 'com.google.cloud:google-cloud-logging'


If you are using sbt, add the following to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.10.0"

java.util.logging configuration

Logging handlers can be added by using a configuration file programmatically. You must provide the path to the configuration file to your application as a system property: -Djava.util.logging.config.file=/path/to/logging.properties.

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!"

Live masterclass