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.log4j2osgi;
018
019import de.mnl.osgi.lf4osgi.core.LoggerFacade;
020import de.mnl.osgi.lf4osgi.core.LoggerFacadeManager;
021
022import org.apache.logging.log4j.Level;
023import org.apache.logging.log4j.Marker;
024import org.apache.logging.log4j.message.Message;
025import org.apache.logging.log4j.message.MessageFactory;
026import org.apache.logging.log4j.spi.AbstractLogger;
027import org.osgi.service.log.LoggerFactory;
028
029public class OsgiLogger extends AbstractLogger implements LoggerFacade {
030    private static final long serialVersionUID = 1L;
031    private OsgiLoggerContext context;
032    private org.osgi.service.log.Logger delegee;
033
034    public OsgiLogger(OsgiLoggerContext context, final String name,
035            final MessageFactory messageFactory) {
036        super(name, messageFactory);
037        this.context = context;
038        LoggerFacadeManager.registerFacade(this);
039    }
040
041    public OsgiLogger(OsgiLoggerContext context, final String name) {
042        super(name);
043        this.context = context;
044        LoggerFacadeManager.registerFacade(this);
045    }
046
047    @Override
048    public void loggerFactoryUpdated(LoggerFactory factory) {
049        delegee = factory.getLogger(context.getBundle(), name,
050            org.osgi.service.log.Logger.class);
051    }
052
053    @Override
054    public Level getLevel() {
055        if (delegee.isTraceEnabled()) {
056            return Level.TRACE;
057        }
058        if (delegee.isDebugEnabled()) {
059            return Level.DEBUG;
060        }
061        if (delegee.isInfoEnabled()) {
062            return Level.INFO;
063        }
064        if (delegee.isWarnEnabled()) {
065            return Level.WARN;
066        }
067        if (delegee.isErrorEnabled()) {
068            return Level.ERROR;
069        }
070        // Option: throw new IllegalStateException("Unknown SLF4JLevel");
071        // Option: return Level.ALL;
072        return Level.OFF;
073    }
074
075    @Override
076    public boolean isEnabled(final Level level, final Marker marker,
077            final Message data, final Throwable thr) {
078        return isEnabledFor(level, marker);
079    }
080
081    @Override
082    public boolean isEnabled(final Level level, final Marker marker,
083            final CharSequence data, final Throwable thr) {
084        return isEnabledFor(level, marker);
085    }
086
087    @Override
088    public boolean isEnabled(final Level level, final Marker marker,
089            final Object data, final Throwable thr) {
090        return isEnabledFor(level, marker);
091    }
092
093    @Override
094    public boolean isEnabled(final Level level, final Marker marker,
095            final String data) {
096        return isEnabledFor(level, marker);
097    }
098
099    @Override
100    public boolean isEnabled(final Level level, final Marker marker,
101            final String data, final Object... p1) {
102        return isEnabledFor(level, marker);
103    }
104
105    @Override
106    public boolean isEnabled(final Level level, final Marker marker,
107            final String message, final Object p0) {
108        return isEnabledFor(level, marker);
109    }
110
111    @Override
112    public boolean isEnabled(final Level level, final Marker marker,
113            final String message, final Object p0,
114            final Object p1) {
115        return isEnabledFor(level, marker);
116    }
117
118    @Override
119    public boolean isEnabled(final Level level, final Marker marker,
120            final String message, final Object p0,
121            final Object p1, final Object p2) {
122        return isEnabledFor(level, marker);
123    }
124
125    @Override
126    public boolean isEnabled(final Level level, final Marker marker,
127            final String message, final Object p0,
128            final Object p1, final Object p2, final Object p3) {
129        return isEnabledFor(level, marker);
130    }
131
132    @Override
133    public boolean isEnabled(final Level level, final Marker marker,
134            final String message, final Object p0,
135            final Object p1, final Object p2, final Object p3,
136            final Object p4) {
137        return isEnabledFor(level, marker);
138    }
139
140    @Override
141    public boolean isEnabled(final Level level, final Marker marker,
142            final String message, final Object p0,
143            final Object p1, final Object p2, final Object p3,
144            final Object p4, final Object p5) {
145        return isEnabledFor(level, marker);
146    }
147
148    @Override
149    public boolean isEnabled(final Level level, final Marker marker,
150            final String message, final Object p0,
151            final Object p1, final Object p2, final Object p3,
152            final Object p4, final Object p5, final Object p6) {
153        return isEnabledFor(level, marker);
154    }
155
156    @Override
157    public boolean isEnabled(final Level level, final Marker marker,
158            final String message, final Object p0,
159            final Object p1, final Object p2, final Object p3,
160            final Object p4, final Object p5, final Object p6,
161            final Object p7) {
162        return isEnabledFor(level, marker);
163    }
164
165    @Override
166    public boolean isEnabled(final Level level, final Marker marker,
167            final String message, final Object p0,
168            final Object p1, final Object p2, final Object p3,
169            final Object p4, final Object p5, final Object p6,
170            final Object p7, final Object p8) {
171        return isEnabledFor(level, marker);
172    }
173
174    @Override
175    public boolean isEnabled(final Level level, final Marker marker,
176            final String message, final Object p0,
177            final Object p1, final Object p2, final Object p3,
178            final Object p4, final Object p5, final Object p6,
179            final Object p7, final Object p8, final Object p9) {
180        return isEnabledFor(level, marker);
181    }
182
183    @Override
184    public boolean isEnabled(final Level level, final Marker marker,
185            final String data, final Throwable thr) {
186        return isEnabledFor(level, marker);
187    }
188
189    private boolean isEnabledFor(final Level level, final Marker marker) {
190        switch (level.getStandardLevel()) {
191        case DEBUG:
192            return delegee.isDebugEnabled();
193        case TRACE:
194            return delegee.isTraceEnabled();
195        case INFO:
196            return delegee.isInfoEnabled();
197        case WARN:
198            return delegee.isWarnEnabled();
199        case ERROR:
200            return delegee.isErrorEnabled();
201        default:
202            return delegee.isErrorEnabled();
203        }
204    }
205
206    @Override
207    public void logMessage(final String fqcn, final Level level,
208            final Marker marker, final Message message, final Throwable thr) {
209        switch (level.getStandardLevel()) {
210        case TRACE:
211            delegee.trace(message.getFormattedMessage(),
212                message.getParameters(), thr);
213            break;
214        case DEBUG:
215            delegee.debug(message.getFormattedMessage(),
216                message.getParameters(), thr);
217            break;
218        case INFO:
219            delegee.info(message.getFormattedMessage(),
220                message.getParameters(), thr);
221            break;
222        case WARN:
223            delegee.warn(message.getFormattedMessage(),
224                message.getParameters(), thr);
225            break;
226        case ERROR:
227            delegee.error(message.getFormattedMessage(),
228                message.getParameters(), thr);
229            break;
230        default:
231            delegee.error(message.getFormattedMessage(),
232                message.getParameters(), thr);
233            break;
234
235        }
236    }
237
238}