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.microservice;
018
019import java.util.*;
020import java.util.logging.*;
021import java.util.logging.Formatter;
022
023import org.apache.juneau.microservice.resources.*;
024
025/**
026 * Can be used for configuration of simple logging in the microservice.
027 *
028 * <p>
029 * Instances of this class can be created using {@link #create()} and passing the result to
030 * {@link Microservice.Builder#logConfig(LogConfig)}.
031 *
032 * <p>
033 * These values override values specified in the <js>"Logging"</js> configuration section.
034 */
035public class LogConfig {
036
037   String logFile, logDir;
038   Boolean append;
039   Integer limit, count;
040   Level fileLevel, consoleLevel;
041   Map<String,Level> levels = new LinkedHashMap<>();
042   Formatter formatter;
043
044   LogConfig() {}
045
046   /**
047    * Copy constructor.
048    *
049    * @param copyFrom The log config to copy from.
050    */
051   protected LogConfig(LogConfig copyFrom) {
052      this.logFile = copyFrom.logFile;
053      this.logDir = copyFrom.logDir;
054      this.append = copyFrom.append;
055      this.limit = copyFrom.limit;
056      this.count = copyFrom.count;
057      this.fileLevel = copyFrom.fileLevel;
058      this.consoleLevel = copyFrom.consoleLevel;
059      this.levels = new LinkedHashMap<>(copyFrom.levels);
060      this.formatter = copyFrom.formatter;
061   }
062
063   /**
064    * Creates a copy of this log configuration.
065    *
066    * @return A new copy of this log configuration.
067    */
068   public LogConfig copy() {
069      return new LogConfig(this);
070   }
071
072   /**
073    * Creates a new instance of this config.
074    *
075    * @return A new instance of this config.
076    */
077   public static LogConfig create() {
078      return new LogConfig();
079   }
080
081   /**
082    * Returns the name of the log file on the file system to store the log file for this microservice.
083    *
084    * <p>
085    * This overrides the configuration value <js>"Logging/logFile"</js>.
086    * If not specified, no file system logging will be used.
087    *
088    * @param logFile The log file.
089    * @return This object (for method chaining).
090    */
091   public LogConfig logFile(String logFile) {
092      this.logFile = logFile;
093      return this;
094   }
095
096   /**
097    * The location of the log directory to create the log file.
098    *
099    * <p>
100    * This overrides the configuration value <js>"Logging/logDir"</js>.
101    * If not specified, uses the JVM working directory.
102    *
103    * @param logDir The log directory location as a path relative to the working directory.
104    * @return This object (for method chaining).
105    */
106   public LogConfig logDir(String logDir) {
107      this.logDir = logDir;
108      return this;
109   }
110
111   /**
112    * The log entry formatter.
113    *
114    * <p>
115    * If not specified, uses the following values pulled from the configuration to construct a {@link LogEntryFormatter}:
116    * <ul>
117    *    <li><js><js>"Logging/format"</js> (default is <js>"[{date} {level}] {msg}%n"</js>)
118    *    <li><js><js>"Logging/dateFormat"</js> (default is <js>"yyyy.MM.dd hh:mm:ss"</js>)
119    *    <li><js><js>"Logging/useStackTraceHashes"</js> (default is <jk>false</jk>)
120    * </ul>
121    *
122    *
123    * @param formatter The log entry formatter.
124    * @return This object (for method chaining).
125    * @see LogEntryFormatter
126    */
127   public LogConfig formatter(Formatter formatter) {
128      this.formatter = formatter;
129      return this;
130   }
131
132   /**
133    * Specified append mode for the log file.
134    *
135    * @return This object (for method chaining).
136    */
137   public LogConfig append() {
138      this.append = true;
139      return this;
140   }
141
142   /**
143    * The maximum number of bytes to write to any one log file.
144    *
145    * @param limit The number of bytes.
146    * @return This object (for method chaining).
147    */
148   public LogConfig limit(int limit) {
149      this.limit = limit;
150      return this;
151   }
152
153   /**
154    * The number of log files to use.
155    *
156    * @param count The number of log files.
157    * @return This object (for method chaining).
158    */
159   public LogConfig count(int count) {
160      this.count = count;
161      return this;
162   }
163
164   /**
165    * The default logging level for the log file.
166    *
167    * @param fileLevel The logging level.
168    * @return This object (for method chaining).
169    */
170   public LogConfig fileLevel(Level fileLevel) {
171      this.fileLevel = fileLevel;
172      return this;
173   }
174
175   /**
176    * The default logging level for the console.
177    *
178    * @param consoleLevel The logging level.
179    * @return This object (for method chaining).
180    */
181   public LogConfig consoleLevel(Level consoleLevel) {
182      this.consoleLevel = consoleLevel;
183      return this;
184   }
185
186   /**
187    * Default logging levels for loggers.
188    *
189    * @param levels A map of logger names to logger levels.
190    * @return This object (for method chaining).
191    */
192   public LogConfig levels(Map<String,Level> levels) {
193      this.levels.putAll(levels);
194      return this;
195   }
196
197   /**
198    * Default logging level for logger.
199    *
200    * @param logger Logger name.
201    * @param level Logger level.
202    * @return This object (for method chaining).
203    */
204   public LogConfig level(String logger, Level level) {
205      this.levels.put(logger, level);
206      return this;
207   }
208}