View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
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          { //must be windows
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                      //System.out.println("Use logging configuration " + config);
64                      try
65                      {
66                          LogManager.getLogManager().readConfiguration(new FileInputStream(config));
67                      } catch (Exception e)
68                      {
69                          //nothing it can do
70                          //System.out.println("- init logging problem: " + e.getMessage());
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  }