diff options
Diffstat (limited to 'org.eclipse.osee.logger.slf4j')
10 files changed, 439 insertions, 0 deletions
diff --git a/org.eclipse.osee.logger.slf4j/.classpath b/org.eclipse.osee.logger.slf4j/.classpath new file mode 100644 index 000000000..1fa3e6803 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/.classpath @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/org.eclipse.osee.logger.slf4j/.project b/org.eclipse.osee.logger.slf4j/.project new file mode 100644 index 000000000..af3299e9f --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/.project @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>org.eclipse.osee.logger.slf4j</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ManifestBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.SchemaBuilder</name> + <arguments> + </arguments> + </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ds.core.builder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.pde.PluginNature</nature> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/org.eclipse.osee.logger.slf4j/META-INF/MANIFEST.MF b/org.eclipse.osee.logger.slf4j/META-INF/MANIFEST.MF new file mode 100644 index 000000000..1081f5b85 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/META-INF/MANIFEST.MF @@ -0,0 +1,15 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Osee Logger Slf4j (Incubation) +Bundle-SymbolicName: org.eclipse.osee.logger.slf4j +Bundle-Version: 0.25.3.qualifier +Import-Package: org.eclipse.osee.framework.jdk.core.util, + org.eclipse.osee.logger, + org.osgi.framework, + org.osgi.service.component, + org.osgi.service.log, + org.slf4j +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Service-Component: OSGI-INF/*.xml +Bundle-Vendor: Eclipse Open System Engineering Environment +Bundle-ActivationPolicy: lazy diff --git a/org.eclipse.osee.logger.slf4j/OSGI-INF/log.impl.provider.xml b/org.eclipse.osee.logger.slf4j/OSGI-INF/log.impl.provider.xml new file mode 100644 index 000000000..72fdbd6c4 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/OSGI-INF/log.impl.provider.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="start" deactivate="stop" immediate="false" name="org.eclipse.osee.logger.slf4j.internal.Sfl4jLogImpl"> + <implementation class="org.eclipse.osee.logger.slf4j.internal.Sfl4jLogImpl"/> + + <service servicefactory="true"> + <provide interface="org.eclipse.osee.logger.Log"/> + </service> + +</scr:component> diff --git a/org.eclipse.osee.logger.slf4j/OSGI-INF/log.service.reader.xml b/org.eclipse.osee.logger.slf4j/OSGI-INF/log.service.reader.xml new file mode 100644 index 000000000..065107400 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/OSGI-INF/log.service.reader.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" enabled="true" immediate="true" name="org.eclipse.osee.logger.slf4j.internal.OsgiSlf4jLogger"> + <implementation class="org.eclipse.osee.logger.slf4j.internal.OsgiSlf4jLogger"/> + + <reference bind="setLoggingService" + cardinality="1..1" + interface="org.osgi.service.log.LogReaderService" + name="LogReaderService" + policy="static" + target="(objectClass=org.osgi.service.log.LogReaderService)"/> + + +</scr:component> diff --git a/org.eclipse.osee.logger.slf4j/build.properties b/org.eclipse.osee.logger.slf4j/build.properties new file mode 100644 index 000000000..c58ea2178 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/ diff --git a/org.eclipse.osee.logger.slf4j/pom.xml b/org.eclipse.osee.logger.slf4j/pom.xml new file mode 100644 index 000000000..e8b9a8af1 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/pom.xml @@ -0,0 +1,34 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.eclipse.osee</groupId> + <artifactId>org.eclipse.osee.x.core.parent</artifactId> + <version>0.25.3-SNAPSHOT</version> + <relativePath>../../plugins/org.eclipse.osee.x.core.parent</relativePath> + </parent> + + <artifactId>org.eclipse.osee.logger.slf4j</artifactId> + <packaging>eclipse-plugin</packaging> + <name>OSEE Logger SLF4J - (Incubation)</name> + + <build> + <!-- workaround for https://issues.sonatype.org/browse/TYCHO-168 --> + <resources> + <resource> + <directory>src</directory> + <excludes> + <exclude>**/*.java</exclude> + </excludes> + </resource> + </resources> + <plugins> + <plugin> + <groupId>org.eclipse.tycho</groupId> + <artifactId>tycho-source-plugin</artifactId> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file diff --git a/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/OsgiSlf4jLogger.java b/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/OsgiSlf4jLogger.java new file mode 100644 index 000000000..e37ebdb34 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/OsgiSlf4jLogger.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.logger.slf4j.internal; + +import org.osgi.service.log.LogListener; +import org.osgi.service.log.LogReaderService; + +/** + * @author Roberto E. Escobar + */ +public class OsgiSlf4jLogger { + + private LogListener listener; + private LogReaderService logService; + + public void setLoggingService(LogReaderService logService) { + this.logService = logService; + } + + public synchronized void activate() { + listener = new Slf4jLogListener(); + logService.addLogListener(listener); + } + + public synchronized void deactivate() { + logService.removeLogListener(listener); + listener = null; + logService = null; + } +} diff --git a/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/Sfl4jLogImpl.java b/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/Sfl4jLogImpl.java new file mode 100644 index 000000000..2795e86a6 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/Sfl4jLogImpl.java @@ -0,0 +1,207 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.logger.slf4j.internal; + +import org.eclipse.osee.logger.Log; +import org.osgi.framework.Bundle; +import org.osgi.service.component.ComponentContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; + +/** + * @author Roberto E. Escobar + */ +public class Sfl4jLogImpl implements Log { + + private static final int LOG_ERROR = 1; + private static final int LOG_WARNING = 2; + private static final int LOG_INFO = 3; + private static final int LOG_DEBUG = 4; + private static final int LOG_TRACE = 5; + + private Logger logger; + private Marker marker; + + public void start(ComponentContext context) { + if (logger == null) { + Bundle usingBundle = context.getUsingBundle(); + String symbolicName = null; + if (usingBundle != null) { + symbolicName = usingBundle.getSymbolicName(); + } else { + symbolicName = "Osee Log"; + } + logger = LoggerFactory.getLogger(symbolicName); + marker = MarkerFactory.getMarker(symbolicName); + debug("Logger setup for [%s]", logger.getName()); + } + } + + public void stop() { + logger = null; + marker = null; + } + + private Logger getLogger() { + return logger; + } + + @Override + public boolean isTraceEnabled() { + return isEnabled(LOG_TRACE); + } + + @Override + public void trace(String format, Object... args) { + trace(null, format, args); + } + + @Override + public void trace(Throwable th, String format, Object... args) { + logHelper(LOG_TRACE, th, format, args); + } + + @Override + public boolean isDebugEnabled() { + return isEnabled(LOG_DEBUG); + } + + @Override + public void debug(String format, Object... args) { + debug(null, format, args); + } + + @Override + public void debug(Throwable th, String format, Object... args) { + logHelper(LOG_DEBUG, th, format, args); + } + + @Override + public boolean isInfoEnabled() { + return isEnabled(LOG_INFO); + } + + @Override + public void info(String format, Object... args) { + info(null, format, args); + } + + @Override + public void info(Throwable th, String format, Object... args) { + logHelper(LOG_INFO, th, format, args); + } + + @Override + public boolean isWarnEnabled() { + return isEnabled(LOG_WARNING); + } + + @Override + public void warn(String format, Object... args) { + warn(null, format, args); + } + + @Override + public void warn(Throwable th, String format, Object... args) { + logHelper(LOG_WARNING, th, format, args); + } + + @Override + public boolean isErrorEnabled() { + return isEnabled(LOG_ERROR); + } + + @Override + public void error(String format, Object... args) { + error(null, format, args); + } + + @Override + public void error(Throwable th, String format, Object... args) { + logHelper(LOG_ERROR, th, format, args); + } + + private boolean isEnabled(int level) { + boolean result = false; + final Logger logger = getLogger(); + if (logger != null) { + switch (level) { + case LOG_DEBUG: + result = logger.isDebugEnabled(); + break; + case LOG_ERROR: + result = logger.isErrorEnabled(); + break; + case LOG_WARNING: + result = logger.isWarnEnabled(); + break; + case LOG_INFO: + result = logger.isInfoEnabled(); + break; + default: + result = logger.isTraceEnabled(); + break; + } + } + return result; + } + + private void logHelper(int level, Throwable th, String format, Object... args) { + logHelper(null, level, th, format, args); + } + + private void logHelper(Object context, int level, Throwable th, String format, Object... args) { + final Logger logger = getLogger(); + if (isEnabled(level)) { + String message = safeFormat(format, args); + if (message != null) { + switch (level) { + case LOG_DEBUG: + logger.debug(marker, message, th); + break; + case LOG_ERROR: + logger.error(marker, message, th); + break; + case LOG_WARNING: + logger.warn(marker, message, th); + break; + case LOG_INFO: + logger.info(marker, message, th); + break; + default: + logger.trace(marker, message, th); + break; + } + } + } + } + + private static String safeFormat(String message, Object... args) { + String toReturn; + try { + toReturn = String.format(message, args); + } catch (RuntimeException ex) { + StringBuilder builder = new StringBuilder(); + builder.append("Log message could not be formatted:"); + builder.append(message); + builder.append(" with the following arguments ["); + builder.append(org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", args)); + builder.append("]. Cause ["); + builder.append(ex.toString()); + builder.append("]"); + toReturn = builder.toString(); + } + return toReturn; + } + +} diff --git a/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/Slf4jLogListener.java b/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/Slf4jLogListener.java new file mode 100644 index 000000000..8b72c7c68 --- /dev/null +++ b/org.eclipse.osee.logger.slf4j/src/org/eclipse/osee/logger/slf4j/internal/Slf4jLogListener.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.logger.slf4j.internal; + +import org.osgi.service.log.LogEntry; +import org.osgi.service.log.LogListener; +import org.osgi.service.log.LogService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; + +/** + * @author Roberto E. Escobar + */ +public class Slf4jLogListener implements LogListener { + private static final Logger logger = LoggerFactory.getLogger(Slf4jLogListener.class); + private static final String[] remapMessages = new String[] {"Bundle Event", "Service Event"}; + + @Override + public void logged(LogEntry entry) { + try { + String symbolicName = entry.getBundle().getSymbolicName(); + Logger logger = LoggerFactory.getLogger(symbolicName); + Marker marker = MarkerFactory.getMarker(symbolicName); + logLogEntry(logger, entry.getLevel(), marker, entry.getMessage(), entry.getException()); + } catch (Exception ex) { + logger.error("Error during log listening", ex); + } + } + + private void logLogEntry(Logger logger, int level, Marker marker, String message, Throwable throwable) { + int theLevel = level; + if (throwable == null || isOSGIMessage(message)) { + theLevel = LogService.LOG_DEBUG; + } + String originalName = Thread.currentThread().getName(); + try { + Thread.currentThread().setName("Osgi Log Service"); + switch (theLevel) { + case LogService.LOG_DEBUG: + logger.debug(marker, message, throwable); + break; + case LogService.LOG_ERROR: + logger.error(marker, message, throwable); + break; + case LogService.LOG_WARNING: + logger.warn(marker, message, throwable); + break; + case LogService.LOG_INFO: + logger.info(marker, message, throwable); + break; + default: + logger.trace(marker, message, throwable); + break; + } + } finally { + Thread.currentThread().setName(originalName); + } + } + + private boolean isOSGIMessage(String message) { + for (String toCheck : remapMessages) { + if (message.startsWith(toCheck)) { + return true; + } + } + return false; + } +}
\ No newline at end of file |