All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ops4j.pax.logging.log4j2.appender.PaxOsgiAppender Maven / Gradle / Ivy

Go to download

Pax Logging backend implementation based on Apache Log4J2. It provides Log4J2 specific implementation of PaxLoggingService interface and Log4J2 specific configuration methods. Users may customize Log4J2 behaviour (appenders, layouts) by creating fragment attached to this bundle.

There is a newer version: 2.3.0
Show newest version
/*
 * Copyright 2011 Avid Technology, Inc.
 *
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ops4j.pax.logging.log4j2.appender;

import java.io.Serializable;

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.ErrorHandler;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.status.StatusLogger;
import org.ops4j.pax.logging.log4j2.internal.PaxAppenderProxy;
import org.ops4j.pax.logging.log4j2.internal.PaxLoggingEventImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

/**
 * 

* This is a Logback appender that forwards log messages to any services registered with OSGi with the interface * org.ops4j.pax.logging.spi.PaxAppender. That list of appender services is possibly filtered by the filter setting. *

*/ @Plugin(name = "PaxOsgi", category = "Core", elementType = "appender", printObject = true) public class PaxOsgiAppender implements Appender { private final String name; private final String filter; private final Object lifeCycleLock = new Object(); private PaxAppenderProxy proxy; private volatile boolean started; public PaxOsgiAppender(String name, String filter) { this.name = name; this.filter = (filter == null || filter.isEmpty()) ? "*" : filter; } @Override public String getName() { return name; } @Override public Layout getLayout() { return null; } @Override public boolean ignoreExceptions() { return true; } @Override public ErrorHandler getHandler() { return null; } @Override public void setHandler(ErrorHandler handler) { } @Override public boolean isStarted() { return started; } @Override public boolean isStopped() { return !started; } @Override public void start() { synchronized (lifeCycleLock) { if (isStarted()) return; // TODO: use correct bundle context BundleContext bundleContext = null; if (bundleContext == null) { Bundle bundle = FrameworkUtil.getBundle(getClass()); if (bundle != null) { bundleContext = bundle.getBundleContext(); } if (bundleContext == null) { throw new IllegalArgumentException("missing BundleContext, expected in org.ops4j.pax.logging.log4j2.bundlecontext"); } } started = true; proxy = new PaxAppenderProxy(bundleContext, filter); proxy.open(); } } @Override public void stop() { synchronized (lifeCycleLock) { if (!isStarted()) return; if (proxy != null) { proxy.close(); proxy = null; } started = false; } } @Override public void append(LogEvent event) { PaxAppenderProxy p = proxy; if (p != null) { p.doAppend(new PaxLoggingEventImpl(event)); } } /** * Create a Pax Osgi Appender. * @param name The name of the Appender. * @param filter defaults to "*", can be any string that works as a value in {@link org.osgi.framework.Filter} * @param config The Configuration * @return The FileAppender. */ @PluginFactory public static PaxOsgiAppender createAppender( // @formatter:off @PluginAttribute("name") final String name, @PluginAttribute("filter") final String filter, @PluginConfiguration final Configuration config) { // @formatter:on if (name == null) { StatusLogger.getLogger().error("No name provided for PaxOsgiAppender"); return null; } return new PaxOsgiAppender(name, filter); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy