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

backend.utils.FlexBackendLogger Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package backend.utils;

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

/**
 *
 * @author zua
 */
public class FlexBackendLogger {

    private boolean isOn = false;
    private Class aClass;

    public FlexBackendLogger(Class aClass) {
        this.aClass = aClass;
    }

    public void log(String format, Object... values) {
        if (isOn) {
            sout(format, values);
        }
    }

    public void info(String format, Object... values) {
        sout(format, values);
    }

    public void error(String format, Object... values) {
        if(isOn) {
            serr(format, values);
        }
    }

    public void on() {
        isOn = true;
    }

    public void off() {
        isOn = false;
    }

    public boolean isOn() {
        return isOn;
    }

    private void sout(String format, Object[] values) {
        String newFormat = "[%s] %s: " + format;
        Object[] newValues = new Object[values.length + 2];
        newValues[0] = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());
        newValues[1] = aClass.getSimpleName();
        int i = 2;
        for (Object o : values) {
            newValues[i] = o;
            i++;
        }
        String result = String.format(newFormat, newValues);
        System.out.println(result);
    }

    private void serr(String format, Object[] values) {
        String newFormat = "[%s] %s: " + format;
        Object[] newValues = new Object[values.length + 2];
        newValues[0] = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date());
        newValues[1] = aClass.getSimpleName();
        int i = 2;
        for (Object o : values) {
            newValues[i] = o;
            i++;
        }
        String result = String.format(newFormat, newValues);
        System.err.println(result);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy