Skip to content

Commit

Permalink
Add granular severity settings for log_files.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
weisheng-p committed Jan 4, 2024
1 parent 3efcaf2 commit 9cb18b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
15 changes: 10 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ type Config struct {
}

type LogFile struct {
Path string
Tag string
Path string
Tag string
Severity syslog.Priority
}

func init() {
Expand Down Expand Up @@ -309,18 +310,22 @@ func decodeLogFiles(f interface{}) ([]LogFile, error) {

case map[interface{}]interface{}:
var (
tag string
path string
tag string
path string
severity syslog.Priority
)

tag, _ = val["tag"].(string)
path, _ = val["path"].(string)

severity_input, _ := val["severity"].(string)
severity, _ = syslog.Severity(severity_input)

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

files = append(files, LogFile{Tag: tag, Path: path})
files = append(files, LogFile{Tag: tag, Path: path, Severity: severity})

default:
panic(vals)
Expand Down
1 change: 1 addition & 0 deletions examples/log_files.yml.example.advanced
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ files:
tag: site2/access_log
- path: /var/log/httpd/site2/error_log
tag: site2/error_log
severity: err # Note: valid severity includes: emerg, alert, crit, err, warn, notice, info, debug
- /opt/misc/*.log
- /home/**/*.log
- /var/log/mysqld.log
Expand Down
11 changes: 8 additions & 3 deletions remote_syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *Server) closing() bool {
}

// Tails a single file
func (s *Server) tailOne(file, tag string, whence int) {
func (s *Server) tailOne(file, tag string, file_severity syslog.Priority, whence int) {
defer s.registry.Remove(file)

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

if file_severity == 0 {
file_severity = s.config.Severity
}

for {
select {
case line, ok := <-t.Lines():
Expand All @@ -136,7 +140,7 @@ func (s *Server) tailOne(file, tag string, whence int) {
if !matchExps(l, s.config.ExcludePatterns) {

s.logger.Write(syslog.Packet{
Severity: s.config.Severity,
Severity: file_severity,
Facility: s.config.Facility,
Time: time.Now(),
Hostname: s.logger.ClientHostname,
Expand Down Expand Up @@ -181,6 +185,7 @@ func (s *Server) globFiles(firstPass bool) {
for _, glob := range s.config.Files {

tag := glob.Tag
severity := glob.Severity
files, err := filepath.Glob(utils.ResolvePath(glob.Path))

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

s.registry.Add(file)
go s.tailOne(file, tag, whence)
go s.tailOne(file, tag, severity, whence)
}
}
}
Expand Down

0 comments on commit 9cb18b6

Please sign in to comment.