View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.pattern;
15  
16  import ch.qos.logback.classic.spi.ILoggingEvent;
17  
18  public abstract class NamedConverter extends ClassicConverter {
19  
20      Abbreviator abbreviator = null;
21  
22      /**
23       * Gets fully qualified name from event.
24       * 
25       * @param event
26       *          The LoggingEvent to process, cannot not be null.
27       * @return name, must not be null.
28       */
29      protected abstract String getFullyQualifiedName(final ILoggingEvent event);
30  
31      public void start() {
32          String optStr = getFirstOption();
33          if (optStr != null) {
34              try {
35                  int targetLen = Integer.parseInt(optStr);
36                  if (targetLen == 0) {
37                      abbreviator = new ClassNameOnlyAbbreviator();
38                  } else if (targetLen > 0) {
39                      abbreviator = new TargetLengthBasedClassNameAbbreviator(targetLen);
40                  }
41              } catch (NumberFormatException nfe) {
42                  // FIXME: better error reporting
43              }
44          }
45      }
46  
47      public String convert(ILoggingEvent event) {
48          String fqn = getFullyQualifiedName(event);
49  
50          if (abbreviator == null) {
51              return fqn;
52          } else {
53              return abbreviator.abbreviate(fqn);
54          }
55      }
56  }