1 package org.apache.torque.generator.configuration.controller; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.log4j.Level; 25 import org.apache.log4j.Logger; 26 27 /** 28 * The possible log levels. This implementation uses log4j internally, 29 * but the interface is agnostic of the logging framework. 30 */ 31 public enum Loglevel 32 { 33 /** Loglevel trace. */ 34 TRACE("trace", Level.TRACE), 35 /** Loglevel debug. */ 36 DEBUG("debug", Level.DEBUG), 37 /** Loglevel info. */ 38 INFO("info", Level.INFO), 39 /** Loglevel warn. */ 40 WARN("warn", Level.WARN), 41 /** Loglevel error. */ 42 ERROR("error", Level.ERROR); 43 44 /** The key of the loglevel. */ 45 private String key; 46 47 /** The log4j loglevel. */ 48 private Level log4jLevel; 49 50 /** The logger. */ 51 private static Log log = LogFactory.getLog(Loglevel.class); 52 53 /** 54 * Constructor. 55 * 56 * @param key the key, not null. 57 */ 58 private Loglevel(String key, Level log4jLevel) 59 { 60 this.key = key; 61 this.log4jLevel = log4jLevel; 62 } 63 64 /** 65 * Returns the key of the Loglevel. 66 * 67 * @return the key of the Loglevel, not null. 68 */ 69 public String getKey() 70 { 71 return key; 72 } 73 74 /** 75 * Applies the log level. 76 */ 77 public void apply() 78 { 79 if (Logger.getRootLogger().getLevel() != log4jLevel) 80 { 81 log.info("apply() : Setting loglevel to " + this); 82 Logger.getRootLogger().setLevel(log4jLevel); 83 } 84 } 85 86 /** 87 * Returns the Loglevel for a given key. 88 * 89 * @param key the key to look for. 90 * 91 * @return the corresponding Loglevel, not null. 92 * 93 * @throws IllegalArgumentException if no Loglevel can be found 94 * for the key. 95 */ 96 public static Loglevel getByKey(String key) 97 { 98 for (Loglevel loglevel : values()) 99 { 100 if (loglevel.getKey().equals(key)) 101 { 102 return loglevel; 103 } 104 } 105 throw new IllegalArgumentException( 106 "Key " + key + " does not exist"); 107 } 108 109 /** 110 * Returns the current loglevel. 111 * 112 * @return the current loglevel, or INFO if the current loglevel cannot 113 * be determined. 114 */ 115 public static Loglevel getCurrentLoglevel() 116 { 117 Level level = Logger.getRootLogger().getLevel(); 118 for (Loglevel loglevel : values()) 119 { 120 if (loglevel.log4jLevel.equals(level)) 121 { 122 return loglevel; 123 } 124 } 125 return INFO; 126 } 127 }