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.execution.LogController;
19  import net.sourceforge.acelogger.interpolator.JavaTextFormatInterpolator;
20  import net.sourceforge.acelogger.interpolator.TextInterpolator;
21  
22  /**
23   * TODO: Create Doc.
24   * 
25   * @author Zardi (https://sourceforge.net/users/daniel_zardi)
26   * @version 1.0.0
27   * @since 1.0.0
28   */
29  public class PatternFormatter extends BaseFormatter {
30  
31  	private boolean shouldPrintStackTrace;
32  
33  	private TextInterpolator formatInterpolator;
34  
35  	private String pattern;
36  
37  	public PatternFormatter(String identifier, String pattern) {
38  		this(identifier, pattern, true);
39  	}
40  
41  	public PatternFormatter(String identifier, String pattern, boolean shouldPrintStackTrace) {
42  		this(identifier, pattern, shouldPrintStackTrace, JavaTextFormatInterpolator.getInstance(),
43  				JavaTextFormatInterpolator.getInstance());
44  	}
45  
46  	public PatternFormatter(String identifier, String pattern, boolean shouldPrintStackTrace,
47  			TextInterpolator formatInterpolator, TextInterpolator messageInterpolator) {
48  		super(identifier, messageInterpolator);
49  		this.shouldPrintStackTrace = shouldPrintStackTrace;
50  		this.formatInterpolator = formatInterpolator;
51  		this.pattern = pattern;
52  	}
53  
54  	public String formatLogCall(LogEvent call) {
55  		String formattedCall = "";
56  		Object[] params = new Object[] {};
57  		try {
58  			formattedCall = formatMessage(call.getMessage(), call.getMessageParameters());
59  			// TODO: Define parameters passed to the formatter.
60  			params = new Object[] {
61  					formattedCall,
62  					call.getCallTime(),
63  					call.getLevel(),
64  					call.getLocation().getStackTraceLikeName()
65  				};
66  			formattedCall = formatInterpolator.interpolate(pattern, params);
67  			if (shouldPrintStackTrace) {
68  				formattedCall = formattedCall.concat(formatStackTrace(call.getCause()));
69  			}
70  		} catch (IllegalArgumentException e) {
71  			LogController.getInternalLogger().error(
72  					"It was not possible to format \"{0}\" with [{1}]", e, pattern, params);
73  		}
74  		return formattedCall;
75  	}
76  
77  }