Initial Commit of Log-Level plug-in

Allows setting log-levels on plug-in start-up. The plug-in works
similar to the ssh logging command but allows setting log-levels
that survives a restart.

Change-Id: I3eb7b1663a489c85ab5392e8a85bb3a67200dd3c
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f90bb5f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,8 @@
+/.buckd
+/.buckversion
+/.classpath
+/.project
+/.watchmanconfig
+/buck-out/
+/bucklets
+/eclipse-out
diff --git a/BUCK b/BUCK
new file mode 100644
index 0000000..1e8c3d9
--- /dev/null
+++ b/BUCK
@@ -0,0 +1,18 @@
+include_defs('//bucklets/gerrit_plugin.bucklet')
+
+gerrit_plugin(
+  name = 'log-level',
+  srcs = glob(['src/main/java/**/*.java']),
+  deps = ['//lib/log:log4j'],
+  resources = glob(['src/main/resources/**/*']),
+  manifest_entries = [
+    'Gerrit-PluginName: log-level',
+    'Gerrit-Module: com.googlesource.gerrit.plugins.loglevel.LogLevelModule',
+    'Implementation-Title: Log Level plugin',
+  ]
+)
+
+java_library(
+  name = 'classpath',
+  deps = GERRIT_PLUGIN_API + [':log-level__plugin'],
+)
diff --git a/lib/gerrit/BUCK b/lib/gerrit/BUCK
new file mode 100644
index 0000000..85b90a8
--- /dev/null
+++ b/lib/gerrit/BUCK
@@ -0,0 +1,12 @@
+include_defs('//bucklets/maven_jar.bucklet')
+
+VER = '2.12-SNAPSHOT'
+REPO = MAVEN_LOCAL
+
+maven_jar(
+  name = 'plugin-api',
+  id = 'com.google.gerrit:gerrit-plugin-api:' + VER,
+  license = 'Apache2.0',
+  attach_source = False,
+  repository = REPO,
+)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/loglevel/LogLevel.java b/src/main/java/com/googlesource/gerrit/plugins/loglevel/LogLevel.java
new file mode 100644
index 0000000..fa4aa5f
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/loglevel/LogLevel.java
@@ -0,0 +1,64 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// 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.googlesource.gerrit.plugins.loglevel;
+
+import com.google.gerrit.extensions.events.LifecycleListener;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import com.google.inject.Inject;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.eclipse.jgit.lib.Config;
+
+public class LogLevel implements LifecycleListener {
+  private static final Logger log =
+      Logger.getLogger(LogLevel.class);
+
+  private static final String CFG_NAME = "log-level";
+
+  private static enum LevelOption {
+    ALL,
+    TRACE,
+    DEBUG,
+    INFO,
+    WARN,
+    ERROR,
+    FATAL,
+    OFF
+  }
+
+  @Inject
+  private PluginConfigFactory configFactory;
+
+  @Override
+  public void start() {
+    log.info("Plug-in started");
+    Config config = configFactory.getGlobalPluginConfig(CFG_NAME);
+    for (LevelOption level : LevelOption.values()) {
+      String[] names = config.getStringList("log-levels", level.name(), "name");
+      if (names != null) {
+        log.info("Setting log levels to [" + level + "] ");
+        for (String name : names) {
+          Logger logger = Logger.getLogger(name);
+          logger.setLevel(Level.toLevel(level.name()));
+          log.info("[" + level + "] " + name);
+        }
+      }
+    }
+  }
+
+  @Override
+  public void stop() {
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/loglevel/LogLevelModule.java b/src/main/java/com/googlesource/gerrit/plugins/loglevel/LogLevelModule.java
new file mode 100644
index 0000000..c101548
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/loglevel/LogLevelModule.java
@@ -0,0 +1,26 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// 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.googlesource.gerrit.plugins.loglevel;
+
+import com.google.gerrit.extensions.events.LifecycleListener;
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.inject.AbstractModule;
+
+public class LogLevelModule extends AbstractModule {
+
+  @Override
+  protected void configure() {
+    DynamicSet.bind(binder(), LifecycleListener.class).to(LogLevel.class);
+  }
+}
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
new file mode 100644
index 0000000..e09aaff
--- /dev/null
+++ b/src/main/resources/Documentation/about.md
@@ -0,0 +1 @@
+Allows setting log-levels on plug-in start-up.
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..1086a18
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,79 @@
+Build
+=====
+
+This @PLUGIN@ plugin is built with Buck.
+
+Two build modes are supported: Standalone and in Gerrit tree.
+The standalone build mode is recommended, as this mode doesn't require
+the Gerrit tree to exist locally.
+
+#### Build standalone
+
+Clone bucklets library:
+
+```
+  git clone https://gerrit.googlesource.com/bucklets
+
+```
+and link it to @PLUGIN@ plugin directory:
+
+```
+  cd @PLUGIN@ && ln -s ../bucklets .
+```
+
+Add link to the .buckversion file:
+
+```
+  cd @PLUGIN@ && ln -s bucklets/buckversion .buckversion
+```
+
+Add link to the .watchmanconfig file:
+
+```
+  cd @PLUGIN@ && ln -s bucklets/watchmanconfig .watchmanconfig
+```
+
+To build the plugin, issue the following command:
+
+```
+  buck build plugin
+```
+
+The output is created in
+
+```
+  buck-out/gen/@PLUGIN@.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./bucklets/tools/eclipse.py
+```
+
+#### Build in Gerrit tree
+
+Clone or link this plugin to the plugins directory of Gerrit's source
+tree, and issue the command:
+
+```
+  buck build plugins/@PLUGIN@
+```
+
+in the root of Gerrit's source tree to build
+
+The output is created in
+
+```
+  buck-out/gen/plugins/@PLUGIN@/@PLUGIN@.jar
+```
+
+This project can be imported into the Eclipse IDE:
+
+```
+  ./tools/eclipse/project.py
+```
+
+[Back to @PLUGIN@ documentation index][index]
+
+[index]: index.html
diff --git a/src/main/resources/Documentation/log-level.md b/src/main/resources/Documentation/log-level.md
new file mode 100644
index 0000000..ed7367a
--- /dev/null
+++ b/src/main/resources/Documentation/log-level.md
@@ -0,0 +1,22 @@
+@PLUGIN@
+==============
+
+NAME
+----
+@PLUGIN@ - Set log-levels on plug-in start-up
+
+SYNOPSIS
+--------
+@PLUGIN@.config:
+```
+[log-level "FATAL"]
+    name = com.google.gerrit.server.change.EmailReviewComments
+
+[log-level "INFO"]
+    name = com.google.gerrit.common.Version
+```
+
+DESCRIPTION
+-----------
+Allows setting log-levels on plug-in start-up. The plug-in works similar to the
+ssh `logging` command but allows setting log-levels permanently.