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

ch.openchvote.simpleservices.SimpleLogger Maven / Gradle / Ivy

Go to download

This module provides simple implementations of several infrastructure services.

The newest version!
/*
 * Copyright (C) 2024 Berner Fachhochschule https://e-voting.bfh.ch
 *
 *  - This program is free software: you can redistribute it and/or modify                           -
 *  - it under the terms of the GNU Affero General Public License as published by                    -
 *  - the Free Software Foundation, either version 3 of the License, or                              -
 *  - (at your option) any later version.                                                            -
 *  -                                                                                                -
 *  - This program is distributed in the hope that it will be useful,                                -
 *  - but WITHOUT ANY WARRANTY; without even the implied warranty of                                 -
 *  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                   -
 *  - GNU General Public License for more details.                                                   -
 *  -                                                                                                -
 *  - You should have received a copy of the GNU Affero General Public License                       -
 *  - along with this program. If not, see .                           -
 */
package ch.openchvote.simpleservices;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.ResourceBundle;

/**
 * This class is a simple implementation of the {@link System.Logger} interface, which allows printing the logger
 * messages to the standard output {@code System.out} or error output {@code System.err} using
 * {@link java.io.PrintStream#println}.
 */
public class SimpleLogger implements System.Logger {

    private final Level loggerLevel;
    private final int maxLength;

    /**
     * Main constructor for this class. The provided {@link java.lang.System.Logger.Level} enum constant defines the
     * granularity of the displayed logging messages, i.e., only messages which {@link Level#getSeverity()} greater or
     * equal to the severity of the given {@link java.lang.System.Logger.Level} are displayed.
     *
     * @param loggerLevel The given logger level
     */
    @SuppressWarnings("OptionalGetWithoutIsPresent")
    public SimpleLogger(Level loggerLevel) {

        // set logger level
        this.loggerLevel = loggerLevel;

        // computing the maximal string length of the relevant logger levels
        this.maxLength = Arrays.stream(Level.values())
                .filter(value -> value.getSeverity() >= this.loggerLevel.getSeverity())
                .mapToInt(value -> value.getName().length())
                .max()
                .getAsInt();
    }

    @Override
    public String getName() {
        return this.loggerLevel.getName();
    }

    @Override
    public boolean isLoggable(Level level) {
        return level.getSeverity() >= this.loggerLevel.getSeverity();
    }

    @Override
    public void log(Level level, ResourceBundle bundle, String message, Throwable thrown) {
        var numberOfSpaces = this.maxLength - level.getName().length() + 1;
        var formatString = "[%s]%" + numberOfSpaces + "s%s";
        if (this.isLoggable(level)) {
            var output = String.format(formatString, level, " ", message);
            switch (level) {
                case INFO, DEBUG, TRACE -> System.out.println(output);
                case WARNING, ERROR -> System.err.println(output);
            }
        }
    }

    @Override
    public void log(Level level, ResourceBundle bundle, String format, Object... params) {
        var message = MessageFormat.format(format, params);
        this.log(level, bundle, message, (Throwable) null);
    }

    @Override
    public void log(Level level, String message) {
        this.log(level, null, message, (Throwable) null);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy