Logging getLogging() { if (logging == null) { synchronized (this) { if (logging == null) { logging = LoggingOptions.getDefaultInstance().getService(); } } } return logging; }
String getProjectId() { return LoggingOptions.getDefaultInstance().getProjectId(); }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment try (Logging logging = LoggingOptions.getDefaultInstance().getService()) { // Create a metric MetricInfo metricInfo = MetricInfo.newBuilder("test-metric", "severity >= ERROR") .setDescription("Log entries with severity higher or equal to ERROR") .build(); logging.create(metricInfo); // List metrics Page<Metric> metrics = logging.listMetrics(); for (Metric metric : metrics.iterateAll()) { System.out.println(metric); } } } }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment try (Logging logging = LoggingOptions.getDefaultInstance().getService()) { // Create a sink to back log entries to a BigQuery dataset SinkInfo sinkInfo = SinkInfo.newBuilder("test-sink", DatasetDestination.of("test-dataset")) .setFilter("severity >= ERROR") .build(); logging.create(sinkInfo); // List sinks Page<Sink> sinks = logging.listSinks(); for (Sink sink : sinks.iterateAll()) { System.out.println(sink); } } } }
List<LoggingEnhancer> enhancers) { try { loggingOptions = options != null ? options : LoggingOptions.getDefaultInstance(); LoggingConfig config = new LoggingConfig(getClass().getName()); setFilter(config.getFilter());
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment LoggingOptions options = LoggingOptions.getDefaultInstance(); try (Logging logging = options.getService()) { // Create a log entry LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message")) .setLogName("test-log") .setResource( MonitoredResource.newBuilder("global") .addLabel("project_id", options.getProjectId()) .build()) .build(); logging.write(Collections.singleton(firstEntry)); // List log entries Page<LogEntry> entries = logging.listLogEntries( EntryListOption.filter( "logName=projects/" + options.getProjectId() + "/logs/test-log")); for (LogEntry logEntry : entries.iterateAll()) { System.out.println(logEntry); } } } }
/** Expects an existing Stackdriver log name as an argument. */ public static void main(String... args) throws Exception { // [START logging_list_log_entries] // Instantiates a client LoggingOptions options = LoggingOptions.getDefaultInstance(); String logName = args[0]; try (Logging logging = options.getService()) { String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName; // List all log entries Page<LogEntry> entries = logging.listLogEntries( EntryListOption.filter(logFilter)); do { for (LogEntry logEntry : entries.iterateAll()) { System.out.println(logEntry); } entries = entries.getNextPage(); } while (entries != null); } // [END logging_list_log_entries] } }
/** Expects a new or existing Stackdriver log name as the first argument.*/ public static void main(String... args) throws Exception { // Instantiates a client Logging logging = LoggingOptions.getDefaultInstance().getService(); // The name of the log to write to String logName = args[0]; // "my-log"; // The data to write to the log String text = "Hello, world!"; LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) .setSeverity(Severity.ERROR) .setLogName(logName) .setResource(MonitoredResource.newBuilder("global").build()) .build(); // Writes the log entry asynchronously logging.write(Collections.singleton(entry)); System.out.printf("Logged: %s%n", text); } }
Logging getLogging() { if (logging == null) { synchronized (this) { if (logging == null) { logging = LoggingOptions.getDefaultInstance().getService(); } } } return logging; }
String getProjectId() { return LoggingOptions.getDefaultInstance().getProjectId(); }
List<LoggingEnhancer> enhancers) { try { loggingOptions = options != null ? options : LoggingOptions.getDefaultInstance(); LoggingConfig config = new LoggingConfig(getClass().getName()); setFilter(config.getFilter());