2 * JCarder -- cards Java programs to keep threads disentangled
4 * Copyright (C) 2006-2007 Enea AB
5 * Copyright (C) 2007 Ulrik Svensson
6 * Copyright (C) 2007 Joel Rosdahl
8 * This program is made available under the GNU GPL version 2, with a special
9 * exception for linking with JUnit. See the accompanying file LICENSE.txt for
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.
17 package com.enea.jcarder.util.logging;
19 import java.util.ArrayList;
20 import java.util.Collection;
23 * A simple logging framework.
25 * We use our own simple Logging framework for several reasons:
26 * - To avoid interfering with logging from the user application. Even if we
27 * were using Log4J instead of java.util.Logging.*, JCarder might interfere with
28 * for example Log4J system properties.
29 * - The default java.util.logging.LogManager is reset by a shutdown hook and
30 * there is no fixed order in which the shutdown hooks are executed.
31 * - Minimizing the usage of the Java standard library improves performance and
32 * minimizes the risk of deadlock if the standard library is instrumented by
35 public final class Logger {
36 public static enum Level {
46 * Parse a string and return the corresponding log level.
48 * @param string The string to parse.
51 public static Level fromString(String string) {
52 for (Level level : values()) {
53 if (string.equalsIgnoreCase(level.toString())) {
60 public static String getEnumeration() {
61 StringBuffer sb = new StringBuffer();
63 for (Level level : values()) {
69 sb.append(level.toString());
75 final Collection<Handler> mHandlers;
79 * Constructor setting default value of log level.
81 * The default is to log everything, i.e., log level FINEST.
83 * @param handlers Log handlers. null means no handlers.
85 public Logger(Collection<Handler> handlers) {
86 this(handlers, Level.FINEST);
92 * @param handlers Log handlers. null means no handlers.
93 * @param logLevel Log level.
95 public Logger(Collection<Handler> handlers, Level logLevel) {
97 mHandlers = new ArrayList<Handler>();
98 if (handlers != null) {
99 // Create a copy of the provided collection instead of sharing
100 // it, in order to make sure that mHandlers is immutable and
102 mHandlers.addAll(handlers);
107 * Check whether a message with a certain level would be logged.
109 * @param level The level.
110 * @return True if the message would be logged, otherwise false.
112 public boolean isLoggable(Level level) {
113 return level.compareTo(mLevel) <= 0;
117 * Log a message with level SEVERE.
119 * @param message The message.
121 public void severe(String message) {
122 publishLog(Level.SEVERE, message);
127 * Log a message with level WARNING.
129 * @param message The message.
131 public void warning(String message) {
132 publishLog(Level.WARNING, message);
137 * Log a message with level INFO.
139 * @param message The message.
141 public void info(String message) {
142 publishLog(Level.INFO, message);
147 * Log a message with level CONFIG.
149 * @param message The message.
151 public void config(String message) {
152 publishLog(Level.CONFIG, message);
157 * Log a message with level FINE.
159 * @param message The message.
161 public void fine(String message) {
162 publishLog(Level.FINE, message);
167 * Log a message with level FINER.
169 * @param message The message.
171 public void finer(String message) {
172 publishLog(Level.FINER, message);
176 * Log a message with level FINEST.
178 * @param message The message.
180 public void finest(String message) {
181 publishLog(Level.FINEST, message);
184 private void publishLog(Level level, String message) {
185 if (isLoggable(level)) {
186 for (Handler handler : mHandlers) {
187 handler.publish(level, message);