001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.common.logging;
020
021import java.net.URL;
022
023/**
024 * Helper for log4j.
025 *
026 * @since 9.2
027 */
028public class Log4JHelper {
029
030    // utility class
031    private Log4JHelper() {
032    }
033
034    /**
035     * Calls log4j's DOMConfigurator.configureAndWatch method, if available, to automatically watch and reload the
036     * configuration file if needed.
037     *
038     * @param delay
039     *            the delay (in milliseconds)
040     * @return {@code true} if log4j is available and the call succeeded
041     */
042    public static Log4jWatchdogHandle configureAndWatch(long delay) {
043        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
044        URL url = contextClassLoader.getResource("log4j.xml");
045        if (url == null) {
046            return null;
047        }
048        try {
049            return Log4jWatchdog.watch(url.getFile(), delay);
050        } catch (LinkageError cause) {
051            return null;
052        }
053    }
054
055}