001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.pattern;
019
020import org.apache.log4j.Level;
021import org.apache.log4j.spi.LoggingEvent;
022
023
024/**
025 * Return the event's level in a StringBuffer.
026 *
027 * @author Ceki Gülcü
028 */
029public final class LevelPatternConverter extends LoggingEventPatternConverter {
030
031  /**
032   *   Integer severity for Level.TRACE.
033   */
034  private static final int TRACE_INT = 5000;
035  /**
036   * Singleton.
037   */
038  private static final LevelPatternConverter INSTANCE =
039    new LevelPatternConverter();
040
041  /**
042   * Private constructor.
043   */
044  private LevelPatternConverter() {
045    super("Level", "level");
046  }
047
048  /**
049   * Obtains an instance of pattern converter.
050   * @param options options, may be null.
051   * @return instance of pattern converter.
052   */
053  public static LevelPatternConverter newInstance(
054    final String[] options) {
055    return INSTANCE;
056  }
057
058  /**
059   * {@inheritDoc}
060   */
061  public void format(final LoggingEvent event, final StringBuffer output) {
062    output.append(event.getLevel().toString());
063  }
064
065  /**
066   * {@inheritDoc}
067   */
068  public String getStyleClass(Object e) {
069    if (e instanceof LoggingEvent) {
070      int lint = ((LoggingEvent) e).getLevel().toInt();
071
072      switch (lint) {
073      case TRACE_INT:
074        return "level trace";
075
076      case Level.DEBUG_INT:
077        return "level debug";
078
079      case Level.INFO_INT:
080        return "level info";
081
082      case Level.WARN_INT:
083        return "level warn";
084
085      case Level.ERROR_INT:
086        return "level error";
087
088      case Level.FATAL_INT:
089        return "level fatal";
090
091      default:
092        return "level " + ((LoggingEvent) e).getLevel().toString();
093      }
094    }
095
096    return "level";
097  }
098}