src/core/net/sf/basedb/util/extensions/logging/ExtensionsLogger.java

Code
Comments
Other
Rev Date Author Line
8125 09 Mar 23 nicklas 1 /**
8125 09 Mar 23 nicklas 2   $Id$
8125 09 Mar 23 nicklas 3
8125 09 Mar 23 nicklas 4   Copyright (C) 2023 Nicklas Nordborg
8125 09 Mar 23 nicklas 5
8125 09 Mar 23 nicklas 6   This file is part of BASE - BioArray Software Environment.
8125 09 Mar 23 nicklas 7   Available at http://base.thep.lu.se/
8125 09 Mar 23 nicklas 8
8125 09 Mar 23 nicklas 9   BASE is free software; you can redistribute it and/or
8125 09 Mar 23 nicklas 10   modify it under the terms of the GNU General Public License
8125 09 Mar 23 nicklas 11   as published by the Free Software Foundation; either version 3
8125 09 Mar 23 nicklas 12   of the License, or (at your option) any later version.
8125 09 Mar 23 nicklas 13
8125 09 Mar 23 nicklas 14   BASE is distributed in the hope that it will be useful,
8125 09 Mar 23 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
8125 09 Mar 23 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8125 09 Mar 23 nicklas 17   GNU General Public License for more details.
8125 09 Mar 23 nicklas 18
8125 09 Mar 23 nicklas 19   You should have received a copy of the GNU General Public License
8125 09 Mar 23 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
8125 09 Mar 23 nicklas 21 */
8124 09 Mar 23 nicklas 22 package net.sf.basedb.util.extensions.logging;
8116 21 Feb 23 nicklas 23
8116 21 Feb 23 nicklas 24 /**
8116 21 Feb 23 nicklas 25   A simple logger that services can use to send log message to the BASE
8116 21 Feb 23 nicklas 26   core. An instance is handed out to the service by the {@link 
8125 09 Mar 23 nicklas 27   ServiceControllerAction#start(ExtensionsLogger)} method, which the service
8116 21 Feb 23 nicklas 28   can use as long as it is running.
8116 21 Feb 23 nicklas 29   <p>
8116 21 Feb 23 nicklas 30   Note that the BASE core will automatically send INFO-level messages
8116 21 Feb 23 nicklas 31   when the service is started and stopped.
8116 21 Feb 23 nicklas 32   <p>
8116 21 Feb 23 nicklas 33   The log is kept in memory only and will not survive server restarts.
8116 21 Feb 23 nicklas 34   The number of log entries is also limited. When the limit is reached
8116 21 Feb 23 nicklas 35   the oldest entries are automatically removed from the log.
8116 21 Feb 23 nicklas 36   
8116 21 Feb 23 nicklas 37   @author nicklas
8116 21 Feb 23 nicklas 38   @since 3.19.8
8116 21 Feb 23 nicklas 39 */
8125 09 Mar 23 nicklas 40 public class ExtensionsLogger 
8116 21 Feb 23 nicklas 41 {
8125 09 Mar 23 nicklas 42   private final ExtensionsLog log;
8121 07 Mar 23 nicklas 43   private final org.slf4j.Logger otherLogger;
8116 21 Feb 23 nicklas 44   
8125 09 Mar 23 nicklas 45   ExtensionsLogger(ExtensionsLog log)
8117 22 Feb 23 nicklas 46   {
8121 07 Mar 23 nicklas 47     this(log, null);
8121 07 Mar 23 nicklas 48   }
8121 07 Mar 23 nicklas 49   
8125 09 Mar 23 nicklas 50   ExtensionsLogger(ExtensionsLog log, org.slf4j.Logger otherLogger)
8121 07 Mar 23 nicklas 51   {
8118 22 Feb 23 nicklas 52     this.log = log;
8121 07 Mar 23 nicklas 53     this.otherLogger = otherLogger;
8117 22 Feb 23 nicklas 54   }
8116 21 Feb 23 nicklas 55   
8121 07 Mar 23 nicklas 56   /**
8121 07 Mar 23 nicklas 57     Wrap a generic SLF4J logger with this logger. Messages
8121 07 Mar 23 nicklas 58     that are submitted to the logger created by this method are
8121 07 Mar 23 nicklas 59     also forwarded to the SLF4J logger.
8121 07 Mar 23 nicklas 60   */
8125 09 Mar 23 nicklas 61   public ExtensionsLogger wrap(org.slf4j.Logger otherLogger)
8121 07 Mar 23 nicklas 62   {
8125 09 Mar 23 nicklas 63     return new ExtensionsLogger(this.log, otherLogger);
8121 07 Mar 23 nicklas 64   }
8121 07 Mar 23 nicklas 65   
8121 07 Mar 23 nicklas 66   /**
8123 09 Mar 23 nicklas 67     Check if DEBUG logging has been enabled or not. Debug logging
8125 09 Mar 23 nicklas 68     can be enabled either by the {@link ExtensionsLog#setDebugEnabled(boolean)}
8123 09 Mar 23 nicklas 69     method or by a wrapped {@link Logger}.
8121 07 Mar 23 nicklas 70   */
8118 22 Feb 23 nicklas 71   public boolean isDebugEnabled()
8116 21 Feb 23 nicklas 72   {
8123 09 Mar 23 nicklas 73     return log.isDebugEnabled() || otherLogger != null && otherLogger.isDebugEnabled();
8116 21 Feb 23 nicklas 74   }
8116 21 Feb 23 nicklas 75   
8121 07 Mar 23 nicklas 76   /**
8123 09 Mar 23 nicklas 77     Check if TRACE logging has been enabled or not. Trace logging is enabled
8125 09 Mar 23 nicklas 78     only if both DEBUG logging and {@link ExtensionsLog#isCopyingToStdout()}
8123 09 Mar 23 nicklas 79     are enabled or if a wrapped {@link Logger} has it enabled.
8123 09 Mar 23 nicklas 80   */
8123 09 Mar 23 nicklas 81   public boolean isTraceEnabled()
8123 09 Mar 23 nicklas 82   {
8123 09 Mar 23 nicklas 83     return (log.isCopyingToStdout() && isDebugEnabled()) || 
8123 09 Mar 23 nicklas 84       (otherLogger != null && otherLogger.isTraceEnabled());
8123 09 Mar 23 nicklas 85   }
8123 09 Mar 23 nicklas 86   
8123 09 Mar 23 nicklas 87   /**
8121 07 Mar 23 nicklas 88     Log a message at the specified level.
8121 07 Mar 23 nicklas 89   */
8118 22 Feb 23 nicklas 90   public void log(LogLevel level, String msg)
8116 21 Feb 23 nicklas 91   {
8118 22 Feb 23 nicklas 92     log.add(new LogEntry(level, msg, null));
8121 07 Mar 23 nicklas 93     if (otherLogger != null) level.logTo(otherLogger, msg, null);
8116 21 Feb 23 nicklas 94   }
8116 21 Feb 23 nicklas 95   
8121 07 Mar 23 nicklas 96   /**
8121 07 Mar 23 nicklas 97     Log a message at the specified level with an optional stacktrace.
8121 07 Mar 23 nicklas 98   */
8121 07 Mar 23 nicklas 99   public void log(LogLevel level, String msg, Throwable stacktrace)
8116 21 Feb 23 nicklas 100   {
8121 07 Mar 23 nicklas 101     log.add(new LogEntry(level, msg, stacktrace));
8121 07 Mar 23 nicklas 102     if (otherLogger != null) level.logTo(otherLogger, msg, stacktrace);
8116 21 Feb 23 nicklas 103   }
8118 22 Feb 23 nicklas 104   
8121 07 Mar 23 nicklas 105   /**
8121 07 Mar 23 nicklas 106     Log an INFO message.
8121 07 Mar 23 nicklas 107   */
8121 07 Mar 23 nicklas 108   public void info(String msg)
8121 07 Mar 23 nicklas 109   {
8121 07 Mar 23 nicklas 110     log(LogLevel.INFO, msg);
8121 07 Mar 23 nicklas 111   }
8121 07 Mar 23 nicklas 112   /**
8121 07 Mar 23 nicklas 113     Log an INFO message with an optional stacktrace.
8121 07 Mar 23 nicklas 114   */
8121 07 Mar 23 nicklas 115   public void info(String msg, Throwable stacktrace)
8121 07 Mar 23 nicklas 116   {
8121 07 Mar 23 nicklas 117     log(LogLevel.INFO, msg, stacktrace);
8121 07 Mar 23 nicklas 118   }
8121 07 Mar 23 nicklas 119   
8121 07 Mar 23 nicklas 120   /**
8121 07 Mar 23 nicklas 121     Log a WARNING message.
8121 07 Mar 23 nicklas 122   */
8126 09 Mar 23 nicklas 123   public void warn(String msg)
8121 07 Mar 23 nicklas 124   {
8121 07 Mar 23 nicklas 125     log(LogLevel.WARNING, msg);
8121 07 Mar 23 nicklas 126   }
8121 07 Mar 23 nicklas 127   /**
8121 07 Mar 23 nicklas 128     Log a WARNING message with an optional stacktrace.
8121 07 Mar 23 nicklas 129   */
8126 09 Mar 23 nicklas 130   public void warn(String msg, Throwable t)
8121 07 Mar 23 nicklas 131   {
8121 07 Mar 23 nicklas 132     log(LogLevel.WARNING, msg, t);
8121 07 Mar 23 nicklas 133   }
8121 07 Mar 23 nicklas 134   
8121 07 Mar 23 nicklas 135   /**
8121 07 Mar 23 nicklas 136     Log an ERROR message.
8121 07 Mar 23 nicklas 137   */
8121 07 Mar 23 nicklas 138   public void error(String msg)
8121 07 Mar 23 nicklas 139   {
8121 07 Mar 23 nicklas 140     log(LogLevel.ERROR, msg);
8121 07 Mar 23 nicklas 141   }
8121 07 Mar 23 nicklas 142   /**
8121 07 Mar 23 nicklas 143     Log an ERROR message with an optional stacktrace.
8121 07 Mar 23 nicklas 144   */
8121 07 Mar 23 nicklas 145   public void error(String msg, Throwable t)
8121 07 Mar 23 nicklas 146   {
8121 07 Mar 23 nicklas 147     log(LogLevel.ERROR, msg, t);
8121 07 Mar 23 nicklas 148   }
8121 07 Mar 23 nicklas 149   
8121 07 Mar 23 nicklas 150   /**
8121 07 Mar 23 nicklas 151     Log a DEBUG message.
8121 07 Mar 23 nicklas 152   */
8121 07 Mar 23 nicklas 153   public void debug(String msg)
8121 07 Mar 23 nicklas 154   {
8121 07 Mar 23 nicklas 155     log(LogLevel.DEBUG, msg);
8121 07 Mar 23 nicklas 156   }
8121 07 Mar 23 nicklas 157   /**
8121 07 Mar 23 nicklas 158     Log a DEBUG message with an optional stacktrace.
8121 07 Mar 23 nicklas 159   */
8121 07 Mar 23 nicklas 160   public void debug(String msg, Throwable t)
8121 07 Mar 23 nicklas 161   {
8121 07 Mar 23 nicklas 162     log(LogLevel.DEBUG, msg, t);
8121 07 Mar 23 nicklas 163   }
8121 07 Mar 23 nicklas 164   
8121 07 Mar 23 nicklas 165   /**
8121 07 Mar 23 nicklas 166     Log a TRACE message with the related SLF4J logger. 
8121 07 Mar 23 nicklas 167     If no SLF4J logger is related 
8121 07 Mar 23 nicklas 168     (see {@link #wrap(org.slf4j.Logger)}) all TRACE messages
8121 07 Mar 23 nicklas 169     will be ignored.
8121 07 Mar 23 nicklas 170   */
8121 07 Mar 23 nicklas 171   public void trace(String msg)
8121 07 Mar 23 nicklas 172   {
8123 09 Mar 23 nicklas 173     log(LogLevel.TRACE, msg);
8121 07 Mar 23 nicklas 174   }
8121 07 Mar 23 nicklas 175   /**
8121 07 Mar 23 nicklas 176     Log a TRACE message with an optional stacktrace.
8121 07 Mar 23 nicklas 177     If no SLF4J logger is related 
8121 07 Mar 23 nicklas 178     (see {@link #wrap(org.slf4j.Logger)}) all TRACE messages
8121 07 Mar 23 nicklas 179     will be ignored.
8121 07 Mar 23 nicklas 180   */
8121 07 Mar 23 nicklas 181   public void trace(String msg, Throwable t)
8121 07 Mar 23 nicklas 182   {
8123 09 Mar 23 nicklas 183     log(LogLevel.TRACE, msg, t);
8121 07 Mar 23 nicklas 184   }
8121 07 Mar 23 nicklas 185   
8116 21 Feb 23 nicklas 186 }