刪除記錄檔

刪除全域 `_Default` 記錄檔值區中記錄檔的所有記錄項目。

程式碼範例

C#

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

private void DeleteLog(string logId)
{
    var client = LoggingServiceV2Client.Create();
    LogName logName = new LogName(s_projectId, logId);
    client.DeleteLog(logName, _retryAWhile);
    Console.WriteLine($"Deleted {logId}.");
}

Go

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

import (
	"context"
	"log"

	"cloud.google.com/go/logging/logadmin"
)

func deleteLog(projectID string) error {
	ctx := context.Background()
	adminClient, err := logadmin.NewClient(ctx, projectID)
	if err != nil {
		log.Fatalf("Failed to create logadmin client: %v", err)
	}
	defer adminClient.Close()

	const name = "log-example"
	if err := adminClient.DeleteLog(ctx, name); err != nil {
		return err
	}
	return nil
}

Java

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

boolean deleted = logging.deleteLog(logName);
if (deleted) {
  // the log was deleted
} else {
  // the log was not found
}

Node.js

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');

// Creates a client
const logging = new Logging();

/**
 * TODO(developer): Uncomment the following line to run the code.
 */
// const logName = 'Name of the log to delete, e.g. my-log';

const log = logging.log(logName);

async function deleteLog() {
  // Deletes a logger and all its entries.
  // Note that a deletion can take several minutes to take effect.
  // See https://googleapis.dev/nodejs/logging/latest/Log.html#delete
  await log.delete();
  console.log(`Deleted log: ${logName}`);
}
deleteLog();

PHP

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

use Google\Cloud\Logging\LoggingClient;

/** Delete a logger and all its entries.
 *
 * @param string $projectId The Google project ID.
 * @param string $loggerName The name of the logger.
 */
function delete_logger($projectId, $loggerName)
{
    $logging = new LoggingClient(['projectId' => $projectId]);
    $logger = $logging->logger($loggerName);
    $logger->delete();
    printf("Deleted a logger '%s'." . PHP_EOL, $loggerName);
}

Python

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

def delete_logger(logger_name):
    """Deletes a logger and all its entries.

    Note that a deletion can take several minutes to take effect.
    """
    logging_client = logging.Client()
    logger = logging_client.logger(logger_name)

    logger.delete()

    print("Deleted all logging entries for {}".format(logger.name))

Ruby

如要瞭解如何安裝及使用 Logging 的用戶端程式庫,請參閱這篇文章

如要向 Logging 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

require "google/cloud/logging"

logging = Google::Cloud::Logging.new

# log_name = "The name of the log"
logging.delete_log log_name
puts "Deleted log: #{log_name}"

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器