View Javadoc
1   package com.acumenvelocity.ath.common;
2   
3   import java.util.Map;
4   import java.util.concurrent.ConcurrentHashMap;
5   
6   import org.slf4j.Logger;
7   import org.slf4j.LoggerFactory;
8   import org.slf4j.helpers.MessageFormatter;
9   
10  /**
11   * Utility class for application logging using SLF4J framework.
12   * Provides a simplified interface for logging with automatic logger management
13   * and message formatting capabilities.
14   * 
15   * <p>
16   * This class maintains a cache of Logger instances per class to avoid
17   * repeated logger lookups and improve performance.
18   * </p>
19   * 
20   * @author Acumen Velocity
21   * @version 1.0
22   * @since 1.0
23   * @see Logger
24   * @see LoggerFactory
25   */
26  public class Log {
27    // Thread-safe cache of Logger instances keyed by class
28    private static Map<Class<?>, Logger> loggers = new ConcurrentHashMap<>();
29  
30    /**
31     * Retrieves or creates a Logger instance for the specified class.
32     * Uses concurrent map for thread-safe logger caching.
33     * 
34     * @param loggingClass the class for which to get the logger
35     * @return Logger instance for the specified class
36     */
37    private static Logger getLogger(Class<?> loggingClass) {
38      Logger logger = loggers.get(loggingClass);
39      if (logger == null) {
40        logger = LoggerFactory.getLogger(loggingClass);
41        loggers.put(loggingClass, logger);
42      }
43  
44      return logger;
45    }
46  
47    /**
48     * Logs a debug level message with parameterized formatting.
49     * 
50     * @param loggingClass the class from which the log originates
51     * @param format       the message format string
52     * @param arguments    the arguments for the format string
53     */
54    public static void debug(Class<?> loggingClass, String format, Object... arguments) {
55      getLogger(loggingClass).debug(format, arguments);
56    }
57  
58    /**
59     * Logs an error level message with parameterized formatting.
60     * 
61     * @param loggingClass the class from which the log originates
62     * @param format       the message format string
63     * @param arguments    the arguments for the format string
64     */
65    public static void error(Class<?> loggingClass, boolean printStackTrace, String format,
66        Object... arguments) {
67  
68      if (printStackTrace) {
69        getLogger(loggingClass).error(format, arguments);
70  
71      } else {
72        System.err.println("[ERROR] " + Log.format(format, arguments));
73      }
74    }
75  
76    /**
77     * Logs an error level message with parameterized formatting.
78     * 
79     * @param loggingClass the class from which the log originates
80     * @param format       the message format string
81     * @param arguments    the arguments for the format string
82     */
83    public static void error(Class<?> loggingClass, String format, Object... arguments) {
84      error(loggingClass, true, format, arguments);
85    }
86  
87    /**
88     * Logs an error level message with exception details and parameterized formatting.
89     * Includes both the formatted message and exception information, plus stack trace.
90     * 
91     * @param loggingClass the class from which the log originates
92     * @param cause        the Throwable that caused the error
93     * @param format       the message format string
94     * @param arguments    the arguments for the format string
95     */
96    public static void error(Class<?> loggingClass, Throwable cause, String format,
97        Object... arguments) {
98  
99      getLogger(loggingClass).error("{} -- {}", format(format, arguments), cause.toString());
100     cause.printStackTrace(); // Also print stack trace to standard error
101   }
102 
103   public static void error(Class<?> loggingClass, Throwable cause, boolean printStackTrace,
104       String format, Object... arguments) {
105 
106     getLogger(loggingClass).error("{} -- {}", format(format, arguments), cause.toString());
107 
108     // the stack trace is printed by logback ()
109     // if (printStackTrace) {
110     // cause.printStackTrace(); // Also print stack trace to standard error
111     // }
112   }
113 
114   /**
115    * Logs an info level message with parameterized formatting.
116    * 
117    * @param loggingClass the class from which the log originates
118    * @param format       the message format string
119    * @param arguments    the arguments for the format string
120    */
121   public static void info(Class<?> loggingClass, String format, Object... arguments) {
122     getLogger(loggingClass).info(format, arguments);
123   }
124 
125   /**
126    * Logs a trace level message with parameterized formatting.
127    * 
128    * @param loggingClass the class from which the log originates
129    * @param format       the message format string
130    * @param arguments    the arguments for the format string
131    */
132   public static void trace(Class<?> loggingClass, String format, Object... arguments) {
133     getLogger(loggingClass).trace(format, arguments);
134   }
135 
136   /**
137    * Logs a warn level message with parameterized formatting.
138    * 
139    * @param loggingClass the class from which the log originates
140    * @param format       the message format string
141    * @param arguments    the arguments for the format string
142    */
143   public static void warn(Class<?> loggingClass, String format, Object... arguments) {
144     getLogger(loggingClass).warn(format, arguments);
145   }
146 
147   /**
148    * Formats a message string with parameters using SLF4J-style parameter substitution.
149    * 
150    * @param format    the message format string with {} placeholders
151    * @param arguments the values to substitute into the placeholders
152    * @return formatted message string
153    */
154   public static String format(String format, Object... arguments) {
155     return MessageFormatter.arrayFormat(format, arguments).getMessage();
156   }
157 
158   /**
159    * Checks if trace level logging is enabled for the specified class.
160    * 
161    * @param loggingClass the class to check
162    * @return true if trace level is enabled, false otherwise
163    */
164   public static boolean isTraceEnabled(Class<?> loggingClass) {
165     return getLogger(loggingClass).isTraceEnabled();
166   }
167 
168   /**
169    * Checks if debug level logging is enabled for the specified class.
170    * 
171    * @param loggingClass the class to check
172    * @return true if debug level is enabled, false otherwise
173    */
174   public static boolean isDebugEnabled(Class<?> loggingClass) {
175     return getLogger(loggingClass).isDebugEnabled();
176   }
177 }