co.runrightfast.core.utils.LoggingUtils.java Source code

Java tutorial

Introduction

Here is the source code for co.runrightfast.core.utils.LoggingUtils.java

Source

/*
 Copyright 2015 Alfio Zappala
    
 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 co.runrightfast.core.utils;

import static com.google.common.base.Preconditions.checkArgument;
import java.util.function.Supplier;
import java.util.logging.Level;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;
import java.util.logging.Logger;
import javax.json.JsonObject;
import lombok.NonNull;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 *
 * @author alfio
 */
public interface LoggingUtils {

    static final class JsonLog {

        public static JsonLog newInfoLog(@NonNull final Logger logger, final String className) {
            return new JsonLog(logger, INFO, className);
        }

        public static JsonLog newWarningLog(@NonNull final Logger logger, final String className) {
            return new JsonLog(logger, WARNING, className);
        }

        public static JsonLog newErrorLog(@NonNull final Logger logger, final String className) {
            return new JsonLog(logger, SEVERE, className);
        }

        private final Logger logger;

        private final Level level;

        private final String className;

        public JsonLog(@NonNull final Logger logger, @NonNull final Level level, final String className) {
            checkArgument(isNotBlank(className));
            this.logger = logger;
            this.level = level;
            this.className = className;
        }

        public void log(final String method, @NonNull final Supplier<JsonObject> message) {
            logger.logp(level, className, method, () -> message.get().toString());
        }

        public void log(final String method, @NonNull final Supplier<JsonObject> message,
                @NonNull final Throwable exception) {
            logger.logp(level, className, method, exception, () -> message.get().toString());
        }
    }

    /**
     * Helper for logging JSON messages
     *
     * @param logger
     * @param level
     * @param className
     * @param method
     * @param message
     */
    static void log(@NonNull final Logger logger, final Level level, final String className, final String method,
            @NonNull final Supplier<JsonObject> message) {
        logger.logp(level, className, method, () -> message.get().toString());
    }

    /**
     * Helper for logging JSON messages
     *
     * @param logger
     * @param level
     * @param className
     * @param method
     * @param message
     * @param exception
     */
    static void log(@NonNull final Logger logger, final Level level, final String className, final String method,
            @NonNull final Supplier<JsonObject> message, @NonNull final Throwable exception) {
        logger.logp(level, className, method, exception, () -> message.get().toString());
    }

}