1 package net.sf.logdistiller.logtypes;
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28 public class SyslogLogEvent
29 extends LogEvent
30 {
31 public final static String ID = "syslog";
32
33
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
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
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 }