From 9667eaef7defc7e02d166b56fced71c2d8cf95c5 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 4 May 2017 15:54:30 -0700 Subject: [PATCH 01/13] Initial commit : Logback appender --- .../google-cloud-logging-logback/README.md | 127 +++++++++ .../google-cloud-logging-logback/pom.xml | 48 ++++ .../logging/contrib/LoggingAppender.java | 267 ++++++++++++++++++ .../logging/contrib/LoggingAppenderTest.java | 151 ++++++++++ .../enhancers/AnotherTestLoggingEnhancer.java | 28 ++ .../enhancers/TestLoggingEnhancer.java | 28 ++ google-cloud-contrib/pom.xml | 1 + 7 files changed, 650 insertions(+) create mode 100644 google-cloud-contrib/google-cloud-logging-logback/README.md create mode 100644 google-cloud-contrib/google-cloud-logging-logback/pom.xml create mode 100644 google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/contrib/LoggingAppender.java create mode 100644 google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/contrib/LoggingAppenderTest.java create mode 100644 google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/contrib/enhancers/AnotherTestLoggingEnhancer.java create mode 100644 google-cloud-contrib/google-cloud-logging-logback/src/test/java/com/google/cloud/logging/contrib/enhancers/TestLoggingEnhancer.java diff --git a/google-cloud-contrib/google-cloud-logging-logback/README.md b/google-cloud-contrib/google-cloud-logging-logback/README.md new file mode 100644 index 000000000000..a14f8c2f043b --- /dev/null +++ b/google-cloud-contrib/google-cloud-logging-logback/README.md @@ -0,0 +1,127 @@ +Logback Appender for Google Cloud Logging +========================================= + +Logback appender `com.google.cloud.logging.CloudLoggingAppender` for +[Google Cloud Logging](https://cloud.google.com/logging/). + +This library allows you to use [Logback](https://logback.qos.ch/) configuration and the [Slf4j](https://www.slf4j.org/) interface to log via Google Cloud logging + +- [Homepage](https://googlecloudplatform.github.io/google-cloud-java/) +- [API Documentation](https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/logging/package-summary.html) + +> Note: This client is a work-in-progress, and may occasionally +> make backwards-incompatible changes. + +Quickstart +---------- +If you are using Maven, add this to your pom.xml file +```xml + + com.google.cloud + google-cloud-logging-logback + 0.17.1-alpha + +``` +If you are using Gradle, add this to your dependencies +```Groovy +compile 'com.google.cloud:google-cloud-logging-logback:0.17.1-alpha' +``` +If you are using SBT, add this to your dependencies +```Scala +libraryDependencies += "com.google.cloud" % "google-cloud-logging-logback" % "0.17.1-alpha" +``` + +Usage +----- + +Add the appender to your [Logback configuration](https://logback.qos.ch/manual/configuration.html) `logback.xml` +```xml + + + DEBUG + application.log + com.google.cloud.logging.TestLoggingEnhancer + WARN + + + + + + +``` + +In your code : + +```java +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestLogger { + private final Logger logger = LoggerFactory.getLogger(TestLogger.class); + + public void log(String name) { + logger.info("This is a test"); + } + + public static void main(String[] args) { + TestLogger testLogger = new TestLogger(); + testLogger.log("test"); + } +} +``` + + +Authentication +-------------- + +See the [Authentication](https://github.com/GoogleCloudPlatform/google-cloud-java#authentication) +section in the base directory's README. + + +Limitations +----------- + +This library is usable, but not yet complete. + * Auto-reload of properties is not fully tested + + +Java Versions +------------- + +Java 7 or above is required for using this client. + +Versioning +---------- + +This library follows [Semantic Versioning](http://semver.org/). + +It is currently in major version zero (``0.y.z``), which means that anything +may change at any time and the public API should not be considered +stable. + +Contributing +------------ + +Contributions to this library are always welcome and highly encouraged. + +See `google-cloud`'s [CONTRIBUTING] documentation and the +[shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) +for more information on how to get started. + +Please note that this project is released with a Contributor Code of Conduct. +By participating in this project you agree to abide by its terms. See +[Code of Conduct][code-of-conduct] for more information. + +License +------- +Apache 2.0 - See [LICENSE] for more information. + + +[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/CONTRIBUTING.md +[code-of-conduct]:https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct +[LICENSE]: https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/LICENSE +[TESTING]: https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/TESTING.md#testing-code-that-uses-storage +[cloud-platform]: https://cloud.google.com/ + +[cloud-logging]: https://cloud.google.com/logging/ +[cloud-logging-docs]: https://cloud.google.com/logging/docs/overview diff --git a/google-cloud-contrib/google-cloud-logging-logback/pom.xml b/google-cloud-contrib/google-cloud-logging-logback/pom.xml new file mode 100644 index 000000000000..1c6b556da3b6 --- /dev/null +++ b/google-cloud-contrib/google-cloud-logging-logback/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + google-cloud-logging-logback + 0.17.2-alpha-SNAPSHOT + jar + Google Cloud Logging Logback Appender + https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-contrib/google-cloud-logging-logback + + Logback support for Google Cloud Logging + + + + 1.1.8 + + + + com.google.cloud + google-cloud-contrib + 0.17.2-alpha-SNAPSHOT + + + + ch.qos.logback + logback-classic + ${logback.version} + + + com.google.cloud + google-cloud-logging + + + + org.mockito + mockito-core + 2.5.5 + test + + + junit + junit + 4.12 + test + + + diff --git a/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/contrib/LoggingAppender.java b/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/contrib/LoggingAppender.java new file mode 100644 index 000000000000..d6b7436d1795 --- /dev/null +++ b/google-cloud-contrib/google-cloud-logging-logback/src/main/java/com/google/cloud/logging/contrib/LoggingAppender.java @@ -0,0 +1,267 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.logging.contrib; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.UnsynchronizedAppenderBase; +import com.google.cloud.MonitoredResource; +import com.google.cloud.logging.LogEntry; +import com.google.cloud.logging.Logging; +import com.google.cloud.logging.Logging.WriteOption; +import com.google.cloud.logging.LoggingEnhancer; +import com.google.cloud.logging.LoggingOptions; +import com.google.cloud.logging.MonitoredResourceUtil; +import com.google.cloud.logging.Payload; +import com.google.cloud.logging.Severity; +import com.google.common.collect.ImmutableMap; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * Logback appender for StackDriver Cloud Logging. + * + *

Appender configuration in logback.xml: + *