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

io.github.middlewarelabs.agentapmjava.Logger Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
package io.github.middlewarelabs.agentapmjava;

import java.util.HashMap;
import java.util.Map;
import java.lang.management.ManagementFactory;

import org.fluentd.logger.FluentLogger;
// import com.google.common.flogger.FluentLogger;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;

public class Logger {

    private static String tag = "java.app";
    // private static final FluentLogger logger = FluentLogger.forEnclosingClass();
    private static FluentLogger LOG = FluentLogger.getLogger("app", "localhost", 8006);




    public static void log(String level, String message) {
        String pname = ManagementFactory.getRuntimeMXBean().getName();
        String pid = pname.split("@")[0];
        String projectName = "Project-"+pid;
        String serviceName = "Service-"+pid;

        String resourceAttributes = System.getProperty("otel.resource.attributes","");
        if(resourceAttributes.contains("project.name")){
            String[] strArray= resourceAttributes.split(",");
            HashMap attributeMap = new HashMap();
            for (String pair : strArray) {
                String split[] = pair.split("=");
                attributeMap.put(split[0], split[1]);
            }
            if (attributeMap.containsKey("project.name")){
                projectName = attributeMap.get("project.name");
            }
        }

        String service = System.getProperty("otel.service.name","");
        if(service.length() > 0){
            serviceName = service;
        }

        Map data = new HashMap();
        data.put("level", level);
        data.put("message", message);
        data.put("project.name", projectName);
        data.put("service.name", serviceName);
        LOG.log(tag, data);
    }

    public static void debug(String message) {
        log("debug", message);
    }

    public static void info(String message) {
        log("info", message);
    }

    public static void warn(String message) {
        log("warn", message);
    }

    public static void error(String message) {
        log("error", message);
    }

    public static void recordError(Exception e){
        System.out.println("error message: "+ e.getMessage());
        Span span = Span.current();
        span.setStatus(StatusCode.ERROR, e.getMessage());
        span.recordException(e);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy