001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.logger; 018 019import static java.util.logging.Level.*; 020import static org.apache.juneau.Enablement.*; 021import static org.apache.juneau.common.utils.Utils.*; 022import static org.apache.juneau.rest.logger.CallLoggingDetail.*; 023 024import org.apache.juneau.cp.*; 025import org.apache.juneau.rest.*; 026 027import jakarta.servlet.http.*; 028 029/** 030 * Default implementation of a {@link CallLogger} that only logs REST call errors unless no-log is enabled on the request. 031 * 032 * <p> 033 * Useful for REST tests where you know that a particular call is going to produce an error response and you don't want that 034 * response to be logged producing noisy test output. 035 * 036 * <p> 037 * Requests can be tagged as no-log (meaning don't log if there's an error) in any of the following ways: 038 * <ul> 039 * <li>A <js>"No-Trace: true"</js> header. 040 * <li>A <js>"noTrace=true"</js> query parameter. 041 * <li>A <js>"NoTrace"</js> request attribute with a string value of <js>"true"</js>. 042 * </ul> 043 * 044 * <h5 class='section'>Configured Settings:</h5> 045 * <ul> 046 * <li>Logs to the {@link RestContext#getLogger() context logger}. 047 * <li>Only calls with status code >=400 will be logged. 048 * <li>Logs full request and response entity. 049 * </ul> 050 * 051 * <h5 class='section'>See Also:</h5><ul> 052 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerLoggingAndDebugging">Logging / Debugging</a> 053 * </ul> 054 */ 055public class BasicTestCallLogger extends CallLogger { 056 057 /** 058 * Constructor. 059 * 060 * @param beanStore The bean store containing injectable beans for this logger. 061 */ 062 public BasicTestCallLogger(BeanStore beanStore) { 063 super(beanStore); 064 } 065 066 @Override 067 protected Builder init(BeanStore beanStore) { 068 return super.init(beanStore) 069 .normalRules( // Rules when debugging is not enabled. 070 CallLoggerRule.create(beanStore) // Log 500+ errors with status-line and header information. 071 .statusFilter(x -> x >= 400) 072 .level(SEVERE) 073 .requestDetail(HEADER) 074 .responseDetail(HEADER) 075 .enabled(CONDITIONAL) 076 .enabledPredicate(x -> ! isNoTrace(x)) // Only log if it's not a no-trace request. 077 .logStackTrace() 078 .build(), 079 CallLoggerRule.create(beanStore) // Log 400-500 errors with just status-line information. 080 .statusFilter(x -> x >= 400) 081 .level(WARNING) 082 .requestDetail(STATUS_LINE) 083 .responseDetail(STATUS_LINE) 084 .enabled(CONDITIONAL) 085 .enabledPredicate(x -> ! isNoTrace(x)) // Only log if it's not a no-trace request. 086 .logStackTrace() 087 .build() 088 ) 089 .debugRules( // Rules when debugging is enabled. 090 CallLoggerRule.create(beanStore) // Log everything with full details. 091 .level(SEVERE) 092 .requestDetail(ENTITY) 093 .responseDetail(ENTITY) 094 .logStackTrace() 095 .build() 096 ) 097 ; 098 } 099 100 private static boolean isNoTrace(HttpServletRequest req) { 101 Object o = req.getAttribute("NoTrace"); 102 if (o != null) 103 return "true".equalsIgnoreCase(o.toString()); 104 String s = req.getHeader("No-Trace"); 105 if (s != null) 106 return "true".equalsIgnoreCase(s); 107 return emptyIfNull(req.getQueryString()).contains("noTrace=true"); 108 } 109}