Using the API
Details on Oracle Cloud Infrastructure Logging search API, and SDK examples.
For information about using the API and signing requests, see REST API documentation and Security Credentials. For information about SDKs, see SDKs and the CLI.
Use the following operation to search logs:
Use the following for saved searches:
SearchLogs SDK Examples
See the following SearchLogs SDK examples for:
- Java
- Python
- Go
- TypeScript
- .NET C#
- Ruby
Java example:
/** To make this code sample work in your Oracle Cloud tenancy,
please replace the values for the following placeholders:
<compartment_OCID> - the ocid of your compartment where the logs are stored;
<log_group_OCID> - the ocid of your log group under the above compartment;
<log_OCID> - the ocid of your log object under the above log group.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.loggingsearch.LogSearchClient;
import com.oracle.bmc.loggingsearch.model.*;
import com.oracle.bmc.loggingsearch.requests.*;
import com.oracle.bmc.loggingsearch.responses.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Date;
import java.util.Arrays;
public class SearchLogsExample {
public static void main(String[] args) throws Exception {
/**
* Create a default authentication provider that uses the DEFAULT
* profile in the configuration file.
* Refer to <see href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File>the public documentation</see> on how to prepare a configuration file.
*/
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider = new ConfigFileAuthenticationDetailsProvider(configFile);
/* Create a service client */
LogSearchClient client = new LogSearchClient(provider);
/* Create a request and dependent object(s). */
SearchLogsDetails searchLogsDetails = SearchLogsDetails.builder()
.timeStart(new Date("Mon May 10 01:02:29 UTC 2021"))
.timeEnd(new Date("Mon May 10 02:02:29 UTC 2021"))
.searchQuery("search \"<compartment_OCID>/<log_group_OCID>/<log_OCID>\" | where level = 'INFO'")
.isReturnFieldInfo(false).build();
SearchLogsRequest searchLogsRequest = SearchLogsRequest.builder()
.searchLogsDetails(searchLogsDetails)
.opcRequestId("RIJJ87OXLQBMPBRN6ZWO/OpcRequestIdExample/UniqueRequestId")
.limit(10).build();
/* Send request to the Client */
SearchLogsResponse response = client.searchLogs(searchLogsRequest);
System.out.println("opc-request-id: " + response.getOpcRequestId());
System.out.println("Got results: " + response.getSearchResponse().getResults().stream().count());
}
}
Python example:
# To make this code sample work in your Oracle Cloud tenancy,
# please replace the values for the following placeholders:
# <compartment_OCID> - the ocid of your compartment where the logs are stored;
# <log_group_OCID> - the ocid of your log group under the above compartment;
# <log_OCID> - the ocid of your log object under the above log group.
from datetime import datetime
import oci
# Create a default config using DEFAULT profile in default location
# Refer to
# https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File
# for more info
config = oci.config.from_file()
# Initialize service client with default config file
loggingsearch_client = oci.loggingsearch.LogSearchClient(config)
# Send the request to service, some parameters are not required, see API
# doc for more info
search_logs_response = loggingsearch_client.search_logs(
search_logs_details=oci.loggingsearch.models.SearchLogsDetails(
time_start=datetime.strptime(
"2021-05-10T01:02:29.600Z",
"%Y-%m-%dT%H:%M:%S.%fZ"),
time_end=datetime.strptime(
"2021-05-10T02:02:29.600Z",
"%Y-%m-%dT%H:%M:%S.%fZ"),
search_query="search \"<compartment_OCID>/<log_group_OCID>/<log_OCID>\" | where level = 'INFO'",
is_return_field_info=False,
),
opc_request_id="RIJJ87OXLQBMPBRN6ZWO/OpcRequestIdExample/UniqueRequestId",
limit=10,
)
# Get the data from response
print(search_logs_response.data)
Go example:
// To make this code sample work in your Oracle Cloud tenancy,
// please replace the values for the following placeholders:
// <compartment_OCID> - the ocid of your compartment where the logs are stored;
// <log_group_OCID> - the ocid of your log group under the above compartment;
// <log_OCID> - the ocid of your log object under the above log group.
package main
import (
"context"
"fmt"
"time"
"github.com/oracle/oci-go-sdk/common"
"github.com/oracle/oci-go-sdk/example/helpers"
"github.com/oracle/oci-go-sdk/loggingsearch"
)
func main() {
// Create a default authentication provider that uses the DEFAULT
// profile in the configuration file.
// Refer to <see href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File>the public documentation</see> on how to prepare a configuration file.
client, err := loggingsearch.NewLogSearchClientWithConfigurationProvider(common.DefaultConfigProvider())
helpers.FatalIfError(err)
// Create a request and dependent object(s).
start, _ := time.Parse(time.RFC3339, "2021-05-10T01:02:29.600Z")
end, _ := time.Parse(time.RFC3339, "2021-05-10T02:02:29.600Z")
req := loggingsearch.SearchLogsRequest{OpcRequestId: common.String("RIJJ87OXLQBMPBRN6ZWO/OpcRequestIdExample/UniqueRequestId"),
SearchLogsDetails: loggingsearch.SearchLogsDetails{SearchQuery: common.String(`search "<compartment_OCID>/<log_group_OCID>/<log_OCID>" | where level = 'INFO'`),
TimeStart: &common.SDKTime{Time: start},
TimeEnd: &common.SDKTime{Time: end},
IsReturnFieldInfo: common.Bool(false)},
Limit: common.Int(10)}
// Send the request using the service client
resp, err := client.SearchLogs(context.Background(), req)
helpers.FatalIfError(err)
// Retrieve value from the response.
fmt.Println(resp)
}
TypeScript example:
// To make this code sample work in your Oracle Cloud tenancy,
// please replace the values for the following placeholders:
// <compartment_OCID> - the ocid of your compartment where the logs are stored;
// <log_group_OCID> - the ocid of your log group under the above compartment;
// <log_OCID> - the ocid of your log object under the above log group.
import * as loggingsearch from "oci-loggingsearch";
import common = require("oci-common");
// Create a default authentication provider that uses the DEFAULT
// profile in the configuration file.
// Refer to <see href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File>the public documentation</see> on how to prepare a configuration file.
const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider();
(async () => {
try {
// Create a service client
const client = new loggingsearch.LogSearchClient({ authenticationDetailsProvider: provider });
// Create a request and dependent object(s).
const searchLogsDetails = {
timeStart: new Date("Mon May 10 01:02:29 UTC 2021"),
timeEnd: new Date("Mon May 10 02:02:29 UTC 2021"),
searchQuery: "search \"<compartment_OCID>/<log_group_OCID>/<log_OCID>\" | where level = 'INFO'",
isReturnFieldInfo: false
};
const searchLogsRequest: loggingsearch.requests.SearchLogsRequest = {
searchLogsDetails: searchLogsDetails,
opcRequestId: "RIJJ87OXLQBMPBRN6ZWO/OpcRequestIdExample/UniqueRequestId",
limit: 10
};
// Send request to the Client.
const searchLogsResponse = await client.searchLogs(searchLogsRequest);
console.log("opc-request-id: " + searchLogsResponse.opcRequestId);
console.log("Got results: " + searchLogsResponse.searchResponse.results.length);
} catch (error) {
console.log("searchLogs Failed with error " + error);
}
})();
.NET C# example:
// To make this code sample work in your Oracle Cloud tenancy,
// please replace the values for the following placeholders:
// <compartment_OCID> - the ocid of your compartment where the logs are stored;
// <log_group_OCID> - the ocid of your log group under the above compartment;
// <log_OCID> - the ocid of your log object under the above log group.
using System;
using System.Threading.Tasks;
using Oci.LoggingsearchService;
using Oci.Common;
using Oci.Common.Auth;
namespace Oci.Sdk.DotNet.Example.Loggingsearch
{
public class SearchLogsExample
{
public static async Task Main()
{
// Create a request and dependent object(s).
var searchLogsDetails = new Oci.LoggingsearchService.Models.SearchLogsDetails
{
TimeStart = DateTime.Parse("05/10/2021 01:02:29"),
TimeEnd = DateTime.Parse("05/10/2021 02:02:29"),
SearchQuery = "search \"<compartment_OCID>/<log_group_OCID>/<log_OCID>\" | where level = 'INFO'",
IsReturnFieldInfo = false
};
var searchLogsRequest = new Oci.LoggingsearchService.Requests.SearchLogsRequest
{
SearchLogsDetails = searchLogsDetails,
OpcRequestId = "RIJJ87OXLQBMPBRN6ZWO/OpcRequestIdExample/UniqueRequestId",
Limit = 10
};
// Create a default authentication provider that uses the DEFAULT
// profile in the configuration file.
// Refer to <see href="https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File>the public documentation</see> on how to prepare a configuration file.
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
try
{
// Create a service client and send the request.
using (var client = new LogSearchClient(provider, new ClientConfiguration()))
{
var response = await client.SearchLogs(searchLogsRequest);
// Retrieve value from the response.
var resultsValue = response.SearchResponse.Results;
Console.WriteLine("opc-request-id: " + response.OpcRequestId);
Console.WriteLine("Got results: " + response.SearchResponse.Results.Count);
}
}
catch (Exception e)
{
Console.WriteLine($"SearchLogs Failed with {e.Message}");
throw e;
}
}
}
}
Ruby example:
# To make this code sample work in your Oracle Cloud tenancy,
# please replace the values for the following placeholders:
# <compartment_OCID> - the ocid of your compartment where the logs are stored;
# <log_group_OCID> - the ocid of your log group under the above compartment;
# <log_OCID> - the ocid of your log object under the above log group.
require 'oci'
require 'date'
# Create a default config using DEFAULT profile in default location
# Refer to https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm#SDK_and_CLI_Configuration_File for more info
config = OCI::ConfigFileLoader.load_config
# Initialize service client with default config file
loggingsearch_client = OCI::Loggingsearch::LogSearchClient.new(config: config)
# Send the request to service, some parameters are not required, see API doc for more info
search_logs_response =
loggingsearch_client.search_logs(
OCI::Loggingsearch::Models::SearchLogsDetails.new(
time_start:
DateTime.strptime('2021-05-10T01:02:29Z', '%Y-%m-%dT%H:%M:%SZ'),
time_end:
DateTime.strptime('2021-05-10T02:02:29Z', '%Y-%m-%dT%H:%M:%SZ'),
search_query: 'search "<compartment_OCID>/<log_group_OCID>/<log_OCID>" | where level = \'INFO\'',
is_return_field_info: false
),
opc_request_id: 'RIJJ87OXLQBMPBRN6ZWO/OpcRequestIdExample/UniqueRequestId',
limit: 10
)
# Get the data from response
puts "#{search_logs_response.data}"