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

org.opencds.cqf.utilities.LogUtils Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version

package org.opencds.cqf.utilities;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;

public class LogUtils 
{    
    private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(LogUtils.class);
    private static final Map resourceWarnings = new LinkedHashMap();  
    
    public static void putException(String id, Exception e) {
        resourceWarnings.put(LocalDateTime.now().toString() + ": " + id,  e.getMessage() == null ? e.toString() : e.getMessage());
    }

    public static void putException(String id, String warning) {
        resourceWarnings.put(LocalDateTime.now().toString() + ": " + id, warning);
    }

    public static void info(String message) {
        ourLog.warn(message);
    }

    public static void warn(String libraryName) {
        if (resourceWarnings.isEmpty()) {
            return;
        }
        String exceptionMessage = "";
        for (Map.Entry resourceException : resourceWarnings.entrySet()) {
            String resourceExceptionMessage = truncateMessage(resourceException.getValue()); 
            String resource =  FilenameUtils.getBaseName(resourceException.getKey());           
            exceptionMessage += "\r\n          Resource could not be processed: " + resource + "\r\n                    "  + resourceExceptionMessage;
        }
        ourLog.warn(libraryName +" could not be processed: "  + exceptionMessage);
        resourceWarnings.clear(); 
    } 

    private static String truncateMessage(String message) {   
        int maxSize = 500;     
        String[] messages = message.split("\r\n");
        int cutoffIndex = 0;
        for (String string : messages) {
            int stringIndex = cutoffIndex + string.length() + 4;
            cutoffIndex = stringIndex > maxSize ? maxSize : stringIndex;
            if(cutoffIndex == maxSize) {
                break;
            }
        }
        return message.length() < cutoffIndex ? message : message.substring(0, cutoffIndex) + "...";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy