View Javadoc

1   /*
2    * This file is part of AceLogger.
3    * 
4    * AceLogger is free software: you can redistribute it and/or modify it under the terms of the GNU
5    * Lesser General Public License as published by the Free Software Foundation, either version 3 of
6    * the License, or (at your option) any later version.
7    * 
8    * AceLogger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9    * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10   * Lesser General Public License for more details.
11   * 
12   * You should have received a copy of the GNU Lesser General Public License along with AceLogger.
13   * If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
14   */
15  package net.sourceforge.acelogger.formatter;
16  
17  import net.sourceforge.acelogger.LogEvent;
18  import net.sourceforge.acelogger.constants.FormatterConstants;
19  import net.sourceforge.acelogger.interpolator.JavaTextFormatInterpolator;
20  
21  /**
22   * An implementation of {@link Formatter} that formats the log message and outputs the date,
23   * location and message.
24   * 
25   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
26   * @version 1.0.0
27   * @since 1.0.0
28   */
29  public class SimpleFormatter extends BaseFormatter {
30  
31  	/**
32  	 * The pattern of our simple message.
33  	 */
34  	private static final String LOG_ENTRY = "{0,date,yyyy-MM-dd HH:mm:ss,SSS} [{1}] ({2}) - {3}"
35  			+ FormatterConstants.NEW_LINE;
36  
37  	/**
38  	 * Constructs a new SimpleFormatter with the supplied identifier.
39  	 * 
40  	 * @param identifier
41  	 *            The string that identifies this formatter.
42  	 * @since 1.0.0
43  	 */
44  	public SimpleFormatter(String identifier) {
45  		super(identifier, JavaTextFormatInterpolator.getInstance());
46  	}
47  
48  	/** {@inheritDoc} */
49  	public String formatLogCall(LogEvent call) {
50  		String formattedCall = "";
51  		if (call != null) {
52  			String formattedMessage = formatMessage(call.getMessage(), call.getMessageParameters());
53  			String stackTraceLikeName = call.getLocation().getStackTraceLikeName();
54  			if (stackTraceLikeName.length() == 0) {
55  				stackTraceLikeName = "Source not found";
56  			}
57  			formattedCall = formatMessage(LOG_ENTRY, call.getCallTime(), call.getLevel(),
58  					stackTraceLikeName, formattedMessage);
59  			formattedCall = formattedCall.concat(formatStackTrace(call.getCause()));
60  		}
61  		return formattedCall;
62  	}
63  
64  }