View Javadoc

1   /*-
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2012 - 2019 Prolobjectlink Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jtrolog;
23  
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  import java.util.logging.Formatter;
27  import java.util.logging.Level;
28  import java.util.logging.LogRecord;
29  
30  import io.github.prolobjectlink.prolog.PrologFormatter;
31  
32  /**
33   * 
34   * @author Jose Zalacain
35   * @since 1.0
36   */
37  final class JTrologFormatter extends Formatter implements PrologFormatter {
38  
39  	private static final String ERROR = "ERROR";
40  	private static final String WARN = "WARN";
41  	private static final String INFO = "INFO";
42  	private static final String DEBUG = "DEBUG";
43  	private static final String TRACE = "TRACE";
44  
45  	@Override
46  	public String format(LogRecord record) {
47  		StringBuilder buffer = new StringBuilder();
48  
49  		// retrieve record data
50  		String levelString = "";
51  		Level level = record.getLevel();
52  		if (level.intValue() == Level.SEVERE.intValue()) {
53  			levelString = ERROR;
54  		} else if (level.intValue() == Level.WARNING.intValue()) {
55  			levelString = WARN;
56  		} else if (level.intValue() == Level.INFO.intValue()) {
57  			levelString = INFO;
58  		} else if (level.intValue() == Level.CONFIG.intValue()) {
59  			levelString = DEBUG;
60  		} else if (level.intValue() == Level.FINE.intValue()) {
61  			levelString = DEBUG;
62  		} else if (level.intValue() == Level.FINER.intValue()) {
63  			levelString = DEBUG;
64  		} else if (level.intValue() == Level.FINEST.intValue()) {
65  			levelString = TRACE;
66  		}
67  
68  		long millisecs = record.getMillis();
69  		String name = record.getLoggerName();
70  		String message = record.getMessage();
71  		Throwable throwable = record.getThrown();
72  		SimpleDateFormat f = new SimpleDateFormat("MMM-dd-yyyy HH:mm");
73  		Date resultdate = new Date(millisecs);
74  		String date = f.format(resultdate);
75  
76  		// create log line
77  		buffer.append('[');
78  		buffer.append(name);
79  		buffer.append(']');
80  		buffer.append(' ');
81  		buffer.append(levelString);
82  		buffer.append(' ');
83  		buffer.append(date);
84  		buffer.append(' ');
85  		buffer.append(message);
86  		if (throwable != null) {
87  			buffer.append('\n');
88  			buffer.append(throwable);
89  			buffer.append('\n');
90  			StackTraceElement[] s = throwable.getStackTrace();
91  			for (StackTraceElement stackTraceElement : s) {
92  				buffer.append(stackTraceElement);
93  				buffer.append('\n');
94  			}
95  		}
96  		buffer.append('\n');
97  		return "" + buffer + "";
98  	}
99  
100 }