Example usage for java.util.logging Level toString

List of usage examples for java.util.logging Level toString

Introduction

In this page you can find the example usage for java.util.logging Level toString.

Prototype

@Override
public final String toString() 

Source Link

Document

Returns a string representation of this Level.

Usage

From source file:gemlite.core.internal.db.AsyncEventHelper.java

/**
 * Log the given formatted message and exception to the provided logger. The
 * format expected is the same as supported by
 * {@link String#format(String, Object...)}.
 * //from   w  w  w . j  a  v a 2s . c om
 * @param logger
 *          the {@link Logger} to log the message to
 * @param level
 *          the {@link Level} to use for logging the message
 * @param t
 *          the exception whose backtrace is to be logged; can be null in
 *          which case it is ignored
 * @param format
 *          the message format
 * @param params
 *          the parameters to the message format
 * 
 * @see String#format(String, Object...)
 * @see Formatter#format(String, Object...)
 */
public final void logFormat(Logger logger, Level level, Throwable t, String format, Object... params) {
    StringBuilder sb = new StringBuilder();
    Formatter fmt = new Formatter(sb);
    fmt.format(format, params);
    if (t != null) {
        sb.append(": ");
        getStackTrace(t, sb);
    }
    if (Level.SEVERE.equals(level.toString()))
        logger.error(sb.toString());
    else
        logger.info(sb.toString());
    fmt.close();
}

From source file:org.hippoecm.repository.LoggingServlet.java

/**
 * Get a sorted map with sets "logger name" => <code>LoggerLevelInfo</code>
 * @return SortedMap<String, LoggerLevelInfo>
 *//* ww  w.  j a  v a2 s  . c om*/
private SortedMap<String, LoggerLevelInfo> getLoggerLevelInfosMap() {
    SortedMap<String, LoggerLevelInfo> loggerLevelInfosMap = new TreeMap<String, LoggerLevelInfo>();

    if (isJDK14Log) {
        java.util.logging.LogManager manager = java.util.logging.LogManager.getLogManager();

        for (Enumeration<String> namesIter = manager.getLoggerNames(); namesIter.hasMoreElements();) {
            String loggerName = namesIter.nextElement();
            java.util.logging.Logger logger = manager.getLogger(loggerName);
            java.util.logging.Level level = logger.getLevel();
            java.util.logging.Level effectiveLevel = level;

            // try to find effective level
            if (level == null) {
                for (java.util.logging.Logger l = logger; l != null; l = l.getParent()) {
                    if (l.getLevel() != null) {
                        effectiveLevel = l.getLevel();
                        break;
                    }
                }
            }

            if (level != null) {
                loggerLevelInfosMap.put(loggerName, new LoggerLevelInfo(loggerName, level.toString()));
            } else {
                loggerLevelInfosMap.put(loggerName,
                        new LoggerLevelInfo(loggerName, null, effectiveLevel.toString()));
            }
        }
    } else if (isLog4jLog) {
        try {
            // Log4j Classes
            Class<?> loggerClass = Class.forName("org.apache.log4j.Logger");
            Class<?> managerClass = Class.forName("org.apache.log4j.LogManager");

            // Log4j Methods
            Method getName = loggerClass.getMethod("getName", null);
            Method getLevel = loggerClass.getMethod("getLevel", null);
            Method getEffectiveLevel = loggerClass.getMethod("getEffectiveLevel", null);
            Method getLoggers = managerClass.getMethod("getCurrentLoggers", null);

            // get and sort loggers and log levels
            Enumeration loggers = (Enumeration) getLoggers.invoke(null, null);

            while (loggers.hasMoreElements()) {
                try {
                    Object logger = loggers.nextElement();
                    String loggerName = (String) getName.invoke(logger, null);
                    Object level = getLevel.invoke(logger, null);
                    Object effectiveLevel = getEffectiveLevel.invoke(logger, null);

                    if (level != null) {
                        loggerLevelInfosMap.put(loggerName, new LoggerLevelInfo(loggerName, level.toString()));
                    } else {
                        loggerLevelInfosMap.put(loggerName,
                                new LoggerLevelInfo(loggerName, null, effectiveLevel.toString()));
                    }
                } catch (Exception e) {
                    log.error("Error getting logger name and level : " + e.getMessage());
                }
            }
        } catch (Exception e) {
            log.error("Error getting log4j through reflection: " + e.getMessage(), e);
        }
    }

    return loggerLevelInfosMap;
}

From source file:net.rptools.maptool.launcher.MapToolLauncher.java

private JPanel buildTroubleshootingPanel() {
    final JPanel p = new JPanel();
    p.setLayout(new BorderLayout());

    ActionListener levelChange = new ActionListener() {
        @Override/*w  ww.j a v  a2  s . c o  m*/
        public void actionPerformed(ActionEvent e) {
            Level x = Level.parse(e.getActionCommand());
            if (Level.OFF.equals(x) || Level.INFO.equals(x) || Level.WARNING.equals(x)
                    || Level.SEVERE.equals(x))
                log.setLevel(x);
        }
    };
    JPanel logPanel = new JPanel();
    logPanel.setLayout(new GridLayout(0, 1));
    logPanel.setBorder(new TitledBorder(new LineBorder(Color.BLACK),
            CopiedFromOtherJars.getText("msg.logDetailPanel.border"))); //$NON-NLS-1$
    logPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

    ButtonGroup logGroup = new ButtonGroup();
    for (Level type : new Level[] { Level.OFF, Level.INFO, Level.WARNING, Level.SEVERE }) {
        JRadioButton jrb = new JRadioButton(type.toString());
        jrb.setActionCommand(type.toString());
        jrb.addActionListener(levelChange);
        jrb.setBorder(
                BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.red), jrb.getBorder()));
        logPanel.add(jrb);
        logGroup.add(jrb);
        if (type == Level.WARNING) {
            jrb.setSelected(true);
            log.setLevel(type);
        }
    }
    jcbEnableAssertions.setAlignmentX(Component.LEFT_ALIGNMENT);
    jcbEnableAssertions.setText(CopiedFromOtherJars.getText("msg.info.enableAssertions")); //$NON-NLS-1$
    jcbEnableAssertions.setToolTipText(CopiedFromOtherJars.getText("msg.tooltip.enableAssertions")); //$NON-NLS-1$
    jcbEnableAssertions.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                if (!extraArgs.contains(ASSERTIONS_OPTION)) {
                    extraArgs = (ASSERTIONS_OPTION + " " + extraArgs); //$NON-NLS-1$
                }
            } else if (e.getStateChange() == ItemEvent.DESELECTED) {
                extraArgs = extraArgs.replace(ASSERTIONS_OPTION, ""); //$NON-NLS-1$
            }
            extraArgs = extraArgs.trim();
            jtfArgs.setText(extraArgs);
            updateCommand();
        }
    });
    p.add(logPanel, BorderLayout.NORTH);
    Box other = new Box(BoxLayout.PAGE_AXIS);
    other.add(jcbEnableAssertions);
    other.add(Box.createVerticalGlue());
    p.add(other, BorderLayout.CENTER);
    return p;
}