com.gtcgroup.jped.core.helper.internal.SystemOutLoggingBeanHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.gtcgroup.jped.core.helper.internal.SystemOutLoggingBeanHelper.java

Source

/*
 * [Licensed per the Open Source "MIT License".]
 *
 * Copyright (c) 2006 - 2016 by
 * Global Technology Consulting Group, Inc. at
 * http://gtcGroup.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gtcgroup.jped.core.helper.internal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gtcgroup.jped.core.base.JpcBaseBeanHelper;
import com.gtcgroup.jped.core.base.JpcBaseException;
import com.gtcgroup.jped.core.po.JpcPluginPropertiesPO;
import com.gtcgroup.jped.core.si.JpcLoggingSI;
import com.gtcgroup.jped.core.si.JpcPluginSI;

/**
 * This Helper class provides a default implementation typically used for JUnit
 * test logging.
 *
 * <p style="font-family:Verdana; font-size:10px; font-style:italic">
 * Copyright (c) 2006 - 2016 by Global Technology Consulting Group, Inc. at
 * <a href="http://gtcGroup.com">gtcGroup.com </a>.
 * </p>
 *
 * @author Marvin Toll
 * @since v3.0
 */
@SuppressWarnings("javadoc")
public class SystemOutLoggingBeanHelper extends JpcBaseBeanHelper implements JpcLoggingSI {

    public static boolean EXCEPTION_MODE = true;

    public static boolean LIFECYCLE_MODE = false;

    public static boolean PERFORMANCE_MODE = false;

    public static boolean TRACE_MODE = false;

    public static boolean VALIDATION_MODE = false;

    public static String USER_ID = "testId";

    /**
     * This method converts an instance to a human readable String.
     *
     * @param instance
     * @return {@link String}
     */
    public static String convertToJsonString(final Object[] instances) {

        // *** DO NOT LOG THIS METHOD ***

        final StringBuilder jsonString = new StringBuilder();
        String delimiter = ">";

        for (final Object instance : instances) {

            jsonString.append(" ");
            jsonString.append(delimiter);
            delimiter = delimiter + ">";

            if (null == instance) {

                jsonString.append("NULL=");

            } else {

                jsonString.append(instance.getClass().getSimpleName());
                jsonString.append("=");
            }

            if (instance instanceof String) {

                jsonString.append(instance);

            } else if (instance instanceof List<?>) {

                final List<?> list = (List<?>) instance;

                jsonString.append("[" + list.size() + "]");

            } else {
                try {

                    final ObjectMapper objectMapper = new ObjectMapper();
                    jsonString.append(objectMapper.writeValueAsString(instance));

                } catch (@SuppressWarnings("unused") final Exception e) {
                    jsonString.append("{Unable to serialize this object.}");
                }
            }
        }

        return jsonString.toString();
    }

    /**
     * This method supports all logging.
     *
     * @param className
     * @param methodName
     * @param message
     * @param level
     * @param userId
     */
    protected static void log(final String className, final String methodName, final String message,
            final String level) {

        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
        final Date date = new Date();
        final String dateResult = formatter.format(date);

        final StringBuilder logStatement = new StringBuilder();

        logStatement.append("[jPED ");
        logStatement.append(level);
        logStatement.append("] ");
        logStatement.append(dateResult);
        logStatement.append(" [");
        logStatement.append(Thread.currentThread().getName());
        logStatement.append("] ");
        logStatement.append(SystemOutLoggingBeanHelper.getUserId());
        logStatement.append(": ");
        logStatement.append(className);
        logStatement.append(".");
        logStatement.append(methodName);
        logStatement.append("() -");
        logStatement.append(message);

        System.out.println(logStatement.toString());
    }

    protected static void traceEnterExit(final String enterOrExit, final String className, final String methodName,
            final String methodElapsedMilliseconds, final Object[] objects) {

        final StringBuilder message = new StringBuilder();

        try {
            message.append(SystemOutLoggingBeanHelper.convertToJsonString(objects));
            if (!methodElapsedMilliseconds.isEmpty()) {
                message.append(": ");
                message.append(methodElapsedMilliseconds);
                message.append(" ms]");
            }

        } catch (@SuppressWarnings("unused") final Exception e) {
            // Swallow this exception and keep going!
        }

        SystemOutLoggingBeanHelper.log(className, methodName, message.toString(), enterOrExit);
        return;
    }

    private static String getUserId() {

        return SystemOutLoggingBeanHelper.USER_ID;
    }

    /**
     * @see com.gtcgroup.jped.core.si.JpcPluginSI#assignDefaultPropertiesTM()
     */
    @Override
    public JpcPluginPropertiesPO assignDefaultPropertiesTM() {

        return new JpcPluginPropertiesPO();
    }

    /**
     * @see JpcPluginSI#assignOverridingPropertiesReadEachInvocationTM(java.util.Properties)
     */
    @Override
    public void assignOverridingPropertiesReadEachInvocationTM(final Properties properties) {

        return;
    }

    /**
     * @see JpcPluginSI#assignPropertiesReadOncePerLifeOfApplicationTM(com.gtcgroup.jped.core.po.JpcPluginPropertiesPO)
     */
    @Override
    public void assignPropertiesReadOncePerLifeOfApplicationTM(final JpcPluginPropertiesPO pluginPropertiesPO) {

        return;
    }

    /**
     * @see JpcLoggingSI#config(String, String, String)
     */
    @Override
    public void config(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.CONFIG);
    }

    /**
     * @see JpcLoggingSI#entering(String, String, java.lang.Object)
     */
    @Override
    public void entering(final String className, final String methodName, final Object... objects) {

        if (isModeTrace()) {
            SystemOutLoggingBeanHelper.traceEnterExit(JpcLoggingSI.ENTER, className, methodName, "", objects);
        }
        return;
    }

    /**
     * @see JpcLoggingSI#exiting(String, String, String, java.lang.Object)
     */
    @Override
    public void exiting(final String className, final String methodName, final String methodElapsedMilliseconds,
            final Object... objects) {

        if (isModeTrace()) {
            SystemOutLoggingBeanHelper.traceEnterExit(JpcLoggingSI.EXIT, className, methodName,
                    methodElapsedMilliseconds, objects);
        }
    }

    /**
     * @see JpcLoggingSI#fine(String, String, String)
     */
    @Override
    public void fine(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.FINE);
    }

    /**
     * @see JpcLoggingSI#finer(String, String, String)
     */
    @Override
    public void finer(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.FINER);
    }

    /**
     * @see JpcLoggingSI#finest(String, String, String)
     */
    @Override
    public void finest(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.FINEST);
    }

    /**
     * @see JpcLoggingSI#info(String, String, String)
     */
    @Override
    public void info(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.INFO);
    }

    /**
     * @see JpcLoggingSI#isModeException()
     */
    @Override
    public boolean isModeException() {

        return SystemOutLoggingBeanHelper.EXCEPTION_MODE;
    }

    /**
     * @see JpcLoggingSI#isModeLifecycle()
     */
    @Override
    public boolean isModeLifecycle() {

        return SystemOutLoggingBeanHelper.LIFECYCLE_MODE;
    }

    /**
     * @see JpcLoggingSI#isModePerformance()
     */
    @Override
    public boolean isModePerformance() {

        return SystemOutLoggingBeanHelper.PERFORMANCE_MODE;
    }

    /**
     * @see JpcLoggingSI#isModeTrace()
     */
    @Override
    public boolean isModeTrace() {

        return SystemOutLoggingBeanHelper.TRACE_MODE;
    }

    /**
     * @see JpcLoggingSI#isModeValidation()
     */
    @Override
    public boolean isModeValidation() {

        return SystemOutLoggingBeanHelper.VALIDATION_MODE;
    }

    @Override
    public void logModeException(final JpcBaseException exception) {

        if (isModeException()) {

            final String formattedException = exception.getExceptionPO().formatException(exception);

            SystemOutLoggingBeanHelper.log(exception.getExceptionPO().getClassName(),
                    exception.getExceptionPO().getMethodName(), formattedException, JpcLoggingSI.EXCEPTION);
        }
    }

    /**
     * @see JpcLoggingSI#logModeLifecycle(String, String, String)
     */
    @Override
    public void logModeLifecycle(final String className, final String methodName, final String message) {

        if (isModeLifecycle()) {

            SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.LIFECYCLE);
        }
    }

    /**
     * @see JpcLoggingSI#logModePerformance(String, String, String)
     */
    @Override
    public void logModePerformance(final String className, final String methodName, final String message) {

        if (isModePerformance()) {

            SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.PERFORMANCE);
        }
    }

    /**
     * @see JpcLoggingSI#logModeValidation(String, String, String)
     */
    @Override
    public void logModeValidation(final String className, final String methodName, final String message) {

        if (isModeValidation()) {

            SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.VALIDATION);
        }
    }

    /**
     * @see .JpcLoggingSI#setEXCEPTION_MODE(boolean)
     */
    @SuppressWarnings("unchecked")
    @Override
    public SystemOutLoggingBeanHelper setEXCEPTION_MODE(final boolean eXCEPTION_MODE) {

        SystemOutLoggingBeanHelper.EXCEPTION_MODE = eXCEPTION_MODE;
        return this;
    }

    /**
     * @see .JpcLoggingSI#setLIFECYCLE_MODE(boolean)
     */
    @SuppressWarnings("unchecked")
    @Override
    public SystemOutLoggingBeanHelper setLIFECYCLE_MODE(final boolean lIFECYCLE_MODE) {

        SystemOutLoggingBeanHelper.LIFECYCLE_MODE = lIFECYCLE_MODE;
        return this;
    }

    /**
     * @see .JpcLoggingSI#setPERFORMANCE_MODE(boolean)
     */
    @SuppressWarnings("unchecked")
    @Override
    public SystemOutLoggingBeanHelper setPERFORMANCE_MODE(final boolean pERFORMANCE_MODE) {

        SystemOutLoggingBeanHelper.PERFORMANCE_MODE = pERFORMANCE_MODE;
        return this;
    }

    /**
     * @see .JpcLoggingSI#setTRACE_MODE(boolean)
     */
    @SuppressWarnings("unchecked")
    @Override
    public SystemOutLoggingBeanHelper setTRACE_MODE(final boolean tRACE_MODE) {

        SystemOutLoggingBeanHelper.TRACE_MODE = tRACE_MODE;
        return this;
    }

    /**
     * @see JpcLoggingSI#setUSER_ID(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public SystemOutLoggingBeanHelper setUSER_ID(final String uSER_ID) {

        SystemOutLoggingBeanHelper.USER_ID = uSER_ID;
        return this;
    }

    /**
     * @see JpcLoggingSI#setVALIDATION_MODE(boolean)
     */
    @SuppressWarnings("unchecked")
    @Override
    public SystemOutLoggingBeanHelper setVALIDATION_MODE(final boolean vALIDATION_MODE) {

        SystemOutLoggingBeanHelper.VALIDATION_MODE = vALIDATION_MODE;
        return this;
    }

    /**
     * @see JpcLoggingSI#severe(String, String, String)
     */
    @Override
    public void severe(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.SEVERE);
    }

    /**
     * @see JpcLoggingSI#warning(String, String, String)
     */
    @Override
    public void warning(final String className, final String methodName, final String message) {

        SystemOutLoggingBeanHelper.log(className, methodName, message, JpcLoggingSI.WARNING);
    }
}