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