View Javadoc

1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    *
4    * Copyright (C) 1999-2006, QOS.ch
5    *
6    * This library is free software, you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation.
9    */
10  package ch.qos.logback.classic.pattern;
11  
12  import ch.qos.logback.classic.spi.LoggingEvent;
13  
14  public abstract class NamedConverter extends ClassicConverter {
15  
16    Abbreviator abbreviator = null;
17  
18    /**
19     * Gets fully qualified name from event.
20     * 
21     * @param event
22     *          The LoggingEvent to process, cannot not be null.
23     * @return name, must not be null.
24     */
25    protected abstract String getFullyQualifiedName(final LoggingEvent event);
26  
27    public void start() {
28      String optStr = getFirstOption();
29      if (optStr != null) {
30        try {
31          int targetLen = Integer.parseInt(optStr);
32          if (targetLen == 0) {
33            abbreviator = new ClassNameOnlyAbbreviator();
34          } else if (targetLen > 0) {
35            abbreviator = new TargetLengthBasedClassNameAbbreviator(targetLen);
36          }
37        } catch (NumberFormatException nfe) {
38          // FIXME: better error reporting
39        }
40      }
41    }
42  
43    public String convert(LoggingEvent event) {
44      String fqn = getFullyQualifiedName(event);
45  
46      if (abbreviator == null) {
47        return fqn;
48      } else {
49        return abbreviator.abbreviate(fqn);
50      }
51    }
52  }