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

org.nightcode.milter.util.Log Maven / Gradle / Ivy

There is a newer version: 0.8
Show newest version
/*
 * 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.nightcode.milter.util;

import java.io.PrintStream;
import java.time.LocalDateTime;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.lang.String.format;
import static org.nightcode.milter.util.Log.PrintStreamLoggingHandler.ERR;
import static org.nightcode.milter.util.Log.PrintStreamLoggingHandler.OUT;

public enum Log {
  ;

  public interface LoggingHandler {

    void log(@NotNull Class clazz, String message);

    void log(@NotNull Class clazz, String message, Object... params);

    void log(@NotNull Class clazz, String message, @Nullable Throwable thrown);

    void log(@NotNull Class clazz, Supplier supplier, @Nullable Throwable thrown);

    default void log(@NotNull Class clazz, Throwable thrown) {
      log(clazz, "", thrown);
    }

    default void log(@NotNull Class clazz, Supplier supplier) {
      log(clazz, supplier, null);
    }
  }

  private static final AtomicReference DEBUG = new AtomicReference<>(OUT);
  private static final AtomicReference INFO  = new AtomicReference<>(OUT);
  private static final AtomicReference WARN  = new AtomicReference<>(ERR);
  private static final AtomicReference ERROR = new AtomicReference<>(ERR);
  private static final AtomicReference FATAL = new AtomicReference<>(ERR);

  public static LoggingHandler debug() {
    return DEBUG.get();
  }

  public static LoggingHandler error() {
    return ERROR.get();
  }

  public static LoggingHandler fatal() {
    return FATAL.get();
  }

  public static LoggingHandler info() {
    return INFO.get();
  }

  public static LoggingHandler warn() {
    return WARN.get();
  }

  public static void setLoggingHandler(LoggingHandler debug,
                                       LoggingHandler info,
                                       LoggingHandler warn,
                                       LoggingHandler error,
                                       LoggingHandler fatal) {
    DEBUG.set(debug);
    INFO.set(info);
    WARN.set(warn);
    ERROR.set(error);
    FATAL.set(fatal);
  }

  enum PrintStreamLoggingHandler implements LoggingHandler {
    ERR(System.err),
    OUT(System.out);

    private final PrintStream stream;

    PrintStreamLoggingHandler(PrintStream stream) {
      this.stream = stream;
    }

    @Override public void log(@NotNull Class clazz, String message) {
      log(clazz, () -> message, null);
    }

    @Override public void log(@NotNull Class clazz, String message, @Nullable Throwable thrown) {
      log(clazz, () -> message, thrown);
    }

    @Override public void log(@NotNull Class clazz, String message, Object... params) {
      log(clazz, () -> format(message.replaceAll("\\{}", "%s"), params), null);
    }

    @Override public void log(@NotNull Class clazz, Supplier message, @Nullable Throwable thrown) {
      synchronized (stream) {
        stream.printf("%s [%s/%s]: %s\n", LocalDateTime.now(), Thread.currentThread().getName(), clazz.getSimpleName(), message.get());
        if (thrown != null) {
          thrown.printStackTrace(stream);
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy