|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.logging; |
| 18 | + |
| 19 | +import com.google.cloud.MonitoredResource; |
| 20 | +import com.google.cloud.logging.Logging.WriteOption; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.logging.Filter; |
| 24 | +import java.util.logging.Formatter; |
| 25 | +import java.util.logging.Logger; |
| 26 | +import java.util.logging.SimpleFormatter; |
| 27 | + |
| 28 | +/** |
| 29 | + * A logging handler that asynchronously outputs logs generated with |
| 30 | + * {@link java.util.logging.Logger} to Stackdriver Logging. |
| 31 | + * |
| 32 | + * <p>Java logging levels (see {@link java.util.logging.Level}) are mapped to the following Google |
| 33 | + * Stackdriver Logging severities: |
| 34 | + * |
| 35 | + * <table summary="Mapping of Java logging level to Stackdriver Logging severities"> |
| 36 | + * <tr><th width="50%">Java Level</th><th>Stackdriver Logging Severity</th></tr> |
| 37 | + * <tr><td>SEVERE</td><td>ERROR</td></tr> |
| 38 | + * <tr><td>WARNING</td><td>WARNING</td></tr> |
| 39 | + * <tr><td>INFO</td><td>INFO</td></tr> |
| 40 | + * <tr><td>CONFIG</td><td>INFO</td></tr> |
| 41 | + * <tr><td>FINE</td><td>DEBUG</td></tr> |
| 42 | + * <tr><td>FINER</td><td>DEBUG</td></tr> |
| 43 | + * <tr><td>FINEST</td><td>DEBUG</td></tr> |
| 44 | + * </table> |
| 45 | + * |
| 46 | + * <p>Original Java logging levels are added as labels (with {@code levelName} and |
| 47 | + * {@code levelValue} keys, respectively) to the corresponding Stackdriver Logging {@link LogEntry}. |
| 48 | + * You can read entry labels using {@link LogEntry#labels()}. To use logging levels that correspond |
| 49 | + * to Stackdriver Logging severities you can use {@link LoggingLevel}. |
| 50 | + * |
| 51 | + * <p><b>Configuration</b>: By default each {@code AsyncLoggingHandler} is initialized using the |
| 52 | + * following {@code LogManager} configuration properties (that you can set in the |
| 53 | + * {@code logging.properties} file. If properties are not defined (or have invalid values) then the |
| 54 | + * specified default values are used. |
| 55 | + * <ul> |
| 56 | + * <li>{@code com.google.cloud.logging.AsyncLoggingHandler.log} the log name (defaults to |
| 57 | + * {@code java.log}). |
| 58 | + * <li>{@code com.google.cloud.logging.AsyncLoggingHandler.level} specifies the default level for |
| 59 | + * the handler (defaults to {@code Level.INFO}). |
| 60 | + * <li>{@code com.google.cloud.logging.AsyncLoggingHandler.filter} specifies the name of a |
| 61 | + * {@link Filter} class to use (defaults to no filter). |
| 62 | + * <li>{@code com.google.cloud.logging.AsyncLoggingHandler.formatter} specifies the name of a |
| 63 | + * {@link Formatter} class to use (defaults to {@link SimpleFormatter}). |
| 64 | + * <li>{@code com.google.cloud.logging.AsyncLoggingHandler.flushSize} specifies the maximum size of |
| 65 | + * the log buffer. Once reached, logs are transmitted to the Stackdriver Logging service |
| 66 | + * (defaults to 1). |
| 67 | + * <li>{@code com.google.cloud.logging.AsyncLoggingHandler.flushLevel} specifies the flush log |
| 68 | + * level. When a log with this level is published, logs are transmitted to the Stackdriver |
| 69 | + * Logging service (defaults to {@link LoggingLevel#ERROR}). |
| 70 | + * </ul> |
| 71 | + * |
| 72 | + * <p>To add a {@code LoggingHandler} to an existing {@link Logger} and be sure to avoid infinite |
| 73 | + * recursion when logging, use the {@link #addHandler(Logger, LoggingHandler)} method. |
| 74 | + */ |
| 75 | +public class AsyncLoggingHandler extends LoggingHandler { |
| 76 | + |
| 77 | + public AsyncLoggingHandler() { |
| 78 | + super(); |
| 79 | + } |
| 80 | + |
| 81 | + public AsyncLoggingHandler(String logName) { |
| 82 | + super(logName); |
| 83 | + } |
| 84 | + |
| 85 | + public AsyncLoggingHandler(String logName, LoggingOptions options) { |
| 86 | + super(logName, options); |
| 87 | + } |
| 88 | + |
| 89 | + public AsyncLoggingHandler(String logName, LoggingOptions options, MonitoredResource resource) { |
| 90 | + super(logName, options, resource); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + void write(List<LogEntry> entries, WriteOption... options) { |
| 95 | + logging().writeAsync(entries, options); |
| 96 | + } |
| 97 | +} |
0 commit comments