Menyiapkan Cloud Logging untuk Java

Anda dapat menulis log ke Cloud Logging dari aplikasi Java dengan menggunakan appender Logback atau java.util.logging handler, atau dengan menggunakan library Cloud Logging untuk Java secara langsung.

Agen Cloud Logging tidak harus diinstal untuk menggunakan library Cloud Logging untuk Java.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Logging API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Logging API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

Appender Logback untuk Cloud Logging

Dengan appender Logback, Anda dapat menggunakan Cloud Logging dengan fasad logging SLF4J.

Menginstal dependensi

Jika Anda menggunakan Maven, tambahkan kode berikut ke file pom.xml Anda: Untuk informasi lebih lanjut tentang BOM, lihat BOM Library Google Cloud Platform.


  com.google.cloud
  google-cloud-logging-logback
  0.131.11-alpha

Jika menggunakan Gradle, tambahkan kode berikut ke dependensi Anda:

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

Jika Anda menggunakan sbt, tambahkan kode berikut ke dependensi Anda:

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

Konfigurasi Logback

Logback dapat dikonfigurasi secara terprogram atau menggunakan skrip yang dinyatakan dalam XML atau Groovy.

Anda dapat menyesuaikan nilai minimum keparahan, nama log, atau memberikan peningkatan tambahan. Berikut adalah contoh konfigurasi Logback dalam format XML:

<configuration>
  <appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
    
    ch.qos.logback.classic.filter.ThresholdFilter">
      INFO
    
    application.log 
    gae_app 
    com.example.logging.logback.enhancers.ExampleEnhancer 
    WARN 
  

  info">
    CLOUD" />
  
</configuration>

Contoh

Setelah mengonfigurasi Logback untuk menggunakan Logback appender Cloud Logging, Anda kini dapat mengalihkan log menggunakan API logging SLF4J. Berikan Google Cloud konfigurasi jika Anda berencana untuk menjalankan contoh secara lokal atau di luar Google Cloud. Cuplikan ini menunjukkan cara mencatat ke dalam log menggunakan fasad SLF4J dalam aplikasi Anda:


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");
  }
}

Handler java.util.logging

Anda juga dapat menggunakan Cloud Logging API menggunakan handler Java Logging API default.

Menginstal dependensi

Jika Anda menggunakan Maven dengan BOM, tambahkan kode berikut ke file pom.xml:


  
    
      com.google.cloud
      libraries-bom
      26.55.0
      pom
      import
    
  



  
    com.google.cloud
    google-cloud-logging