View Javadoc

1   package org.lsst.ccs.utilities.logging;
2   
3   import org.lsst.ccs.utilities.beanutils.Optional;
4   
5   import java.awt.*;
6   import java.util.logging.*;
7   
8   /**
9    * Ths static initialization of this class should be fired early in utilities code by doing
10   * <PRE>
11   *     LogManagement.isConfigInitialized() ;
12   * </PRE>
13   * but this code is not necessary in application code (since the <TT>Logger</TT> calls
14   * fire the load-time operation).
15   *
16   * @author bamade
17   */
18  // Date: 31/05/13
19  
20  public class LogManagement {
21      static {
22          Optional<LogManager> optManager =  LogPropertiesLoader.getSystemLoaderLogManager();
23      }
24      /**
25       * a simple global <TT>ErrorManager</TT> that print logging framework problems to the console.
26       * Should be changed to something that fires a network message
27       */
28      public static final ErrorManager ERROR_MANAGER = new ErrorManager();
29  
30      /**
31       * dummy static method to fire the load-time operation.
32       * // todo : get rid of that?
33       * @return
34       */
35      public static boolean isConfigInitialized() {
36          return true ;
37      }
38  
39      /**
40       * if a System Class Loader loads the standard LOgManager and the manager reads
41       * the standard "loggin.properties" and initialize the Handlers we may end up with
42       * two different Console Handlers.
43       * For this purpose once we enter the realm of ccs.utilities we get rid of the
44       * standard ConsoleHandler.
45       * <BR/>
46       * gets the top logger (name "") and removes the standard <TT>ConsoleHandler</TT>
47       */
48      //TODO: get rid of ALL ConsoleHandlers (inclusing those that are linked to specific Loggers ... un unlikely occurrence ... but who knows?
49      public static Level cleanConsole() {
50          //this is to get rid of other Consoles at the top
51          java.util.logging.Logger globalHandler = java.util.logging.Logger.getLogger("");
52          Handler[] handlers = globalHandler.getHandlers();
53          // BEWARE used to know if ConsoleHandler was there!
54          Level consoleLevel = null;
55          for (Handler handler : handlers) {
56              if (handler.getClass().equals(ConsoleHandler.class)) {
57                  consoleLevel = handler.getLevel();
58                  globalHandler.removeHandler(handler);
59              }
60          }
61          return consoleLevel;
62      }
63  
64      /**
65       * to be used by some non-graphic codes
66       * @param consoleLevel
67       * //TODO: deprecate this method?
68       */
69      public static void setDefaultConsoleHandler(Level consoleLevel) {
70          java.util.logging.Logger globalHandler = java.util.logging.Logger.getLogger("");
71          if (consoleLevel != null && !GraphicsEnvironment.isHeadless()) {
72              Handler consoleHandlerN = new ConsoleHandlerN();
73              consoleHandlerN.setLevel(consoleLevel);
74              globalHandler.addHandler(consoleHandlerN);
75          }
76      }
77  
78  
79  }