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.LogEventBuilder;
23 import net.sf.logdistiller.util.StringCutter;
24
25
26
27
28
29 public class SyslogLogEvent
30 extends LogEvent
31 {
32 public final static String ID = "syslog";
33
34
35 public final String timestamp;
36
37 public final String host;
38
39 public final String program;
40
41 public final String pid;
42
43 public final String message;
44
45 public final static LogType LOGTYPE = new LogType.Basic( ID );
46
47 private final static String[] ATTRIBUTE_NAMES = { "logSource", "timestamp", "host", "program", "pid", "message" };
48
49 public final static LogType.Description DESCRIPTION = new Description( (LogType.Basic) LOGTYPE, ATTRIBUTE_NAMES );
50
51 public SyslogLogEvent( LogEvent.Factory factory, String rawLog )
52 throws ParseException
53 {
54 super( factory, rawLog );
55 timestamp = rawLog.substring( 0, 15 );
56 StringCutter sc = new StringCutter( rawLog, 16 );
57 host = sc.parseTo( " " );
58 String remaining = sc.getRemaining();
59 if ( remaining.indexOf( ": " ) < 0 )
60 {
61 program = "";
62 pid = "";
63 message = remaining;
64 }
65 else
66 {
67
68 String programPid = sc.parseTo( ": " );
69 message = sc.getRemaining();
70
71 int index = programPid.indexOf( '[' );
72 if ( ( index >= 0 ) && programPid.endsWith( "]" ) )
73 {
74 program = programPid.substring( 0, index );
75 pid = programPid.substring( index + 1, programPid.length() - 1 );
76 }
77 else
78 {
79 program = programPid;
80 pid = "";
81 }
82 }
83 setAttributes( new String[] { factory.getLogSource(), timestamp, host, program, pid, message } );
84 }
85
86 private static class Description
87 extends LogType.Description
88 {
89 public Description( LogType.Basic logtype, String[] attributeNames )
90 {
91 super( logtype, attributeNames );
92 logtype.setDescription( this );
93 }
94
95 public LogEvent.Factory newFactory( Reader reader, String logSource )
96 throws IOException
97 {
98 return new Factory( this, reader, logSource );
99 }
100
101 public String getDefaultSamplingAttributes()
102 {
103 return "host,program";
104 }
105 }
106
107 private static class Factory
108 extends LogEvent.Factory
109 {
110 private final LineNumberReader reader;
111
112 public Factory( Description description, Reader reader, String logSource )
113 throws FileNotFoundException
114 {
115 super( description, logSource );
116 this.reader = new LineNumberReader( reader );
117 }
118
119 protected LogEvent readNextEvent()
120 throws IOException, ParseException
121 {
122 String curLine = reader.readLine();
123 if ( curLine == null )
124 {
125
126 return null;
127 }
128
129 return BUILDER.buildLogEvent( this, reader.getLineNumber(), curLine );
130 }
131
132 private final static LogEventBuilder BUILDER = new LogEventBuilder()
133 {
134 protected LogEvent newEvent( LogEvent.Factory factory, String curLine, Object... objects )
135 throws ParseException
136 {
137 return new SyslogLogEvent( factory, curLine );
138 }
139 };
140 }
141 }