001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest;
014
015import java.text.*;
016import java.util.logging.*;
017
018import javax.servlet.http.*;
019
020import org.apache.juneau.json.*;
021
022/**
023 * Logging utility class.
024 *
025 * <ul class='seealso'>
026 *    <li class='jf'>{@link RestContext#REST_logger}
027 *    <li class='link'>{@doc juneau-rest-server.LoggingAndDebugging}
028 * </ul>
029 *
030 * @deprecated Use {@link RestCallLogger}
031 */
032@Deprecated
033public interface RestLogger {
034
035   /**
036    * Represents no RestLogger.
037    *
038    * <p>
039    * Used on annotation to indicate that the value should be inherited from the parent class, and
040    * ultimately {@link BasicRestLogger} if not specified at any level.
041    */
042   public interface Null extends RestLogger {}
043
044   /**
045    * Sets the logging level for this logger.
046    *
047    * @param level The new level.
048    */
049   public void setLevel(Level level);
050
051   /**
052    * Log a message to the logger.
053    *
054    * @param level The log level.
055    * @param cause The cause.
056    * @param msg The message to log.
057    * @param args Optional {@link MessageFormat}-style arguments.
058    */
059   public void log(Level level, Throwable cause, String msg, Object...args);
060
061   /**
062    * Log a message.
063    *
064    * @param level The log level.
065    * @param msg The message to log.
066    * @param args Optional {@link MessageFormat}-style arguments.
067    */
068   public void log(Level level, String msg, Object...args);
069
070   /**
071    * Same as {@link #log(Level, String, Object...)} excepts runs the arguments through {@link SimpleJsonSerializer#DEFAULT_READABLE}.
072    *
073    * <p>
074    * Serialization of arguments do not occur if message is not logged, so it's safe to use this method from within
075    * debug log statements.
076    *
077    * <h5 class='section'>Example:</h5>
078    * <p class='bcode w800'>
079    *    logObjects(<jsf>DEBUG</jsf>, <js>"Pojo contents:\n{0}"</js>, myPojo);
080    * </p>
081    *
082    * @param level The log level.
083    * @param msg The message to log.
084    * @param args Optional {@link MessageFormat}-style arguments.
085    */
086   public void logObjects(Level level, String msg, Object...args);
087
088   /**
089    * Callback method for logging errors during HTTP requests.
090    *
091    * <p>
092    * Typically, subclasses will override this method and log errors themselves.
093    *
094    * <p>
095    * The default implementation simply logs errors to the <c>RestServlet</c> logger.
096    *
097    * <p>
098    * Here's a typical implementation showing how stack trace hashing can be used to reduce log file sizes...
099    * <p class='bcode w800'>
100    *    <jk>protected void</jk> onError(HttpServletRequest req, HttpServletResponse res, RestException e, <jk>boolean</jk> noTrace) {
101    *       String qs = req.getQueryString();
102    *       String msg = <js>"HTTP "</js> + req.getMethod() + <js>" "</js> + e.getStatus() + <js>" "</js> + req.getRequestURI() + (qs == <jk>null</jk> ? <js>""</js> : <js>"?"</js> + qs);
103    *       <jk>int</jk> c = e.getOccurrence();
104    *
105    *       <jc>// REST_useStackTraceHashes is disabled, so we have to log the exception every time.</jc>
106    *       <jk>if</jk> (c == 0)
107    *          myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%s] %s"</js>, e.getStatus(), msg), e);
108    *
109    *       <jc>// This is the first time we've countered this error, so log a stack trace
110    *       // unless ?noTrace was passed in as a URL parameter.</jc>
111    *       <jk>else if</jk> (c == 1 &amp;&amp; ! noTrace)
112    *          myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s"</js>, e.hashCode(), e.getStatus(), c, msg), e);
113    *
114    *       <jc>// This error occurred before.
115    *       // Only log the message, not the stack trace.</jc>
116    *       <jk>else</jk>
117    *          myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s, %s"</js>, e.hashCode(), e.getStatus(), c, msg, e.getLocalizedMessage()));
118    *    }
119    * </p>
120    *
121    * @param req The servlet request object.
122    * @param res The servlet response object.
123    * @param e Exception indicating what error occurred.
124    */
125   public void onError(HttpServletRequest req, HttpServletResponse res, RestException e);
126}