Skip to content

Commit f9a2d1a

Browse files
committed
Add JUL handlers for Logging, examples and tests (#1117)
1 parent 3eca483 commit f9a2d1a

8 files changed

Lines changed: 1233 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.examples.logging.snippets;
18+
19+
import com.google.cloud.logging.LoggingHandler;
20+
21+
import java.util.logging.Logger;
22+
23+
/**
24+
* A snippet showing how to use {@link java.util.logging.Logger} to log entries to Stackdriver
25+
* Logging. The snippet shows how to install a Stackdriver Logging handler using
26+
* {@link com.google.cloud.logging.LoggingHandler#addHandler(Logger, LoggingHandler)}. Notice that
27+
* this could also be done through the {@code logging.properties} file, adding the following line:
28+
* <pre>
29+
* {@code com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler}
30+
* </pre>
31+
*/
32+
public class AddLoggingHandler {
33+
34+
private final static Logger LOGGER = Logger.getLogger(AddLoggingHandler.class.getName());
35+
36+
public static void main(String... args) {
37+
// Add the Stackdriver Logging handler
38+
LoggingHandler.addHandler(LOGGER, new LoggingHandler());
39+
40+
// log using the logger
41+
LOGGER.warning("test warning");
42+
}
43+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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. Alternatively
74+
* you can add the handler via {@code logging.properties}. For example using the following line:
75+
* <pre>
76+
* {@code com.example.mypackage.handlers=com.google.cloud.logging.AsyncLoggingHandler}
77+
* </pre>
78+
*/
79+
public class AsyncLoggingHandler extends LoggingHandler {
80+
81+
public AsyncLoggingHandler() {
82+
super();
83+
}
84+
85+
public AsyncLoggingHandler(String logName) {
86+
super(logName);
87+
}
88+
89+
public AsyncLoggingHandler(String logName, LoggingOptions options) {
90+
super(logName, options);
91+
}
92+
93+
public AsyncLoggingHandler(String logName, LoggingOptions options, MonitoredResource resource) {
94+
super(logName, options, resource);
95+
}
96+
97+
@Override
98+
void write(List<LogEntry> entries, WriteOption... options) {
99+
logging().writeAsync(entries, options);
100+
}
101+
}

0 commit comments

Comments
 (0)