View Javadoc

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