Skip to content

Commit

Permalink
Added path.logs and made logging to files the default
Browse files Browse the repository at this point in the history
This adds the `-path.logs` CLI flag and corresponding config file
setting. By default it's set to `{path.home}/logs`. The rotating
file writer now writes to this path by default.

For DEB/RPM, the logs path is set to `/var/log/{beatname}` which
matches the previous default location. However, this is a breaking
change because the previous default was to send the logs to syslog.

Part of elastic#1371.
  • Loading branch information
Tudor Golubenco committed Apr 20, 2016
1 parent 57477cf commit 15192ad
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dev-tools/packer/platforms/centos/init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export PATH
[ -f /etc/sysconfig/{{.beat_name}} ] && . /etc/sysconfig/{{.beat_name}}
pidfile=${PIDFILE-/var/run/{{.beat_name}}.pid}
agent=${BEATS_AGENT-/usr/share/{{.beat_name}}/bin/{{.beat_name}}}
args="-c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}}"
args="-c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}} -path.logs /var/log/{{.beat_name}}"
test_args="-e -configtest"
wrapper="/usr/share/{{.beat_name}}/bin/{{.beat_name}}-god"
wrapperopts="-r / -n -p $pidfile"
Expand Down
2 changes: 1 addition & 1 deletion dev-tools/packer/platforms/centos/systemd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/share/{{.beat_name}}/bin/{{.beat_name}} -c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}}
ExecStart=/usr/share/{{.beat_name}}/bin/{{.beat_name}} -c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}} -path.logs /var/log/{{.beat_name}}
Restart=always

[Install]
Expand Down
2 changes: 1 addition & 1 deletion dev-tools/packer/platforms/debian/init.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="{{.beat_description}}"
NAME="{{.beat_name}}"
DAEMON=/usr/share/{{.beat_name}}/bin/$NAME
DAEMON_ARGS="-c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}}"
DAEMON_ARGS="-c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}} -path.logs /var/log/{{.beat_name}}"
TEST_ARGS="-e -configtest"
PIDFILE=/var/run/{{.beat_name}}.pid
WRAPPER="/usr/share/{{.beat_name}}/bin/{{.beat_name}}-god"
Expand Down
2 changes: 1 addition & 1 deletion dev-tools/packer/platforms/debian/systemd.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/share/{{.beat_name}}/bin/{{.beat_name}} -c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}}
ExecStart=/usr/share/{{.beat_name}}/bin/{{.beat_name}} -c /etc/{{.beat_name}}/{{.beat_name}}.yml -path.home /usr/share/{{.beat_name}} -path.config /etc/{{.beat_name}} -path.data /var/lib/{{.beat_name}} -path.logs /var/log/{{.beat_name}}
Restart=always

[Install]
Expand Down
21 changes: 6 additions & 15 deletions libbeat/logp/logp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"
"io/ioutil"
"log"
"runtime"
"strings"

"github.com/elastic/beats/libbeat/paths"
)

// cmd line flags
Expand Down Expand Up @@ -57,29 +58,19 @@ func Init(name string, config *Logging) error {
logLevel = LOG_DEBUG
}

var defaultToFiles, defaultToSyslog bool
var defaultFilePath string
if runtime.GOOS == "windows" {
// always disabled on windows
defaultToSyslog = false
defaultToFiles = true
defaultFilePath = fmt.Sprintf("C:\\ProgramData\\%s\\Logs", name)
} else {
defaultToSyslog = true
defaultToFiles = false
defaultFilePath = fmt.Sprintf("/var/log/%s", name)
}
// default log location is in the logs path
defaultFilePath := paths.Resolve(paths.Logs, "")

var toSyslog, toFiles bool
if config.ToSyslog != nil {
toSyslog = *config.ToSyslog
} else {
toSyslog = defaultToSyslog
toSyslog = false
}
if config.ToFiles != nil {
toFiles = *config.ToFiles
} else {
toFiles = defaultToFiles
toFiles = true
}

// toStderr disables logging to syslog/files
Expand Down
22 changes: 17 additions & 5 deletions libbeat/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/elastic/beats/libbeat/logp"
)

var (
homePath = flag.String("path.home", "", "Home path")
configPath = flag.String("path.config", "", "Configuration path")
dataPath = flag.String("path.data", "", "Data path")
logsPath = flag.String("path.logs", "", "Logs path")
)

type Path struct {
Home string
Config string
Data string
Logs string
}

// FileType is an enumeration type representing the file types.
Expand All @@ -49,6 +49,7 @@ const (
Home FileType = "home"
Config FileType = "config"
Data FileType = "data"
Logs FileType = "logs"
)

// Paths is the Path singleton on which the top level functions from this
Expand Down Expand Up @@ -91,6 +92,7 @@ func (paths *Path) initPaths(cfg *Path) error {
paths.Home = cfg.Home
paths.Config = cfg.Config
paths.Data = cfg.Data
paths.Logs = cfg.Logs

// overwrite paths from CLI flags
if homePath != nil && len(*homePath) > 0 {
Expand All @@ -102,6 +104,9 @@ func (paths *Path) initPaths(cfg *Path) error {
if dataPath != nil && len(*dataPath) > 0 {
paths.Data = *dataPath
}
if logsPath != nil && len(*logsPath) > 0 {
paths.Logs = *logsPath
}

// default for the home path is the binary location
if len(paths.Home) == 0 {
Expand All @@ -123,6 +128,11 @@ func (paths *Path) initPaths(cfg *Path) error {
paths.Data = filepath.Join(paths.Home, "data")
}

// default for logs path
if len(paths.Logs) == 0 {
paths.Logs = filepath.Join(paths.Home, "logs")
}

return nil
}

Expand All @@ -142,8 +152,10 @@ func (paths *Path) Resolve(fileType FileType, path string) string {
return filepath.Join(paths.Config, path)
case Data:
return filepath.Join(paths.Data, path)
case Logs:
return filepath.Join(paths.Logs, path)
default:
logp.WTF("Unknown file type: %s", fileType)
panic(fmt.Sprintf("Unknown file type: %s", fileType))
return ""
}
}
Expand All @@ -157,6 +169,6 @@ func Resolve(fileType FileType, path string) string {

// String returns a textual representation
func (paths *Path) String() string {
return fmt.Sprintf("Home path: [%s] Config path: [%s] Data path: [%s]",
paths.Home, paths.Config, paths.Data)
return fmt.Sprintf("Home path: [%s] Config path: [%s] Data path: [%s] Logs path: [%s]",
paths.Home, paths.Config, paths.Data, paths.Logs)
}
72 changes: 72 additions & 0 deletions libbeat/paths/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestHomePath(t *testing.T) {
}

for _, test := range tests {
t.Log("Executing test", test)
homePath = test.CLIHome
cfg := Path{Home: test.CfgHome}
assert.NoError(t, Paths.initPaths(&cfg))
Expand Down Expand Up @@ -126,6 +127,7 @@ func TestDataPath(t *testing.T) {
}

for _, test := range tests {
t.Log("Executing test", test)
homePath = test.CLIHome
dataPath = test.CLIData
cfg := Path{Home: test.CfgHome, Data: test.CfgData}
Expand All @@ -135,3 +137,73 @@ func TestDataPath(t *testing.T) {
}

}

func TestLogsPath(t *testing.T) {
type io struct {
CLIHome *string // cli flag home setting
CfgHome string // config file home setting
CLILogs *string // cli flag for data setting
CfgLogs string // config file data setting
Path string // requested path
ResultLogs string // expected logs path
}

binDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
assert.NoError(t, err)
tmp := "/tmp/"
root := "/root/"

tests := []io{
{
CLIHome: nil,
CfgHome: "",
CLILogs: nil,
CfgLogs: "",
Path: "",
ResultLogs: filepath.Join(binDir, "logs"),
},
{
CLIHome: nil,
CfgHome: "",
CLILogs: nil,
CfgLogs: "",
Path: "test",
ResultLogs: filepath.Join(binDir, "logs", "test"),
},
{
CLIHome: nil,
CfgHome: "/tmp/",
CLILogs: nil,
CfgLogs: "/root/logs",
Path: "",
ResultLogs: "/root/logs",
},
{
CLIHome: &tmp,
CfgHome: "",
CLILogs: nil,
CfgLogs: "/root/logs",
Path: "",
ResultLogs: "/root/logs",
},
{
CLIHome: &tmp,
CfgHome: "",
CLILogs: &root,
CfgLogs: "/root/logs",
Path: "",
ResultLogs: "/root",
},
}

for _, test := range tests {
t.Log("Executing test", test)
homePath = test.CLIHome
logsPath = test.CLILogs
cfg := Path{Home: test.CfgHome, Logs: test.CfgLogs}
assert.NoError(t, Paths.initPaths(&cfg))

assert.Equal(t, test.ResultLogs, Resolve(Logs, test.Path))
}

}

0 comments on commit 15192ad

Please sign in to comment.