001/*
002 * Copyright (C) 2019 Michael N. Lipp (http://www.mnl.de)
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
017package de.mnl.osgi.osgi2jul;
018
019import java.time.Instant;
020import java.util.concurrent.CountDownLatch;
021import java.util.logging.Level;
022import java.util.logging.LogRecord;
023import org.osgi.service.log.LogEntry;
024import org.osgi.service.log.LogListener;
025
026/**
027 * This class implements a LogListener that forwards the LogEntries
028 * to a handler.
029 */
030public class LogWriter implements LogListener {
031
032    private final Level auditLevel = new Level("AUDIT", Integer.MAX_VALUE) {
033        private static final long serialVersionUID = 1269723275384552686L;
034    };
035    private final ForwardingManager forwarder;
036    private final CountDownLatch enabled;
037
038    /**
039     * Instantiates a new log writer.
040     *
041     * @param forwarder the forwarder
042     * @param enabled the enabled
043     */
044    public LogWriter(ForwardingManager forwarder, CountDownLatch enabled) {
045        this.forwarder = forwarder;
046        this.enabled = enabled;
047    }
048
049    /*
050     * (non-Javadoc)
051     * 
052     * @see
053     * org.osgi.service.log.LogListener#logged(org.osgi.service.log.LogEntry)
054     */
055    @Override
056    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
057    public void logged(LogEntry entry) {
058        Level level = Level.OFF;
059        switch (entry.getLogLevel()) {
060        case TRACE:
061            level = Level.FINER;
062            break;
063        case DEBUG:
064            level = Level.FINE;
065            break;
066        case INFO:
067            level = Level.INFO;
068            break;
069        case WARN:
070            level = Level.WARNING;
071            break;
072        case ERROR:
073            level = Level.SEVERE;
074            break;
075        case AUDIT:
076            level = auditLevel;
077            break;
078        default:
079            break;
080        }
081        LogRecord record = new LogRecord(level, entry.getMessage());
082        record.setLoggerName(entry.getLoggerName());
083        record.setInstant(Instant.ofEpochMilli(entry.getTime()));
084        record.setSequenceNumber(entry.getSequence());
085        Throwable thrown = entry.getException();
086        if (thrown != null) {
087            record.setThrown(thrown);
088        }
089        try {
090            enabled.await();
091        } catch (InterruptedException e) {
092            // Just an attempt to synchronize.
093        }
094        forwarder.publish(entry, record);
095    }
096
097}