blob: d62c1595bb060e5a07e0bdf55a38c4417a4fe83e [file] [log] [blame]
// ***************************************************************************************************************************
// * 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. *
// ***************************************************************************************************************************
package org.apache.juneau.rest;
import java.text.*;
import java.util.logging.*;
import javax.servlet.http.*;
import org.apache.juneau.json.*;
/**
* Logging utility class.
*
* <h5 class='section'>See Also:</h5>
* <ul>
* <li class='jf'>{@link RestContext#REST_logger}
* <li class='link'>{@doc juneau-rest-server.LoggingAndErrorHandling}
* </ul>
*/
public interface RestLogger {
/**
* Represents no RestLogger.
*
* <p>
* Used on annotation to indicate that the value should be inherited from the parent class, and
* ultimately {@link BasicRestLogger} if not specified at any level.
*/
public interface Null extends RestLogger {}
/**
* Sets the logging level for this logger.
*
* @param level The new level.
*/
public void setLevel(Level level);
/**
* Log a message to the logger.
*
* @param level The log level.
* @param cause The cause.
* @param msg The message to log.
* @param args Optional {@link MessageFormat}-style arguments.
*/
public void log(Level level, Throwable cause, String msg, Object...args);
/**
* Log a message.
*
* @param level The log level.
* @param msg The message to log.
* @param args Optional {@link MessageFormat}-style arguments.
*/
public void log(Level level, String msg, Object...args);
/**
* Same as {@link #log(Level, String, Object...)} excepts runs the arguments through {@link SimpleJsonSerializer#DEFAULT_READABLE}.
*
* <p>
* Serialization of arguments do not occur if message is not logged, so it's safe to use this method from within
* debug log statements.
*
* <h5 class='section'>Example:</h5>
* <p class='bcode w800'>
* logObjects(<jsf>DEBUG</jsf>, <js>"Pojo contents:\n{0}"</js>, myPojo);
* </p>
*
* @param level The log level.
* @param msg The message to log.
* @param args Optional {@link MessageFormat}-style arguments.
*/
public void logObjects(Level level, String msg, Object...args);
/**
* Callback method for logging errors during HTTP requests.
*
* <p>
* Typically, subclasses will override this method and log errors themselves.
*
* <p>
* The default implementation simply logs errors to the <c>RestServlet</c> logger.
*
* <p>
* Here's a typical implementation showing how stack trace hashing can be used to reduce log file sizes...
* <p class='bcode w800'>
* <jk>protected void</jk> onError(HttpServletRequest req, HttpServletResponse res, RestException e, <jk>boolean</jk> noTrace) {
* String qs = req.getQueryString();
* String msg = <js>"HTTP "</js> + req.getMethod() + <js>" "</js> + e.getStatus() + <js>" "</js> + req.getRequestURI() + (qs == <jk>null</jk> ? <js>""</js> : <js>"?"</js> + qs);
* <jk>int</jk> c = e.getOccurrence();
*
* <jc>// REST_useStackTraceHashes is disabled, so we have to log the exception every time.</jc>
* <jk>if</jk> (c == 0)
* myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%s] %s"</js>, e.getStatus(), msg), e);
*
* <jc>// This is the first time we've countered this error, so log a stack trace
* // unless ?noTrace was passed in as a URL parameter.</jc>
* <jk>else if</jk> (c == 1 &amp;&amp; ! noTrace)
* myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s"</js>, e.hashCode(), e.getStatus(), c, msg), e);
*
* <jc>// This error occurred before.
* // Only log the message, not the stack trace.</jc>
* <jk>else</jk>
* myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s, %s"</js>, e.hashCode(), e.getStatus(), c, msg, e.getLocalizedMessage()));
* }
* </p>
*
* @param req The servlet request object.
* @param res The servlet response object.
* @param e Exception indicating what error occurred.
*/
public void onError(HttpServletRequest req, HttpServletResponse res, RestException e);
/**
* Called at the end of a servlet request to log the request.
*
* @param req The servlet request.
* @param res The servlet response.
*/
public void log(HttpServletRequest req, HttpServletResponse res);
}