Dans mon grenier j'ai retrouvé ceci tout fait, peut etre cela te plaira...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
public class ConsoleRedirector extends PrintStream
{
static OutputStream logfile;
static PrintStream oldStdout;
static PrintStream oldStderr;
public static String LogFile = "";
ConsoleRedirector(PrintStream ps)
{
super(ps);
}
// Starts copying stdout and
//stderr to the file f.
public static void start(String f_OUT) throws IOException
{
LogFile = f_OUT;
// Save old settings.
oldStdout = System.out;
oldStderr = System.err;
// Create/Open logfile.
logfile = new PrintStream(new FileOutputStream(f_OUT));
// Start redirecting the output.
System.setErr(new ConsoleRedirector(System.err));
System.setOut(new ConsoleRedirector(System.out));
}
// Restores the original settings.
public static void stop()
{
System.setErr(oldStderr);
System.setOut(oldStdout);
try
{
logfile.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
// PrintStream override.
@Override
public void write(int b)
{
try
{
logfile.write(b);
}
catch (Exception e)
{
e.printStackTrace();
setError();
}
super.write(b);
}
// PrintStream override.
@Override
public void write(byte [] buf, int off, int len)
{
try
{
logfile.write(buf, off, len);
}
catch (Exception e)
{
e.printStackTrace();
setError();
}
super.write(buf, off, len);
}
} |
Et tu l'utilise avec
ConsoleRedirector.start("Logs.txt");
et y'a stop() aussi
Partager