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