View Javadoc

1   /* 
2    * Copyright (c) 2012, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.api.logging;
36  
37  
38  import java.text.MessageFormat;
39  
40  /**
41   * A simple log message that can be used in conjunction with Log4J in order to improve logging speed. A LogMessage
42   * takes a message and a number of arguments that are formated with {@link MessageFormat} on demand, i.e. the
43   * rendering of the resulting message will only take place if the message is really logged by the Log4J
44   * framework, i.e. the log level is set accordingly.
45   * 
46   * @author owaeld
47   */
48  public class LogMessage
49  {
50      private String pattern;
51  
52      private Object[] arguments;
53  
54      /**
55       * Creates a new log message.
56       * 
57       * @param message
58       *            the message pattern
59       * @param params
60       *            the message parameters
61       * 
62       * @see MessageFormat#format(String, Object...)
63       */
64      public LogMessage( String message, Object... params )
65      {
66          pattern = message;
67          arguments = params;
68      }
69  
70      /**
71       * Creates a new log message.
72       * 
73       * @param message
74       *            the message pattern
75       * @param params
76       *            the message parameters
77       * 
78       * @return the new {@link LogEntry}
79       * 
80       * @see MessageFormat#format(String, Object...)
81       */
82      public static LogMessage getMessage( String message, Object... params )
83      {
84          return new LogMessage( message, params );
85      }
86  
87      /**
88       * Formats a log message to a string.
89       * 
90       * @param message
91       *            the message pattern
92       * @param params
93       *            the message parameters
94       * 
95       * @return the new formated message
96       * 
97       * @see MessageFormat#format(String, Object...)
98       */
99      public static String format( String message, Object... params )
100     {
101         return new LogMessage( message, params ).toString();
102     }
103     
104     /**
105      * Renders the message when required by Log4J, i.e. the message is finally printed to the log.
106      * 
107      * @return the rendered message
108      */
109     @Override
110     public String toString()
111     {
112         return MessageFormat.format( pattern, arguments );
113     }
114 }