
io.helidon.build.util.Log Maven / Gradle / Ivy
/*
* Copyright (c) 2019, 2021 Oracle and/or its affiliates.
*
* 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 io.helidon.build.util;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static io.helidon.build.util.StyleFunction.BoldBlue;
import static io.helidon.build.util.StyleFunction.Italic;
import static java.util.Objects.requireNonNull;
/**
* Simple, centralized logging.
*/
public class Log {
private static final AtomicReference WRITER = new AtomicReference<>();
private static final AtomicInteger MESSAGES = new AtomicInteger();
private static final AtomicInteger WARNINGS = new AtomicInteger();
private static final AtomicInteger ERRORS = new AtomicInteger();
private static final boolean DEBUG = "debug".equals(System.getProperty("log.level"));
private static final String PAD = " ";
private Log() {
}
/**
* Levels.
*/
public enum Level {
/**
* Debug level.
*/
DEBUG(java.util.logging.Level.FINEST),
/**
* Verbose level.
*/
VERBOSE(java.util.logging.Level.FINE),
/**
* Info level.
*/
INFO(java.util.logging.Level.INFO),
/**
* Warn level.
*/
WARN((java.util.logging.Level.WARNING)),
/**
* Error level.
*/
ERROR(java.util.logging.Level.SEVERE);
/**
* Returns the corresponding java.util.logging.Level level.
*
* @return The level.
*/
java.util.logging.Level toJulLevel() {
return julLevel;
}
private final java.util.logging.Level julLevel;
Level(java.util.logging.Level julLevel) {
this.julLevel = julLevel;
}
}
/**
* The log writer.
*/
public interface Writer {
/**
* Writes the message and throwable if at or above the given level.
*
* @param level The level.
* @param thrown The throwable. May be {@code null}.
* @param message The message.
* @param args The message args.
*/
void write(Level level, Throwable thrown, String message, Object... args);
/**
* Returns whether or not debug messages will be written.
*
* @return {@code true} if enabled.
*/
boolean isDebug();
/**
* Returns whether or not verbose messages will be written.
*
* @return {@code true} if enabled.
*/
boolean isVerbose();
/**
* Return whether this writer prints to the system {@code stdout} and/or {@code stderr}.
*
* @return {@code true} if system.
*/
default boolean isSystem() {
return false;
}
}
/**
* Sets the writer.
*
* @param writer The writer.
*/
public static void writer(Writer writer) {
WRITER.set(requireNonNull(writer));
}
/**
* Returns the writer.
*
* @return The writer.
*/
public static Writer writer() {
Writer writer = WRITER.get();
if (writer == null) {
writer = SystemLogWriter.create();
writer(writer);
}
return writer;
}
/**
* Returns whether or not debug messages will be written.
*
* @return {@code true} if enabled.
*/
public static boolean isDebug() {
return writer().isDebug();
}
/**
* Returns whether or not verbose messages will be written.
*
* @return {@code true} if enabled.
*/
public static boolean isVerbose() {
return writer().isVerbose();
}
/**
* Returns the number of messages logged.
*
* @return The count.
*/
public static int messages() {
return MESSAGES.get();
}
/**
* Returns the number of WARN messages logged.
*
* @return The count.
*/
public static int warnings() {
return WARNINGS.get();
}
/**
* Returns the number of ERROR messages logged.
*
* @return The count.
*/
public static int errors() {
return ERRORS.get();
}
/**
* Log a message at DEBUG level.
*
* @param message The message.
* @param args The message args.
*/
public static void debug(String message, Object... args) {
log(Level.DEBUG, message, args);
}
/**
* Log a message at VERBOSE level.
*
* @param message The message.
* @param args The message args.
*/
public static void verbose(String message, Object... args) {
log(Level.VERBOSE, message, args);
}
/**
* Log an empty message at INFO level.
*/
public static void info() {
log(Level.INFO, "");
}
/**
* Log a message at INFO level.
*
* @param message The message.
* @param args The message args.
*/
public static void info(String message, Object... args) {
log(Level.INFO, message, args);
}
/**
* Log the entries using {@link StyleFunction#Italic} for all keys and {@link StyleFunction#BoldBlue} for all keys.
*
* @param map The entries.
*/
public static void info(Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy