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 * <h5 class='section'>See Also:</h5> 026 * <ul> 027 * <li class='jf'>{@link RestContext#REST_logger} 028 * <li class='link'>{@doc juneau-rest-server.LoggingAndErrorHandling} 029 * </ul> 030 */ 031public interface RestLogger { 032 033 /** 034 * Represents no RestLogger. 035 * 036 * <p> 037 * Used on annotation to indicate that the value should be inherited from the parent class, and 038 * ultimately {@link BasicRestLogger} if not specified at any level. 039 */ 040 public interface Null extends RestLogger {} 041 042 /** 043 * Sets the logging level for this logger. 044 * 045 * @param level The new level. 046 */ 047 public void setLevel(Level level); 048 049 /** 050 * Log a message to the logger. 051 * 052 * @param level The log level. 053 * @param cause The cause. 054 * @param msg The message to log. 055 * @param args Optional {@link MessageFormat}-style arguments. 056 */ 057 public void log(Level level, Throwable cause, String msg, Object...args); 058 059 /** 060 * Log a message. 061 * 062 * @param level The log level. 063 * @param msg The message to log. 064 * @param args Optional {@link MessageFormat}-style arguments. 065 */ 066 public void log(Level level, String msg, Object...args); 067 068 /** 069 * Same as {@link #log(Level, String, Object...)} excepts runs the arguments through {@link SimpleJsonSerializer#DEFAULT_READABLE}. 070 * 071 * <p> 072 * Serialization of arguments do not occur if message is not logged, so it's safe to use this method from within 073 * debug log statements. 074 * 075 * <h5 class='section'>Example:</h5> 076 * <p class='bcode w800'> 077 * logObjects(<jsf>DEBUG</jsf>, <js>"Pojo contents:\n{0}"</js>, myPojo); 078 * </p> 079 * 080 * @param level The log level. 081 * @param msg The message to log. 082 * @param args Optional {@link MessageFormat}-style arguments. 083 */ 084 public void logObjects(Level level, String msg, Object...args); 085 086 /** 087 * Callback method for logging errors during HTTP requests. 088 * 089 * <p> 090 * Typically, subclasses will override this method and log errors themselves. 091 * 092 * <p> 093 * The default implementation simply logs errors to the <code>RestServlet</code> logger. 094 * 095 * <p> 096 * Here's a typical implementation showing how stack trace hashing can be used to reduce log file sizes... 097 * <p class='bcode w800'> 098 * <jk>protected void</jk> onError(HttpServletRequest req, HttpServletResponse res, RestException e, <jk>boolean</jk> noTrace) { 099 * String qs = req.getQueryString(); 100 * String msg = <js>"HTTP "</js> + req.getMethod() + <js>" "</js> + e.getStatus() + <js>" "</js> + req.getRequestURI() + (qs == <jk>null</jk> ? <js>""</js> : <js>"?"</js> + qs); 101 * <jk>int</jk> c = e.getOccurrence(); 102 * 103 * <jc>// REST_useStackTraceHashes is disabled, so we have to log the exception every time.</jc> 104 * <jk>if</jk> (c == 0) 105 * myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%s] %s"</js>, e.getStatus(), msg), e); 106 * 107 * <jc>// This is the first time we've countered this error, so log a stack trace 108 * // unless ?noTrace was passed in as a URL parameter.</jc> 109 * <jk>else if</jk> (c == 1 && ! noTrace) 110 * myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s"</js>, e.hashCode(), e.getStatus(), c, msg), e); 111 * 112 * <jc>// This error occurred before. 113 * // Only log the message, not the stack trace.</jc> 114 * <jk>else</jk> 115 * myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s, %s"</js>, e.hashCode(), e.getStatus(), c, msg, e.getLocalizedMessage())); 116 * } 117 * </p> 118 * 119 * @param req The servlet request object. 120 * @param res The servlet response object. 121 * @param e Exception indicating what error occurred. 122 */ 123 public void onError(HttpServletRequest req, HttpServletResponse res, RestException e); 124}