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