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