1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.util;
22
23 import java.io.*;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.util.logging.LogManager;
27 /***
28 * Utility methods for loggin
29 * @author TiongHiang Lee (thlee@onemindsoft.org)
30 * @version $Id: LogUtils.java,v 1.4 2005/08/08 05:21:51 thlee Exp $ $Name: $
31 */
32 public final class LogUtils
33 {
34
35 /***
36 * {@inheritDoc}
37 */
38 private LogUtils()
39 {
40 }
41
42 /***
43 * Init java logging by looking for logging.properties in the class paths
44 */
45 public static void initLoggingFromClassPath()
46 {
47 String classPath = (String) System.getProperties().get("java.class.path");
48 String[] paths = null;
49 if (classPath.indexOf(";") != -1)
50 {
51 paths = classPath.split(";");
52 } else
53 {
54 paths = classPath.split(":");
55 }
56 for (int i = 0; i < paths.length; i++)
57 {
58 if (!paths[i].endsWith(".jar"))
59 {
60 String config = FileUtils.concatFilePath(paths[i], "logging.properties");
61 if (new File(config).exists())
62 {
63
64 try
65 {
66 LogManager.getLogManager().readConfiguration(new FileInputStream(config));
67 } catch (Exception e)
68 {
69
70
71 }
72 }
73 }
74 }
75 }
76
77 /***
78 * Get the stack trace in a string
79 * @param e the exception
80 * @return the stack trace string
81 */
82 public static String getTrace(Throwable e)
83 {
84 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
85 PrintStream ps = new PrintStream(buffer);
86 e.printStackTrace(ps);
87 ps.flush();
88 return new String(buffer.toByteArray());
89 }
90 }