<commit_before>/**
 * Copyright (c) Sunesis d.o.o.
 */
package com.kumuluz.ee.logs.interceptors;

import com.kumuluz.ee.logs.LogCommons;
import com.kumuluz.ee.logs.LogManager;
import com.kumuluz.ee.logs.annotations.Log;
import com.kumuluz.ee.logs.annotations.enums.LogParams;
import com.kumuluz.ee.logs.messages.SimpleLogMessage;
import com.kumuluz.ee.logs.types.LogMethodContext;
import com.kumuluz.ee.logs.types.LogMethodMessage;

import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.util.HashMap;


/**
 * @Author <NAME>, <NAME>
 */
@Log
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LogInterceptor {

    @AroundInvoke
    public Object logMethodEntryAndExit(InvocationContext context) throws Exception {

        // get annotation either from class or method
        Log annotation = context.getMethod().getDeclaredAnnotation(Log.class) != null ? context.getMethod()
                .getDeclaredAnnotation(Log.class) : context.getMethod().getDeclaringClass().getDeclaredAnnotation(Log
                .class);

        // get annotation params
        LogParams[] value = annotation.value();
        boolean methodCall = annotation.methodCall();

        // get logger
        LogCommons logger = LogManager.getCommonsLogger(context.getTarget().getClass().getSuperclass().getName());

        // set message
        LogMethodMessage message = new LogMethodMessage();

        // set metrics
        for (LogParams logParam : value) {
            if (LogParams.METRICS.equals(logParam)) {
                message.enableMetrics();
                break;
            }
        }

        SimpleLogMessage msg = new SimpleLogMessage();
        msg.setMessage("Entering method.");
        msg.setFields(new HashMap<String, String>());

        // set method call
        if (methodCall) {
            msg.getFields().put("class", context.getMethod().getDeclaringClass().getName());
            msg.getFields().put("method", context.getMethod().getName());

            if (context.getParameters() != null && context.getParameters().length > 0) {
                String parameters = "[";
                for (Object o : context.getParameters()) {
                    parameters += o + ", ";
                }
                msg.getFields().put("parameters", parameters.substring(0, parameters.length() - 2) + "]");
            }

        }
        message.enableCall(msg);

        // log entry
        LogMethodContext logMethodContext = logger.logMethodEntry(message);

        Object result = context.proceed();

        // set method call
        msg.setMessage("Exiting method.");
        msg.getFields().put("result", result != null ? result.toString() : null);
        logMethodContext.setCallExitMessage(msg);

        // log exit
        logger.logMethodExit(logMethodContext);

        return result;
    }
}<commit_msg>Simplified paramaters to string convertion.<commit_after>/**
 * Copyright (c) Sunesis d.o.o.
 */
package com.kumuluz.ee.logs.interceptors;

import com.kumuluz.ee.logs.LogCommons;
import com.kumuluz.ee.logs.LogManager;
import com.kumuluz.ee.logs.annotations.Log;
import com.kumuluz.ee.logs.annotations.enums.LogParams;
import com.kumuluz.ee.logs.messages.SimpleLogMessage;
import com.kumuluz.ee.logs.types.LogMethodContext;
import com.kumuluz.ee.logs.types.LogMethodMessage;

import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.util.Arrays;
import java.util.HashMap;


/**
 * @Author <NAME>, <NAME>
 */
@Log
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LogInterceptor {

    @AroundInvoke
    public Object logMethodEntryAndExit(InvocationContext context) throws Exception {

        // get annotation either from class or method
        Log annotation = context.getMethod().getDeclaredAnnotation(Log.class) != null ? context.getMethod()
                .getDeclaredAnnotation(Log.class) : context.getMethod().getDeclaringClass().getDeclaredAnnotation(Log
                .class);

        // get annotation params
        LogParams[] value = annotation.value();
        boolean methodCall = annotation.methodCall();

        // get logger
        LogCommons logger = LogManager.getCommonsLogger(context.getTarget().getClass().getSuperclass().getName());

        // set message
        LogMethodMessage message = new LogMethodMessage();

        // set metrics
        for (LogParams logParam : value) {
            if (LogParams.METRICS.equals(logParam)) {
                message.enableMetrics();
                break;
            }
        }

        SimpleLogMessage msg = new SimpleLogMessage();
        msg.setMessage("Entering method.");
        msg.setFields(new HashMap<String, String>());

        // set method call
        if (methodCall) {
            msg.getFields().put("class", context.getMethod().getDeclaringClass().getName());
            msg.getFields().put("method", context.getMethod().getName());

            if (context.getParameters() != null && context.getParameters().length > 0) {
                msg.getFields().put("parameters", Arrays.toString(context.getParameters()));
            }

        }
        message.enableCall(msg);

        // log entry
        LogMethodContext logMethodContext = logger.logMethodEntry(message);

        Object result = context.proceed();

        // set method call
        msg.setMessage("Exiting method.");
        msg.getFields().put("result", result != null ? result.toString() : null);
        logMethodContext.setCallExitMessage(msg);

        // log exit
        logger.logMethodExit(logMethodContext);

        return result;
    }
}
{'fraction_non_alphanumeric': 0.12141014617368874, 'fraction_numerical': 0.0006878761822871883, 'mean_word_length': 2.7329910141206675, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4232208', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1546, 'n_words': 300}