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

com.objectsql.spring.LogHandler Maven / Gradle / Ivy

The newest version!
package com.objectsql.spring;

import com.objectsql.handler.DebugHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LogHandler implements DebugHandler {

    private final Logger logger = LoggerFactory.getLogger(LogHandler.class);

    @Override
    public void handle(Class clazz, String msg, Object... objects) {
        if (objects != null && objects.length > 0){
            LoggerFactory.getLogger(clazz).info("{}, {}", msg, objects[0]);
            if (objects.length > 1){
                List params = Arrays.stream(objects).collect(Collectors.toList());
                params.remove(0);
                Object last = params.get(params.size() - 1);
                if (last instanceof Throwable){
                    params.remove(last);
                }else{
                    last = null;
                }
                if (params.isEmpty()) {
                    LoggerFactory.getLogger(clazz).info("Other params: {}", params);
                }
                if (last != null){
                    LoggerFactory.getLogger(clazz).info("Exception", (Throwable)last);
                }
            }
        }else{
            LoggerFactory.getLogger(clazz).info("{}", msg);
        }
    }
}