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 java.net.InetAddress;
13  import java.net.UnknownHostException;
14  import java.text.DateFormatSymbols;
15  import java.text.SimpleDateFormat;
16  import java.util.Date;
17  import java.util.Locale;
18  
19  import ch.qos.logback.classic.spi.LoggingEvent;
20  import ch.qos.logback.classic.util.LevelToSyslogSeverity;
21  import ch.qos.logback.core.net.SyslogAppenderBase;
22  
23  public class SyslogStartConverter extends ClassicConverter {
24  
25    long lastTimestamp = -1;
26    String timesmapStr = null;
27    SimpleDateFormat simpleFormat;
28    String localHostName;
29    int facility;
30  
31    public void start() {
32      int errorCount = 0;
33      
34      String facilityStr = getFirstOption();
35      if (facilityStr == null) {
36        addError("was expecting a facility string as an option");
37        return;
38      }
39  
40      facility = SyslogAppenderBase.facilityStringToint(facilityStr);
41    
42      localHostName = getLocalHostname();
43      try {
44        // hours should be in 0-23, see also http://jira.qos.ch/browse/LBCLASSIC-48
45        simpleFormat = new SimpleDateFormat("MMM dd HH:mm:ss", new DateFormatSymbols(Locale.US));
46      } catch (IllegalArgumentException e) {
47        addError("Could not instantiate SimpleDateFormat", e);
48        errorCount++;
49      }
50  
51      if(errorCount == 0) {
52        super.start();
53      }
54    }
55  
56    public String convert(LoggingEvent event) {
57      StringBuilder sb = new StringBuilder();
58  
59      int pri = facility + LevelToSyslogSeverity.convert(event);
60    
61      sb.append("<");
62      sb.append(pri);
63      sb.append(">");
64      fillInTimestamp(sb, event.getTimeStamp());
65      sb.append(' ');
66      sb.append(localHostName);
67      sb.append(' ');
68  
69      return sb.toString();
70    }
71  
72    /**
73     * This method gets the network name of the machine we are running on.
74     * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name 
75     * cannot be found.
76     * @return String the name of the local host
77     */
78    public String getLocalHostname() {
79      try {
80        InetAddress addr = InetAddress.getLocalHost();
81        return addr.getHostName();
82      } catch (UnknownHostException uhe) {
83        addError("Could not determine local host name", uhe);
84        return "UNKNOWN_LOCALHOST";
85      }
86    }
87  
88    void fillInTimestamp(StringBuilder sb, long timestamp) {
89      // if called multiple times within the same millisecond
90      // use last value
91      if (timestamp != lastTimestamp) {
92        lastTimestamp = timestamp;
93        timesmapStr = simpleFormat.format(new Date(timestamp));
94      }
95      sb.append(timesmapStr);
96    }
97  }