Hierarchy.java 5.98 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.jivesoftware.util.log;

import org.jivesoftware.util.log.format.PatternFormatter;
import org.jivesoftware.util.log.output.io.StreamTarget;
import org.jivesoftware.util.log.util.DefaultErrorHandler;

/**
 * This class encapsulates a basic independent log hierarchy.
 * The hierarchy is essentially a safe wrapper around root logger.
 *
 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
 */
public class Hierarchy {
    ///Format of default formatter
    private static final String FORMAT =
            "%7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): %{message}\\n%{throwable}";

    ///The instance of default hierarchy
    private static final Hierarchy c_hierarchy = new Hierarchy();

    ///Error Handler associated with hierarchy
    private ErrorHandler m_errorHandler;

    ///The root logger which contains all Loggers in this hierarchy
    private Logger m_rootLogger;

    /**
     * Retrieve the default hierarchy.
     * <p/>
     * <p>In most cases the default LogHierarchy is the only
     * one used in an application. However when security is
     * a concern or multiple independent applications will
     * be running in same JVM it is advantageous to create
     * new Hierarchies rather than reuse default.</p>
     *
     * @return the default Hierarchy
     */
    public static Hierarchy getDefaultHierarchy() {
        return c_hierarchy;
    }

    /**
     * Create a hierarchy object.
     * The default LogTarget writes to stdout.
     */
    public Hierarchy() {
        m_errorHandler = new DefaultErrorHandler();
        m_rootLogger = new Logger(new InnerErrorHandler(), "", null, null);

        //Setup default output target to print to console
        final PatternFormatter formatter = new PatternFormatter(FORMAT);
        final StreamTarget target = new StreamTarget(System.out, formatter);

        setDefaultLogTarget(target);
    }

    /**
     * Set the default log target for hierarchy.
     * This is the target inherited by loggers if no other target is specified.
     *
     * @param target the default target
     */
    public void setDefaultLogTarget(final LogTarget target) {
        if (null == target) {
            throw new IllegalArgumentException("Can not set DefaultLogTarget to null");
        }

        final LogTarget[] targets = new LogTarget[]{target};
        getRootLogger().setLogTargets(targets);
    }

    /**
     * Set the default log targets for this hierarchy.
     * These are the targets inherited by loggers if no other targets are specified
     *
     * @param targets the default targets
     */
    public void setDefaultLogTargets(final LogTarget[] targets) {
        if (null == targets || 0 == targets.length) {
            throw new IllegalArgumentException("Can not set DefaultLogTargets to null");
        }

        for (int i = 0; i < targets.length; i++) {
            if (null == targets[i]) {
                throw new IllegalArgumentException("Can not set DefaultLogTarget element to null");
            }
        }

        getRootLogger().setLogTargets(targets);
    }

    /**
     * Set the default priority for hierarchy.
     * This is the priority inherited by loggers if no other priority is specified.
     *
     * @param priority the default priority
     */
    public void setDefaultPriority(final Priority priority) {
        if (null == priority) {
            throw new IllegalArgumentException("Can not set default Hierarchy Priority to null");
        }

        getRootLogger().setPriority(priority);
    }

    /**
     * Set the ErrorHandler associated with hierarchy.
     *
     * @param errorHandler the ErrorHandler
     */
    public void setErrorHandler(final ErrorHandler errorHandler) {
        if (null == errorHandler) {
            throw new IllegalArgumentException("Can not set default Hierarchy ErrorHandler to null");
        }

        m_errorHandler = errorHandler;
    }

    /**
     * Retrieve a logger for named category.
     *
     * @param category the context
     * @return the Logger
     */
    public Logger getLoggerFor(final String category) {
        return getRootLogger().getChildLogger(category);
    }

//    /**
//     * Logs an error message to error handler.
//     * Default Error Handler is stderr.
//     *
//     * @param message a message to log
//     * @param throwable a Throwable to log
//     * @deprecated Logging components should use ErrorHandler rather than Hierarchy.log()
//     */
//    public void log(final String message, final Throwable throwable) {
//        m_errorHandler.error(message, throwable, null);
//    }
//
//    /**
//     * Logs an error message to error handler.
//     * Default Error Handler is stderr.
//     *
//     * @param message a message to log
//     * @deprecated Logging components should use ErrorHandler rather than Hierarchy.log()
//     */
//    public void log(final String message) {
//        log(message, null);
//    }

    private class InnerErrorHandler
            implements ErrorHandler {
        /**
         * Log an unrecoverable error.
         *
         * @param message   the error message
         * @param throwable the exception associated with error (may be null)
         * @param event     the LogEvent that caused error, if any (may be null)
         */
        public void error(final String message,
                          final Throwable throwable,
                          final LogEvent event) {
            m_errorHandler.error(message, throwable, event);
        }
    }

    /**
     * Utility method to retrieve logger for hierarchy.
     * This method is intended for use by sub-classes
     * which can take responsibility for manipulating
     * Logger directly.
     *
     * @return the Logger
     */
    protected final Logger getRootLogger() {
        return m_rootLogger;
    }
}