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.io.IOException;
21 import com.enea.jcarder.util.logging.Logger.Level;
24 * This class acts as a log handler for classes that that implement the
25 * Appendable interface.
27 * The Appendable does not need to be thread-safe; AppendableHandler
28 * synchronizes calls to Appendable's methods.
30 public class AppendableHandler implements Handler {
31 private final Appendable mDestination;
32 private final Level mLevel;
33 private final String mMessageFormat;
38 * The default log level is FINEST and the default message format is
39 * "{level}: {message}\n"
42 * Destination of the log messages.
44 public AppendableHandler(Appendable destination) {
45 this(destination, Logger.Level.FINEST);
51 * The default message format is "{level}: {message}\n"
54 * Destination of the log messages.
58 public AppendableHandler(Appendable destination, Logger.Level logLevel) {
59 this(destination, logLevel, "{level}: {message}\n");
65 * Substrings like {keyword} are expanded in the message format string.
67 * Currently supported keywords:
69 * - {message} -- the message
70 * - {level} -- the log level
72 * @param destination Destination of the log messages.
73 * @param logLevel Log level.
74 * @param messageFormat Message format.
76 public AppendableHandler(Appendable destination,
77 Logger.Level logLevel,
78 String messageFormat) {
79 mDestination = destination;
81 mMessageFormat = messageFormat;
85 public void publish(Level level, String message) {
86 if (level.compareTo(mLevel) <= 0) {
88 String formattedMessage = mMessageFormat
89 .replace("{level}", level.toString())
90 .replace("{message}", message);
91 synchronized (mDestination) {
92 mDestination.append(formattedMessage);
94 } catch (IOException e) {