Skip to content

Commit

Permalink
Add the <filter></filter> only when logfile environment Variable is set
Browse files Browse the repository at this point in the history
  • Loading branch information
ubhattacharjya committed Sep 3, 2020
1 parent 5f2a38e commit eacfbad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
8 changes: 5 additions & 3 deletions agent/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ func seelogConfig() string {
<console />`
c += platformLogConfig()
c += `
</filter>
<filter levels="` + getLevelList(Config.instanceLevel) + `">`
</filter>`
if Config.logfile != "" {
c += `
<filter levels="` + getLevelList(Config.instanceLevel) + `">`
if Config.RolloverType == "size" {
c += `
<rollingfile filename="` + Config.logfile + `" type="size"
Expand All @@ -98,9 +99,10 @@ func seelogConfig() string {
<rollingfile filename="` + Config.logfile + `" type="date"
datepattern="2006-01-02-15" archivetype="none" maxrolls="` + strconv.Itoa(Config.MaxRollCount) + `" />`
}
c += `
</filter>`
}
c += `
</filter>
</outputs>
<formats>
<format id="logfmt" format="%EcsAgentLogfmt" />
Expand Down
25 changes: 25 additions & 0 deletions agent/logger/log_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ func TestSeelogConfig_Default(t *testing.T) {
</seelog>`, c)
}

func TestSeelogConfig_WithoutLogFile(t *testing.T) {
Config = &logConfig{
driverLevel: DEFAULT_LOGLEVEL,
instanceLevel: DEFAULT_LOGLEVEL,
RolloverType: DEFAULT_ROLLOVER_TYPE,
outputFormat: DEFAULT_OUTPUT_FORMAT,
MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE,
MaxRollCount: DEFAULT_MAX_ROLL_COUNT,
}
c := seelogConfig()
require.Equal(t, `
<seelog type="asyncloop">
<outputs formatid="logfmt">
<filter levels="info,warn,error,critical">
<console />
</filter>
</outputs>
<formats>
<format id="logfmt" format="%EcsAgentLogfmt" />
<format id="json" format="%EcsAgentJson" />
<format id="windows" format="%Msg" />
</formats>
</seelog>`, c)
}

func TestSeelogConfig_DebugLevel(t *testing.T) {
Config = &logConfig{
logfile: "foo.log",
Expand Down
26 changes: 26 additions & 0 deletions agent/logger/log_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ func TestSeelogConfigWindows_Default(t *testing.T) {
</seelog>`, c)
}

func TestSeelogConfigWindows_WithoutLogFile(t *testing.T) {
Config = &logConfig{
driverLevel: DEFAULT_LOGLEVEL,
instanceLevel: DEFAULT_LOGLEVEL,
RolloverType: DEFAULT_ROLLOVER_TYPE,
outputFormat: DEFAULT_OUTPUT_FORMAT,
MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE,
MaxRollCount: DEFAULT_MAX_ROLL_COUNT,
}
c := seelogConfig()
require.Equal(t, `
<seelog type="asyncloop">
<outputs formatid="logfmt">
<filter levels="info,warn,error,critical">
<console />
<custom name="wineventlog" formatid="windows" />
</filter>
</outputs>
<formats>
<format id="logfmt" format="%EcsAgentLogfmt" />
<format id="json" format="%EcsAgentJson" />
<format id="windows" format="%Msg" />
</formats>
</seelog>`, c)
}

func TestSeelogConfigWindows_DebugLevel(t *testing.T) {
Config = &logConfig{
logfile: "foo.log",
Expand Down

0 comments on commit eacfbad

Please sign in to comment.