ConsoleLogFormatter.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.log;
  16. import java.io.PrintWriter;
  17. import java.io.StringWriter;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.logging.Formatter;
  21. import java.util.logging.LogRecord;
  22. import javolution.text.TextBuilder;
  23. /**
  24. * This class ...
  25. *
  26. * @version $Revision: 1.1.4.2 $ $Date: 2005/03/27 15:30:08 $
  27. */
  28. public class ConsoleLogFormatter extends Formatter
  29. {
  30. /*
  31. * (non-Javadoc)
  32. *
  33. * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
  34. */
  35. private static final String CRLF = "\r\n";
  36. private SimpleDateFormat dateFmt = new SimpleDateFormat("dd MMM H:mm:ss");
  37. public String format(LogRecord record)
  38. {
  39. TextBuilder output = new TextBuilder();
  40. output.append('[');
  41. output.append(dateFmt.format(new Date(record.getMillis())));
  42. output.append(']');
  43. output.append(' ');
  44. output.append(record.getMessage());
  45. output.append(CRLF);
  46. if (record.getThrown() != null)
  47. {
  48. try
  49. {
  50. StringWriter sw = new StringWriter();
  51. PrintWriter pw = new PrintWriter(sw);
  52. record.getThrown().printStackTrace(pw);
  53. pw.close();
  54. output.append(sw.toString());
  55. output.append(CRLF);
  56. }
  57. catch (Exception ex)
  58. {
  59. }
  60. }
  61. return output.toString();
  62. }
  63. }