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'><a class="doclink" href="../../../../overview-summary.html#juneau-rest-server.LoggingAndErrorHandling">Overview > juneau-rest-server > Logging and Error Handling</a> 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 * Log a message to the logger. 044 * 045 * @param level The log level. 046 * @param cause The cause. 047 * @param msg The message to log. 048 * @param args Optional {@link MessageFormat}-style arguments. 049 */ 050 public void log(Level level, Throwable cause, String msg, Object...args); 051 052 /** 053 * Log a message. 054 * 055 * @param level The log level. 056 * @param msg The message to log. 057 * @param args Optional {@link MessageFormat}-style arguments. 058 */ 059 public void log(Level level, String msg, Object...args); 060 061 /** 062 * Same as {@link #log(Level, String, Object...)} excepts runs the arguments through {@link JsonSerializer#DEFAULT_LAX_READABLE}. 063 * 064 * <p> 065 * Serialization of arguments do not occur if message is not logged, so it's safe to use this method from within 066 * debug log statements. 067 * 068 * <h5 class='section'>Example:</h5> 069 * <p class='bcode'> 070 * logObjects(<jsf>DEBUG</jsf>, <js>"Pojo contents:\n{0}"</js>, myPojo); 071 * </p> 072 * 073 * @param level The log level. 074 * @param msg The message to log. 075 * @param args Optional {@link MessageFormat}-style arguments. 076 */ 077 public void logObjects(Level level, String msg, Object...args); 078 079 /** 080 * Callback method for logging errors during HTTP requests. 081 * 082 * <p> 083 * Typically, subclasses will override this method and log errors themselves. 084 * 085 * <p> 086 * The default implementation simply logs errors to the <code>RestServlet</code> logger. 087 * 088 * <p> 089 * Here's a typical implementation showing how stack trace hashing can be used to reduce log file sizes... 090 * <p class='bcode'> 091 * <jk>protected void</jk> onError(HttpServletRequest req, HttpServletResponse res, RestException e, <jk>boolean</jk> noTrace) { 092 * String qs = req.getQueryString(); 093 * String msg = <js>"HTTP "</js> + req.getMethod() + <js>" "</js> + e.getStatus() + <js>" "</js> + req.getRequestURI() + (qs == <jk>null</jk> ? <js>""</js> : <js>"?"</js> + qs); 094 * <jk>int</jk> c = e.getOccurrence(); 095 * 096 * <jc>// REST_useStackTraceHashes is disabled, so we have to log the exception every time.</jc> 097 * <jk>if</jk> (c == 0) 098 * myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%s] %s"</js>, e.getStatus(), msg), e); 099 * 100 * <jc>// This is the first time we've countered this error, so log a stack trace 101 * // unless ?noTrace was passed in as a URL parameter.</jc> 102 * <jk>else if</jk> (c == 1 && ! noTrace) 103 * myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s"</js>, e.hashCode(), e.getStatus(), c, msg), e); 104 * 105 * <jc>// This error occurred before. 106 * // Only log the message, not the stack trace.</jc> 107 * <jk>else</jk> 108 * myLogger.log(Level.<jsf>WARNING</jsf>, <jsm>format</jsm>(<js>"[%h.%s.%s] %s, %s"</js>, e.hashCode(), e.getStatus(), c, msg, e.getLocalizedMessage())); 109 * } 110 * </p> 111 * 112 * @param req The servlet request object. 113 * @param res The servlet response object. 114 * @param e Exception indicating what error occurred. 115 */ 116 public void onError(HttpServletRequest req, HttpServletResponse res, RestException e); 117}