Skip to content

Commit

Permalink
Apply configuration to audit log
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Dec 16, 2019
1 parent 22c8ac6 commit c6d68f0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 95 deletions.
16 changes: 16 additions & 0 deletions agent/dockerclient/dockerapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,19 @@ func (event *DockerContainerChangeEvent) String() string {

return res
}

// String returns a human readable string of the container change event
func (event *DockerContainerChangeEvent) ShortString() string {
res := fmt.Sprintf("Status: %s, DockerID: %s", event.Status.String(), event.DockerID)
res += ", health: " + event.Health.Status.String()

if event.ExitCode != nil {
res += fmt.Sprintf(", ExitCode: %d", aws.IntValue(event.ExitCode))
}

if event.Error != nil {
res += ", Error: " + event.Error.Error()
}

return res
}
4 changes: 2 additions & 2 deletions agent/engine/task_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ func (mtask *managedTask) handleContainerChange(containerChange dockerContainerC
}

event := containerChange.event
seelog.Infof("Managed task [%s]: handling container change [%v] for container [%s]",
mtask.Arn, event, container.Name)
seelog.Infof("Managed task [%s]: handling container change [%s] for container [%s]",
mtask.Arn, event.ShortString(), container.Name)

// If this is a backwards transition stopped->running, the first time set it
// to be known running so it will be stopped. Subsequently ignore these backward transitions
Expand Down
38 changes: 38 additions & 0 deletions agent/logger/audit/audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ package audit

import (
"fmt"
"strconv"

"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/logger"
"github.com/aws/amazon-ecs-agent/agent/logger/audit/request"
)

type AuditLogger interface {
Log(r request.LogRequest, httpResponseCode int, eventType string)
GetContainerInstanceArn() string
GetCluster() string
}

type InfoLogger interface {
Info(i ...interface{})
}

type auditLog struct {
containerInstanceArn string
cluster string
Expand Down Expand Up @@ -62,3 +74,29 @@ func (a *auditLog) GetCluster() string {
func (a *auditLog) GetContainerInstanceArn() string {
return a.containerInstanceArn
}

func AuditLoggerConfig(cfg *config.Config) string {
config := `
<seelog type="asyncloop" minlevel="info">
<outputs formatid="main">
<console />`
if cfg.CredentialsAuditLogFile != "" {
if logger.Config.RolloverType == "size" {
config += `
<rollingfile filename="` + cfg.CredentialsAuditLogFile + `" type="size"
maxsize="` + strconv.Itoa(int(logger.Config.MaxFileSizeMB*1000000)) + `" archivetype="none" maxrolls="` + strconv.Itoa(logger.Config.MaxRollCount) + `" />`
} else {
config += `
<rollingfile filename="` + cfg.CredentialsAuditLogFile + `" type="date"
datepattern="2006-01-02-15" archivetype="none" maxrolls="` + strconv.Itoa(logger.Config.MaxRollCount) + `" />`
}
}
config += `
</outputs>
<formats>
<format id="main" format="%Msg%n" />
</formats>
</seelog>
`
return config
}
26 changes: 0 additions & 26 deletions agent/logger/audit/interface.go

This file was deleted.

35 changes: 0 additions & 35 deletions agent/logger/audit/seelog_config.go

This file was deleted.

64 changes: 32 additions & 32 deletions agent/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ const (
)

type logConfig struct {
RolloverType string
MaxRollCount int
MaxFileSizeMB float64
logfile string
level string
rolloverType string
outputFormat string
maxRollCount int
maxFileSizeMB float64
sync.Mutex
lock sync.Mutex
}

var config *logConfig
var Config *logConfig

func logfmtFormatter(params string) seelog.FormatterFunc {
return func(message string, level seelog.LogLevel, context seelog.LogContextInterface) interface{} {
Expand All @@ -76,19 +76,19 @@ func reloadConfig() {

func seelogConfig() string {
c := `
<seelog type="asyncloop" minlevel="` + config.level + `">
<outputs formatid="` + config.outputFormat + `">
<seelog type="asyncloop" minlevel="` + Config.level + `">
<outputs formatid="` + Config.outputFormat + `">
<console />`
c += platformLogConfig()
if config.logfile != "" {
if config.rolloverType == "size" {
if Config.logfile != "" {
if Config.RolloverType == "size" {
c += `
<rollingfile filename="` + config.logfile + `" type="size"
maxsize="` + strconv.Itoa(int(config.maxFileSizeMB*1000000)) + `" archivetype="none" maxrolls="` + strconv.Itoa(config.maxRollCount) + `" />`
<rollingfile filename="` + Config.logfile + `" type="size"
maxsize="` + strconv.Itoa(int(Config.MaxFileSizeMB*1000000)) + `" archivetype="none" maxrolls="` + strconv.Itoa(Config.MaxRollCount) + `" />`
} else {
c += `
<rollingfile filename="` + config.logfile + `" type="date"
datepattern="2006-01-02-15" archivetype="none" maxrolls="` + strconv.Itoa(config.maxRollCount) + `" />`
<rollingfile filename="` + Config.logfile + `" type="date"
datepattern="2006-01-02-15" archivetype="none" maxrolls="` + strconv.Itoa(Config.MaxRollCount) + `" />`
}
}
c += `
Expand All @@ -114,50 +114,50 @@ func SetLevel(logLevel string) {
parsedLevel, ok := levels[strings.ToLower(logLevel)]

if ok {
config.Lock()
defer config.Unlock()
config.level = parsedLevel
Config.lock.Lock()
defer Config.lock.Unlock()
Config.level = parsedLevel
reloadConfig()
}
}

// GetLevel gets the log level
func GetLevel() string {
config.Lock()
defer config.Unlock()
Config.lock.Lock()
defer Config.lock.Unlock()

return config.level
return Config.level
}

func init() {
config = &logConfig{
Config = &logConfig{
logfile: os.Getenv(LOGFILE_ENV_VAR),
level: DEFAULT_LOGLEVEL,
rolloverType: DEFAULT_ROLLOVER_TYPE,
RolloverType: DEFAULT_ROLLOVER_TYPE,
outputFormat: DEFAULT_OUTPUT_FORMAT,
maxFileSizeMB: DEFAULT_MAX_FILE_SIZE,
maxRollCount: DEFAULT_MAX_ROLL_COUNT,
MaxFileSizeMB: DEFAULT_MAX_FILE_SIZE,
MaxRollCount: DEFAULT_MAX_ROLL_COUNT,
}

SetLevel(os.Getenv(LOGLEVEL_ENV_VAR))
if rolloverType := os.Getenv(LOG_ROLLOVER_TYPE_ENV_VAR); rolloverType != "" {
config.rolloverType = rolloverType
if RolloverType := os.Getenv(LOG_ROLLOVER_TYPE_ENV_VAR); RolloverType != "" {
Config.RolloverType = RolloverType
}
if outputFormat := os.Getenv(LOG_OUTPUT_FORMAT_ENV_VAR); outputFormat != "" {
config.outputFormat = outputFormat
Config.outputFormat = outputFormat
}
if maxRollCount := os.Getenv(LOG_MAX_ROLL_COUNT_ENV_VAR); maxRollCount != "" {
i, err := strconv.Atoi(maxRollCount)
if MaxRollCount := os.Getenv(LOG_MAX_ROLL_COUNT_ENV_VAR); MaxRollCount != "" {
i, err := strconv.Atoi(MaxRollCount)
if err == nil {
config.maxRollCount = i
Config.MaxRollCount = i
} else {
seelog.Error("Invalid value for "+LOG_MAX_ROLL_COUNT_ENV_VAR, err)
}
}
if maxFileSizeMB := os.Getenv(LOG_MAX_FILE_SIZE_ENV_VAR); maxFileSizeMB != "" {
f, err := strconv.ParseFloat(maxFileSizeMB, 64)
if MaxFileSizeMB := os.Getenv(LOG_MAX_FILE_SIZE_ENV_VAR); MaxFileSizeMB != "" {
f, err := strconv.ParseFloat(MaxFileSizeMB, 64)
if err == nil {
config.maxFileSizeMB = f
Config.MaxFileSizeMB = f
} else {
seelog.Error("Invalid value for "+LOG_MAX_FILE_SIZE_ENV_VAR, err)
}
Expand Down

0 comments on commit c6d68f0

Please sign in to comment.