| Refresh | Home EGTry.com

a very simple servlet file logger -flush Filewriter so all output can be seen after that


package io;

import java.io.FileWriter;
import java.io.IOException;

public class MyLogger {
	private static FileWriter out;
	
	static {
		try {
			out=new FileWriter("/tmp/log.txt");
		} catch (IOException e) {
			
		}
	}
	
	public static void main(String[] args) throws Exception {
		for(int i=0; i<1000; i++) {
			MyLogger.trace(i+"");
			System.out.println(i);
			Thread.sleep(1000);
		}
	}
	
	public static void trace(String msg) {
		if (out !=null) {
			try {
				out.write(msg+"\n");
				out.flush();
			} catch (IOException e) {
				
			}
		}
	}
}