Ejemplo n.º 1
0
func writeToDisk(m *syslog.Message) error {
	file, err := getLogFile(m)
	if err != nil {
		return err
	}
	bytes := []byte(m.String() + "\n")
	file.Write(bytes)
	return nil
}
Ejemplo n.º 2
0
func getLogFile(m *syslog.Message) (io.Writer, error) {
	r := regexp.MustCompile(`^.* ([a-z-]+)\[[a-z0-9\.]+\].*`)
	match := r.FindStringSubmatch(m.String())
	if match == nil {
		return nil, errors.New("Could not find app name in message")
	}
	appName := match[1]
	filePath := path.Join(logRoot, appName+".log")
	// check if file exists
	exists, err := fileExists(filePath)
	if err != nil {
		return nil, err
	}
	// return a new file or the existing file for appending
	var file io.Writer
	if exists {
		file, err = os.OpenFile(filePath, os.O_RDWR|os.O_APPEND, 0644)
	} else {
		file, err = os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
	}
	return file, err
}