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

src.main.java.org.kawanfw.sql.servlet.PrivateTmpLogger Maven / Gradle / Ivy

Go to download

AceQL HTTP is a framework of REST like http APIs that allow to access to remote SQL databases over http from any device that supports http. AceQL HTTP is provided with three client SDK: - The AceQL C# Client SDK allows to wrap the HTTP APIs using Microsoft SQL Server like calls in their code, just like they would for a local database. - The AceQL Java Client JDBC Driver allows to wrap the HTTP APIs using JDBC calls in their code, just like they would for a local database. - The AceQL Python Client SDK allows SQL calls to be encoded with standard unmodified DB-API 2.0 syntax

There is a newer version: 12.2
Show newest version
/**
 * 
 */
package org.kawanfw.sql.servlet;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
 * Our own internal private logger that will always catch all Exceptions includind rutime Exceptions
 * @author Nicolas de Pomereu
 *
 */
public class PrivateTmpLogger {

    private static final int MB = 1024 * 1024;
    private static final long MAX_LENGTH = 200 * MB;
    
    private Throwable throwable = null;

   
    /**
     * Constructor
     * @param throwable	thrown when any catch or runtime exceptions are thrown
     */
    public PrivateTmpLogger(Throwable throwable) {
	this.throwable = throwable;
    }


    public void log() throws FileNotFoundException, IOException {

	File tempDir = SystemUtils.getJavaIoTmpDir();
	String tempKawanSoftDirStr = tempDir.toString();
	if (! tempKawanSoftDirStr.endsWith(File.separator)) {
	    tempKawanSoftDirStr+=File.separator;
	}
	
	tempKawanSoftDirStr += ".kawansoft";
	File tempKawanSoftDir = new File(tempKawanSoftDirStr);
	if (!tempKawanSoftDir.exists()) {
	    tempKawanSoftDir.mkdirs();
	}
	
	File file = new File(tempKawanSoftDirStr +  File.separator + "aceql_exceptions.log");
	
	// Security delete if more than 200 MB
	if (file.length() > MAX_LENGTH) {
	    file.delete();
	}
	
	try(OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) {
	    ServerSqlManager.writeLine(out, getNowFormatted() + " " + ExceptionUtils.getStackTrace(throwable));	
	}
	
    }
    
    public static String getNowFormatted() {
	Timestamp tsNow = new Timestamp(System.currentTimeMillis());
	DateFormat df = new SimpleDateFormat("yy/MM/dd HH:mm:ss.SSS");
	String now = df.format(tsNow);
	return now;
    }
    
    
    

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy