You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2019/06/27 22:11:52 UTC

[juneau] branch master updated: JUNEAU-109 - BasicRestLogger should take in servlet class

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new d419fc8  JUNEAU-109 - BasicRestLogger should take in servlet class
d419fc8 is described below

commit d419fc8b35d25524b3a594c4f164c8ed58c3a819
Author: JamesBognar <ja...@apache.org>
AuthorDate: Thu Jun 27 18:10:35 2019 -0400

    JUNEAU-109 - BasicRestLogger should take in servlet class
---
 .../org/apache/juneau/internal/JuneauLogger.java   | 12 +++++++-
 .../32.LoggingAndErrorHandling/01.LOG4J.html       | 34 ++++++++++++++++++++++
 .../org/apache/juneau/rest/BasicRestLogger.java    | 31 ++++++++++++++++++--
 .../org/apache/juneau/rest/NoOpRestLogger.java     |  7 +++++
 4 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/JuneauLogger.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/JuneauLogger.java
index cea0a37..8f44deb 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/JuneauLogger.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/JuneauLogger.java
@@ -51,7 +51,17 @@ public class JuneauLogger extends java.util.logging.Logger {
 	 * @return A new <l>Logger</l>.
 	 */
 	public static JuneauLogger getLogger(Class<?> forClass) {
-		return new JuneauLogger(java.util.logging.Logger.getLogger(forClass.getName()));
+		return getLogger(forClass.getName());
+	}
+
+	/**
+	 * Get logger for specified class.
+	 *
+	 * @param loggerName The logger name.
+	 * @return A new <l>Logger</l>.
+	 */
+	public static JuneauLogger getLogger(String loggerName) {
+		return new JuneauLogger(java.util.logging.Logger.getLogger(loggerName));
 	}
 
 	/**
diff --git a/juneau-doc/docs/Topics/07.juneau-rest-server/32.LoggingAndErrorHandling/01.LOG4J.html b/juneau-doc/docs/Topics/07.juneau-rest-server/32.LoggingAndErrorHandling/01.LOG4J.html
new file mode 100644
index 0000000..2321677
--- /dev/null
+++ b/juneau-doc/docs/Topics/07.juneau-rest-server/32.LoggingAndErrorHandling/01.LOG4J.html
@@ -0,0 +1,34 @@
+<!--
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ ***************************************************************************************************************************/
+ -->
+
+{new} Using LOG4J for logging
+
+<p>
+	The REST API uses Java logging by default.  If you wish to use LOG4J logging, you simple need to add the following
+	to your JVM arguments and maven dependencies:
+</p>
+
+<h5 class='figure'>Command-line argument</h5>
+<p class='bpcode'>
+	-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
+</p>
+
+<h5 class='figure'>Maven dependency</h5>
+<p class='bpcode'>
+	<xt>&lt;dependency&gt;</xt>
+	    <xt>&lt;groupId&gt;</xt>org.apache.logging.log4j<xt>&lt;/groupId&gt;</xt>
+	    <xt>&lt;artifactId&gt;</xt>log4j-jul<xt>&lt;/artifactId&gt;</xt>
+	<xt>&lt;/dependency&gt;</xt>
+</p>
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestLogger.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestLogger.java
index 444a5d0..12b7fdb 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestLogger.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/BasicRestLogger.java
@@ -37,7 +37,34 @@ import org.apache.juneau.json.*;
  */
 public class BasicRestLogger implements RestLogger {
 
-	private final JuneauLogger logger = JuneauLogger.getLogger(getClass());
+	private final JuneauLogger logger;
+	private final RestContext context;
+
+
+	/**
+	 * Constructor.
+	 *
+	 * @param context The context of the resource object.
+	 */
+	public BasicRestLogger(RestContext context) {
+		this.context = context;
+		this.logger = JuneauLogger.getLogger(getLoggerName());
+	}
+
+	/**
+	 * Returns the logger name.
+	 *
+	 * <p>
+	 * By default returns the class name of the servlet class passed in to the context.
+	 *
+	 * <p>
+	 * Subclasses can override this to provide their own customized logger names.
+	 *
+	 * @return The logger name.
+	 */
+	protected String getLoggerName() {
+		return context == null ? getClass().getName() : context.getResource().getClass().getName();
+	}
 
 	/**
 	 * Returns the Java logger used for logging.
@@ -56,7 +83,7 @@ public class BasicRestLogger implements RestLogger {
 	public void setLevel(Level level) {
 		getLogger().setLevel(level);
 	}
-	
+
 	/**
 	 * Log a message to the logger.
 	 *
diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/NoOpRestLogger.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/NoOpRestLogger.java
index e558db7..b12b018 100644
--- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/NoOpRestLogger.java
+++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/NoOpRestLogger.java
@@ -30,6 +30,13 @@ import java.util.logging.*;
 public class NoOpRestLogger extends BasicRestLogger {
 
 	/**
+	 * Constructor.
+	 */
+	public NoOpRestLogger() {
+		super(null);
+	}
+
+	/**
 	 * Log a message to the logger.
 	 *
 	 * <p>