View Javadoc

1   package net.sf.logdistiller.logtypes;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.io.*;
18  import java.text.ParseException;
19  
20  import net.sf.logdistiller.LogEvent;
21  import net.sf.logdistiller.LogType;
22  import net.sf.logdistiller.util.StringCutter;
23  
24  /**
25   * Log event for Unix syslog facility. By default, the classification rules generated by the GUI for this type of logs
26   * will sort events based on the following attributes: <code>host</code>, then <code>program</code>.
27   */
28  public class SyslogLogEvent
29      extends LogEvent
30  {
31      public final static String ID = "syslog";
32  
33      // see http://logreport.org/doc/gen/os/
34      public final String timestamp;
35  
36      public final String host;
37  
38      public final String program;
39  
40      public final String pid;
41  
42      public final String message;
43  
44      public final static LogType LOGTYPE = new LogType.Basic( ID );
45  
46      private final static String[] ATTRIBUTE_NAMES = { "logSource", "timestamp", "host", "program", "pid", "message" };
47  
48      public final static LogType.Description DESCRIPTION = new Description( (LogType.Basic) LOGTYPE, ATTRIBUTE_NAMES );
49  
50      public SyslogLogEvent( Factory factory, String rawLog )
51          throws ParseException
52      {
53          super( factory, rawLog );
54          timestamp = rawLog.substring( 0, 15 );
55          StringCutter sc = new StringCutter( rawLog, 16 );
56          host = sc.parseTo( " " );
57          String remaining = sc.getRemaining();
58          if ( remaining.indexOf( ": " ) < 0 )
59          {
60              program = "";
61              pid = "";
62              message = remaining;
63          }
64          else
65          {
66              // <program>[<pid>]: <message>
67              String programPid = sc.parseTo( ": " );
68              message = sc.getRemaining();
69  
70              int index = programPid.indexOf( '[' );
71              if ( ( index >= 0 ) && programPid.endsWith( "]" ) )
72              {
73                  program = programPid.substring( 0, index );
74                  pid = programPid.substring( index + 1, programPid.length() - 1 );
75              }
76              else
77              {
78                  program = programPid;
79                  pid = "";
80              }
81          }
82          setAttributes( new String[] { factory.getLogSource(), timestamp, host, program, pid, message } );
83      }
84  
85      private static class Description
86          extends LogType.Description
87      {
88          public Description( LogType.Basic logtype, String[] attributeNames )
89          {
90              super( logtype, attributeNames );
91              logtype.setDescription( this );
92          }
93  
94          public LogEvent.Factory newFactory( Reader reader, String logSource )
95              throws IOException
96          {
97              return new Factory( this, reader, logSource );
98          }
99  
100         public String getDefaultSamplingAttributes()
101         {
102             return "host,program";
103         }
104     }
105 
106     private static class Factory
107         extends LogEvent.Factory
108     {
109         private final LineNumberReader reader;
110 
111         public Factory( Description description, Reader reader, String logSource )
112             throws FileNotFoundException
113         {
114             super( description, logSource );
115             this.reader = new LineNumberReader( reader );
116         }
117 
118         protected LogEvent readNextEvent()
119             throws IOException, ParseException
120         {
121             String curLine = reader.readLine();
122             if ( curLine == null )
123             {
124                 // EOF
125                 return null;
126             }
127             try
128             {
129                 return new SyslogLogEvent( this, curLine );
130             }
131             catch ( RuntimeException re )
132             {
133                 throw new RuntimeException( "error while parsing line '" + curLine + "'", re );
134             }
135         }
136     }
137 }