Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Added log truncation option
Browse files Browse the repository at this point in the history
Updated logrus version to match same than most plugins
  • Loading branch information
obourdon committed Jun 20, 2016
1 parent 6a8ba3a commit 91fc4c2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/configs/snap-config-sample.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"log_level": 2,
"log_path": "/some/log/dir",
"log_truncate": false,
"gomaxprocs": 2,
"control": {
"auto_discover_path": "/some/directory/with/plugins",
Expand Down
5 changes: 5 additions & 0 deletions examples/configs/snap-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ log_level: 2
# the provided directory.
log_path: /some/log/dir

# log_truncate specifies how the log file with be opened
# false => append
# true => truncate
log_truncate: false

# Gomaxprocs sets the number of cores to use on the system
# for snapd to use. Default for gomaxprocs is 1
gomaxprocs: 2
Expand Down
60 changes: 40 additions & 20 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ var (
Usage: "Path for logs. Empty path logs to stdout.",
EnvVar: "SNAP_LOG_PATH",
}
flLogTruncate = cli.BoolFlag{
Name: "log-truncate",
Usage: "Log file truncating mode. Default is false => append (true => truncate).",
}
flLogLevel = cli.IntFlag{
Name: "log-level, l",
Usage: "1-5 (Debug, Info, Warning, Error, Fatal)",
Expand Down Expand Up @@ -93,24 +97,26 @@ var (

// default configuration values
const (
defaultLogLevel int = 3
defaultGoMaxProcs int = 1
defaultLogPath string = ""
defaultConfigPath string = "/etc/snap/snapd.conf"
defaultLogLevel int = 3
defaultGoMaxProcs int = 1
defaultLogPath string = ""
defaultLogTruncate bool = false
defaultConfigPath string = "/etc/snap/snapd.conf"
)

// holds the configuration passed in through the SNAP config file
// Note: if this struct is modified, then the switch statement in the
// UnmarshalJSON method in this same file needs to be modified to
// match the field mapping that is defined here
type Config struct {
LogLevel int `json:"log_level"yaml:"log_level"`
GoMaxProcs int `json:"gomaxprocs"yaml:"gomaxprocs"`
LogPath string `json:"log_path"yaml:"log_path"`
Control *control.Config `json:"control"yaml:"control"`
Scheduler *scheduler.Config `json:"scheduler"yaml:"scheduler"`
RestAPI *rest.Config `json:"restapi"yaml:"restapi"`
Tribe *tribe.Config `json:"tribe"yaml:"tribe"`
LogLevel int `json:"log_level,omitempty"yaml:"log_level,omitempty"`
GoMaxProcs int `json:"gomaxprocs,omitempty"yaml:"gomaxprocs,omitempty"`
LogPath string `json:"log_path,omitempty"yaml:"log_path,omitempty"`
LogTruncate bool `json:"log_truncate,omitempty"yaml:"log_truncate,omitempty"`
Control *control.Config `json:"control,omitempty"yaml:"control,omitempty"`
Scheduler *scheduler.Config `json:"scheduler,omitempty"yaml:"scheduler,omitempty"`
RestAPI *rest.Config `json:"restapi,omitempty"yaml:"restapi,omitempty"`
Tribe *tribe.Config `json:"tribe,omitempty"yaml:"tribe,omitempty"`
}

const (
Expand All @@ -129,6 +135,10 @@ const (
"description": "path to log file for snapd to use",
"type": "string"
},
"log_truncate": {
"description": "truncate log file default is false",
"type": "boolean"
},
"gomaxprocs": {
"description": "value to be used for gomaxprocs",
"type": "integer",
Expand Down Expand Up @@ -179,6 +189,7 @@ func main() {
app.Flags = []cli.Flag{
flLogLevel,
flLogPath,
flLogTruncate,
flMaxProcs,
flConfig,
}
Expand Down Expand Up @@ -227,8 +238,11 @@ func action(ctx *cli.Context) {
if !f.IsDir() {
log.Fatal("log path provided must be a directory")
}

file, err := os.OpenFile(fmt.Sprintf("%s/snapd.log", logPath), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
aMode := os.O_APPEND
if cfg.LogTruncate {
aMode = os.O_TRUNC
}
file, err := os.OpenFile(fmt.Sprintf("%s/snapd.log", logPath), os.O_RDWR|os.O_CREATE|aMode, 0666)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -435,13 +449,14 @@ func action(ctx *cli.Context) {
// get the default snapd configuration
func getDefaultConfig() *Config {
return &Config{
LogLevel: defaultLogLevel,
GoMaxProcs: defaultGoMaxProcs,
LogPath: defaultLogPath,
Control: control.GetDefaultConfig(),
Scheduler: scheduler.GetDefaultConfig(),
RestAPI: rest.GetDefaultConfig(),
Tribe: tribe.GetDefaultConfig(),
LogLevel: defaultLogLevel,
GoMaxProcs: defaultGoMaxProcs,
LogPath: defaultLogPath,
LogTruncate: defaultLogTruncate,
Control: control.GetDefaultConfig(),
Scheduler: scheduler.GetDefaultConfig(),
RestAPI: rest.GetDefaultConfig(),
Tribe: tribe.GetDefaultConfig(),
}
}

Expand Down Expand Up @@ -560,6 +575,7 @@ func applyCmdLineFlags(cfg *Config, ctx *cli.Context) {
cfg.GoMaxProcs = setIntVal(cfg.GoMaxProcs, ctx, "max-procs")
cfg.LogLevel = setIntVal(cfg.LogLevel, ctx, "log-level")
cfg.LogPath = setStringVal(cfg.LogPath, ctx, "log-path")
cfg.LogTruncate = setBoolVal(cfg.LogTruncate, ctx, "log-truncate")
// next for the flags related to the control package
cfg.Control.MaxRunningPlugins = setIntVal(cfg.Control.MaxRunningPlugins, ctx, "max-running-plugins")
cfg.Control.PluginTrust = setIntVal(cfg.Control.PluginTrust, ctx, "plugin-trust")
Expand Down Expand Up @@ -680,6 +696,10 @@ func (c *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(v, &(c.LogPath)); err != nil {
return fmt.Errorf("%v (while parsing 'log_path')", err)
}
case "log_truncate":
if err := json.Unmarshal(v, &(c.LogTruncate)); err != nil {
return fmt.Errorf("%v (while parsing 'log_truncate')", err)
}
case "control":
if err := json.Unmarshal(v, c.Control); err != nil {
return err
Expand Down

0 comments on commit 91fc4c2

Please sign in to comment.