Skip to content

Commit 1a31c2e

Browse files
committed
Add the ability to set a custom hostname in the config
Fixes papertrail#126
1 parent 7d8579f commit 1a31c2e

5 files changed

+115
-5
lines changed

config.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Config struct {
6565
type LogFile struct {
6666
Path string
6767
Tag string
68+
Hostname string
6869
}
6970

7071
func init() {
@@ -299,6 +300,8 @@ func decodeLogFiles(f interface{}) ([]LogFile, error) {
299300
case string:
300301
lf := strings.Split(val, "=")
301302
switch len(lf) {
303+
case 3:
304+
files = append(files, LogFile{Tag: lf[0], Hostname: lf[1], Path: lf[2]})
302305
case 2:
303306
files = append(files, LogFile{Tag: lf[0], Path: lf[1]})
304307
case 1:
@@ -310,17 +313,19 @@ func decodeLogFiles(f interface{}) ([]LogFile, error) {
310313
case map[interface{}]interface{}:
311314
var (
312315
tag string
316+
hostname string
313317
path string
314318
)
315319

316320
tag, _ = val["tag"].(string)
321+
hostname, _ = val["hostname"].(string)
317322
path, _ = val["path"].(string)
318323

319324
if path == "" {
320325
return files, fmt.Errorf("Invalid log file %#v", val)
321326
}
322327

323-
files = append(files, LogFile{Tag: tag, Path: path})
328+
files = append(files, LogFile{Tag: tag, Hostname: hostname, Path: path})
324329

325330
default:
326331
panic(vals)

config_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func TestRawConfig(t *testing.T) {
4343
Tag: "apache",
4444
Path: "/var/log/httpd/access_log",
4545
},
46+
{
47+
Tag: "debian",
48+
Path: "/var/log/syslog",
49+
Hostname: "myhost.mydomain.com",
50+
},
4651
})
4752
assert.Equal(c.TcpMaxLineLength, 99991)
4853
assert.Equal(c.NewFileCheckInterval, 10*time.Second)

remote_syslog.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (s *Server) closing() bool {
9393
}
9494

9595
// Tails a single file
96-
func (s *Server) tailOne(file, tag string, whence int) {
96+
func (s *Server) tailOne(file, tag string, hostname string, whence int) {
9797
defer s.registry.Remove(file)
9898

9999
t, err := follower.New(file, follower.Config{
@@ -111,6 +111,10 @@ func (s *Server) tailOne(file, tag string, whence int) {
111111
tag = path.Base(file)
112112
}
113113

114+
if hostname == "" {
115+
hostname = s.logger.ClientHostname
116+
}
117+
114118
for {
115119
select {
116120
case line, ok := <-t.Lines():
@@ -139,7 +143,7 @@ func (s *Server) tailOne(file, tag string, whence int) {
139143
Severity: s.config.Severity,
140144
Facility: s.config.Facility,
141145
Time: time.Now(),
142-
Hostname: s.logger.ClientHostname,
146+
Hostname: hostname,
143147
Tag: tag,
144148
Token: s.config.Destination.Token,
145149
Message: l,
@@ -181,6 +185,7 @@ func (s *Server) globFiles(firstPass bool) {
181185
for _, glob := range s.config.Files {
182186

183187
tag := glob.Tag
188+
hostname := glob.Hostname
184189
files, err := filepath.Glob(utils.ResolvePath(glob.Path))
185190

186191
if err != nil {
@@ -206,7 +211,7 @@ func (s *Server) globFiles(firstPass bool) {
206211
}
207212

208213
s.registry.Add(file)
209-
go s.tailOne(file, tag, whence)
214+
go s.tailOne(file, tag, hostname, whence)
210215
}
211216
}
212217
}

remote_syslog_test.go

+93-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,98 @@ func TestNewFileSeek(t *testing.T) {
7777

7878
packet := <-server.packets
7979
assert.Equal(msg, packet.Message)
80+
assert.Equal("testhost", packet.Hostname)
81+
}
82+
}
83+
84+
func TestCustomTag(t *testing.T) {
85+
os.RemoveAll(tmpdir)
86+
os.Mkdir(tmpdir, 0755)
87+
88+
assert := assert.New(t)
89+
90+
server = newTestSyslogServer("127.0.0.1:0")
91+
go server.serve()
92+
93+
// Add custom tag
94+
config := testConfig()
95+
path := "tmp/*.log"
96+
tag := "customTag"
97+
config.Files = []LogFile {
98+
{
99+
Path: path,
100+
Tag: tag,
101+
},
102+
}
103+
104+
s := NewServer(config)
105+
s.registry = NewInMemoryRegistry()
106+
go s.Start()
107+
defer s.Close()
108+
109+
// just a quick rest to get the server started
110+
time.Sleep(1 * time.Second)
111+
112+
for _, msg := range []string{
113+
"welcome to the jungle",
114+
"we got alerts and logs",
115+
"we got everything you want",
116+
"as long as it's alerts and logs",
117+
} {
118+
file := tmpLogFile()
119+
defer file.Close()
120+
121+
writeLog(file, msg)
122+
123+
packet := <-server.packets
124+
assert.Equal(msg, packet.Message)
125+
assert.Equal(tag, packet.Tag)
126+
assert.Equal("testhost", packet.Hostname)
127+
}
128+
}
129+
130+
func TestCustomHostname(t *testing.T) {
131+
os.RemoveAll(tmpdir)
132+
os.Mkdir(tmpdir, 0755)
133+
134+
assert := assert.New(t)
135+
136+
server = newTestSyslogServer("127.0.0.1:0")
137+
go server.serve()
138+
139+
// Add custom hostname
140+
config := testConfig()
141+
path := "tmp/*.log"
142+
hostname := "custom.hostname"
143+
config.Files = []LogFile {
144+
{
145+
Path: path,
146+
Hostname: hostname,
147+
},
148+
}
149+
150+
s := NewServer(config)
151+
s.registry = NewInMemoryRegistry()
152+
go s.Start()
153+
defer s.Close()
154+
155+
// just a quick rest to get the server started
156+
time.Sleep(1 * time.Second)
157+
158+
for _, msg := range []string{
159+
"welcome to the jungle",
160+
"we got alerts and logs",
161+
"we got everything you want",
162+
"as long as it's alerts and logs",
163+
} {
164+
file := tmpLogFile()
165+
defer file.Close()
166+
167+
writeLog(file, msg)
168+
169+
packet := <-server.packets
170+
assert.Equal(msg, packet.Message)
171+
assert.Equal(hostname, packet.Hostname)
80172
}
81173
}
82174

@@ -155,7 +247,7 @@ func testConfig() *Config {
155247
ConnectTimeout: 10 * time.Second,
156248
WriteTimeout: 10 * time.Second,
157249
NewFileCheckInterval: 1 * time.Second,
158-
LogLevels: "<root>=INFO",
250+
LogLevels: "<root>=DEBUG",
159251
TcpMaxLineLength: 99990,
160252
NoDetach: true,
161253
Hostname: "testhost",

test/config.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ files:
44
- "nginx=/var/log/nginx/nginx.log"
55
- path: /var/log/httpd/access_log
66
tag: apache
7+
- path: /var/log/syslog
8+
tag: debian
9+
hostname: myhost.mydomain.com
710
destination:
811
host: logs.papertrailapp.com
912
port: 514

0 commit comments

Comments
 (0)