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.logger;
014
015import static java.util.logging.Level.*;
016import static org.apache.juneau.rest.logger.CallLoggingDetail.*;
017
018import org.apache.juneau.cp.*;
019import org.apache.juneau.rest.*;
020
021/**
022 * Basic implementation of a call logger.
023 *
024 * <h5 class='section'>Configured Settings:</h5>
025 * <ul>
026 *    <li>Logs to the {@link RestContext#getLogger() context logger}.
027 *    <li>Only calls with status code &gt;=400 will be logged.
028 *    <li>Logs full request and response entity.
029 * </ul>
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.LoggingAndDebugging">Logging / Debugging</a>
033 * </ul>
034 */
035public class BasicCallLogger extends CallLogger {
036
037   /**
038    * Constructor using specific settings.
039    *
040    * @param beanStore The bean store containing injectable beans for this logger.
041    */
042   public BasicCallLogger(BeanStore beanStore) {
043      super(beanStore);
044   }
045
046   /**
047    * Constructor using default settings.
048    * <p>
049    * Uses the same settings as {@link CallLogger}.
050    */
051   public BasicCallLogger() {
052      super(BeanStore.INSTANCE);
053   }
054
055   @Override
056   protected Builder init(BeanStore beanStore) {
057      return super.init(beanStore)
058         .normalRules(  // Rules when debugging is not enabled.
059            CallLoggerRule.create(beanStore)  // Log 500+ errors with status-line and header information.
060               .statusFilter(x -> x >= 500)
061               .level(SEVERE)
062               .requestDetail(HEADER)
063               .responseDetail(HEADER)
064               .build(),
065            CallLoggerRule.create(beanStore)  // Log 400-500 errors with just status-line information.
066               .statusFilter(x -> x >= 400)
067               .level(WARNING)
068               .requestDetail(STATUS_LINE)
069               .responseDetail(STATUS_LINE)
070               .build()
071         )
072         .debugRules(  // Rules when debugging is enabled.
073            CallLoggerRule.create(beanStore)  // Log everything with full details.
074               .level(SEVERE)
075               .requestDetail(ENTITY)
076               .responseDetail(ENTITY)
077               .build()
078         )
079      ;
080   }
081}