001/*
002 * Copyright (c) 2010-2020 Mark Allen, Norbert Bartels.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.restfb.logging;
023
024import java.util.Optional;
025import java.util.logging.Level;
026import java.util.logging.LogRecord;
027import java.util.logging.Logger;
028
029/**
030 * Logger implementation based on {@code java.util.logging}.
031 *
032 * The JUL configuration should be provided by a external application. The mapping is defined like:
033 * <ul>
034 * <li>trace maps to java.util.logging.Level.<i>FINER</i></li>
035 * <li>debug maps to java.util.logging.Level.<i>FINE</i></li>
036 * <li>info maps to java.util.logging.Level.<i>INFO</i></li>
037 * <li>warn maps to java.util.logging.Level.<i>WARNING</i></li>
038 * <li>error maps to java.util.logging.Level.<i>SEVERE</i></li>
039 * <li>fatal maps to java.util.logging.Level.<i>SEVERE</i></li>
040 * </ul>
041 */
042public class JulLogger extends RestFBLogger {
043
044  private final Logger logger;
045
046  public JulLogger(String logName) {
047    logger = Logger.getLogger(logName);
048  }
049
050  @Override
051  public void trace(String msg, Object... args) {
052    createLogMessage(Level.FINER, msg, args);
053  }
054
055  @Override
056  public void debug(String msg, Object... args) {
057    createLogMessage(Level.FINE, msg, args);
058  }
059
060  @Override
061  public void info(String msg, Object... args) {
062    createLogMessage(Level.INFO, msg, args);
063  }
064
065  @Override
066  public void warn(String msg, Object... args) {
067    createLogMessage(Level.WARNING, msg, args);
068  }
069
070  @Override
071  public void error(String msg, Object... args) {
072    createLogMessage(Level.SEVERE, msg, args);
073  }
074
075  @Override
076  public void fatal(String msg, Object... args) {
077    createLogMessage(Level.SEVERE, msg, args);
078  }
079
080  @Override
081  public boolean isDebugEnabled() {
082    return logger.isLoggable(Level.FINE);
083  }
084
085  @Override
086  public boolean isInfoEnabled() {
087    return logger.isLoggable(java.util.logging.Level.INFO);
088  }
089
090  @Override
091  public boolean isTraceEnabled() {
092    return logger.isLoggable(Level.FINER);
093  }
094
095  private void createLogMessage(Level level, String msg, Object[] args) {
096    if (logger.isLoggable(level)) {
097      JulMessage.MessageTuple tuple = JulMessage.convertMessageString(msg, args);
098      LogRecord logRecord = new LogRecord(level, tuple.getMessage());
099      Optional.ofNullable(tuple.getThrowable()).ifPresent(logRecord::setThrown);
100      logRecord.setSourceClassName(null);
101      logRecord.setSourceMethodName(null);
102      logRecord.setLoggerName(logger.getName());
103      logger.log(logRecord);
104    }
105  }
106}